Fix Create and destroy tokens in cloned tokens

This commit is contained in:
Jordi Baylina 2017-07-30 10:41:34 +02:00
parent 6a5e38f4bd
commit d8983286f2
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
5 changed files with 4743 additions and 51 deletions

View File

@ -415,7 +415,7 @@ contract MiniMeToken is Controlled {
/// @return True if the tokens are generated correctly /// @return True if the tokens are generated correctly
function generateTokens(address _owner, uint _amount function generateTokens(address _owner, uint _amount
) onlyController returns (bool) { ) onlyController returns (bool) {
uint curTotalSupply = getValueAt(totalSupplyHistory, block.number); uint curTotalSupply = totalSupply();
if (curTotalSupply + _amount < curTotalSupply) throw; // Check for overflow if (curTotalSupply + _amount < curTotalSupply) throw; // Check for overflow
updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount); updateValueAtNow(totalSupplyHistory, curTotalSupply + _amount);
var previousBalanceTo = balanceOf(_owner); var previousBalanceTo = balanceOf(_owner);
@ -432,7 +432,7 @@ contract MiniMeToken is Controlled {
/// @return True if the tokens are burned correctly /// @return True if the tokens are burned correctly
function destroyTokens(address _owner, uint _amount function destroyTokens(address _owner, uint _amount
) onlyController returns (bool) { ) onlyController returns (bool) {
uint curTotalSupply = getValueAt(totalSupplyHistory, block.number); uint curTotalSupply = totalSupply();
if (curTotalSupply < _amount) throw; if (curTotalSupply < _amount) throw;
updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount); updateValueAtNow(totalSupplyHistory, curTotalSupply - _amount);
var previousBalanceFrom = balanceOf(_owner); var previousBalanceFrom = balanceOf(_owner);

File diff suppressed because one or more lines are too long

81
dist/minimetoken.js vendored
View File

@ -1,33 +1,29 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _async = require("async");
var _async2 = _interopRequireDefault(_async);
var _bignumber = require("bignumber.js");
var _bignumber2 = _interopRequireDefault(_bignumber);
var _runethtx = require("runethtx");
var _MiniMeTokenSol = require("../contracts/MiniMeToken.sol.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var MiniMeToken = function () { var async = require("async");
var BigNumber = require("bignumber.js");
var _require = require("runethtx"),
_deploy = _require.deploy,
sendContractTx = _require.sendContractTx,
asyncfunc = _require.asyncfunc;
var _require2 = require("../contracts/MiniMeToken.sol.js"),
MiniMeTokenAbi = _require2.MiniMeTokenAbi,
MiniMeTokenByteCode = _require2.MiniMeTokenByteCode,
MiniMeTokenFactoryAbi = _require2.MiniMeTokenFactoryAbi,
MiniMeTokenFactoryByteCode = _require2.MiniMeTokenFactoryByteCode;
module.exports = function () {
function MiniMeToken(web3, address) { function MiniMeToken(web3, address) {
_classCallCheck(this, MiniMeToken); _classCallCheck(this, MiniMeToken);
this.web3 = web3; this.web3 = web3;
this.contract = this.web3.eth.contract(_MiniMeTokenSol.MiniMeTokenAbi).at(address); this.contract = this.web3.eth.contract(MiniMeTokenAbi).at(address);
} }
_createClass(MiniMeToken, [{ _createClass(MiniMeToken, [{
@ -35,12 +31,12 @@ var MiniMeToken = function () {
value: function getState(_cb) { value: function getState(_cb) {
var _this = this; var _this = this;
return (0, _runethtx.asyncfunc)(function (cb) { return asyncfunc(function (cb) {
var st = { var st = {
balances: {} balances: {}
}; };
var accounts = void 0; var accounts = void 0;
_async2.default.series([function (cb1) { async.series([function (cb1) {
_this.contract.name(function (err, _name) { _this.contract.name(function (err, _name) {
if (err) { if (err) {
cb1(err);return; cb1(err);return;
@ -77,7 +73,7 @@ var MiniMeToken = function () {
if (err) { if (err) {
cb1(err);return; cb1(err);return;
} }
st.totalSupply = _totalSupply.div(new _bignumber2.default(10).pow(st.decimals)).toNumber(); st.totalSupply = _totalSupply.div(new BigNumber(10).pow(st.decimals)).toNumber();
cb1(); cb1();
}); });
}, function (cb1) { }, function (cb1) {
@ -97,12 +93,12 @@ var MiniMeToken = function () {
cb1(); cb1();
}); });
}, function (cb1) { }, function (cb1) {
_async2.default.eachSeries(accounts, function (account, cb2) { async.eachSeries(accounts, function (account, cb2) {
_this.contract.balanceOf(account, function (err, res) { _this.contract.balanceOf(account, function (err, res) {
if (err) { if (err) {
cb2(err);return; cb2(err);return;
} }
st.balances[account] = res.div(new _bignumber2.default(10).pow(st.decimals)).toNumber(); st.balances[account] = res.div(new BigNumber(10).pow(st.decimals)).toNumber();
cb2(); cb2();
}); });
}, cb1); }, cb1);
@ -116,8 +112,8 @@ var MiniMeToken = function () {
value: function createCloneToken(opts, _cb) { value: function createCloneToken(opts, _cb) {
var _this2 = this; var _this2 = this;
return (0, _runethtx.asyncfunc)(function (cb) { return asyncfunc(function (cb) {
(0, _runethtx.sendContractTx)(_this2.web3, _this2.contract, "createCloneToken", opts, function (err2, txHash) { sendContractTx(_this2.web3, _this2.contract, "createCloneToken", opts, function (err2, txHash) {
if (err2) { if (err2) {
cb(err2); cb(err2);
return; return;
@ -155,15 +151,15 @@ var MiniMeToken = function () {
value: function convertAmountAndSend(method, opts, _cb) { value: function convertAmountAndSend(method, opts, _cb) {
var _this3 = this; var _this3 = this;
return (0, _runethtx.asyncfunc)(function (cb) { return asyncfunc(function (cb) {
_this3.contract.decimals(function (err, _decimals) { _this3.contract.decimals(function (err, _decimals) {
if (err) { if (err) {
cb(err); cb(err);
return; return;
} }
var params = Object.assign({}, opts); var params = Object.assign({}, opts);
params.amount = new _bignumber2.default(10).pow(_decimals).mul(params.amount); params.amount = new BigNumber(10).pow(_decimals).mul(params.amount);
(0, _runethtx.sendContractTx)(_this3.web3, _this3.contract, method, params, function (err2, txHash) { sendContractTx(_this3.web3, _this3.contract, method, params, function (err2, txHash) {
if (err2) { if (err2) {
cb(err2); cb(err2);
return; return;
@ -200,10 +196,10 @@ var MiniMeToken = function () {
value: function allowance(opts, _cb) { value: function allowance(opts, _cb) {
var _this4 = this; var _this4 = this;
return (0, _runethtx.asyncfunc)(function (cb) { return asyncfunc(function (cb) {
var decimals = void 0; var decimals = void 0;
var allowance = void 0; var allowance = void 0;
_async2.default.series([function (cb1) { async.series([function (cb1) {
_this4.contract.decimals(function (err, _decimals) { _this4.contract.decimals(function (err, _decimals) {
if (err) { if (err) {
cb1(err); cb1(err);
@ -218,7 +214,7 @@ var MiniMeToken = function () {
cb1(err); cb1(err);
return; return;
} }
allowance = res.div(new _bignumber2.default(10).pow(decimals)).toNumber(); allowance = res.div(new BigNumber(10).pow(decimals)).toNumber();
cb1(); cb1();
}); });
}], function (err2) { }], function (err2) {
@ -233,15 +229,15 @@ var MiniMeToken = function () {
}], [{ }], [{
key: "deploy", key: "deploy",
value: function deploy(web3, opts, _cb) { value: function deploy(web3, opts, _cb) {
return (0, _runethtx.asyncfunc)(function (cb) { return asyncfunc(function (cb) {
var params = Object.assign({}, opts); var params = Object.assign({}, opts);
params.parentToken = params.parentToken || 0; params.parentToken = params.parentToken || 0;
params.parentSnapShotBlock = params.parentSnapShotBlock || 0; params.parentSnapShotBlock = params.parentSnapShotBlock || 0;
params.transfersEnabled = typeof params.transfersEnabled === "undefined" ? true : params.transfersEnabled; params.transfersEnabled = typeof params.transfersEnabled === "undefined" ? true : params.transfersEnabled;
_async2.default.series([function (cb1) { async.series([function (cb1) {
params.abi = _MiniMeTokenSol.MiniMeTokenFactoryAbi; params.abi = MiniMeTokenFactoryAbi;
params.byteCode = _MiniMeTokenSol.MiniMeTokenFactoryByteCode; params.byteCode = MiniMeTokenFactoryByteCode;
(0, _runethtx.deploy)(web3, params, function (err, _tokenFactory) { _deploy(web3, params, function (err, _tokenFactory) {
if (err) { if (err) {
cb1(err); cb1(err);
return; return;
@ -250,9 +246,9 @@ var MiniMeToken = function () {
cb1(); cb1();
}); });
}, function (cb1) { }, function (cb1) {
params.abi = _MiniMeTokenSol.MiniMeTokenAbi; params.abi = MiniMeTokenAbi;
params.byteCode = _MiniMeTokenSol.MiniMeTokenByteCode; params.byteCode = MiniMeTokenByteCode;
(0, _runethtx.deploy)(web3, params, cb1); _deploy(web3, params, cb1);
}], function (err, res) { }], function (err, res) {
if (err) { if (err) {
cb(err); cb(err);
@ -267,6 +263,3 @@ var MiniMeToken = function () {
return MiniMeToken; return MiniMeToken;
}(); }();
exports.default = MiniMeToken;
module.exports = exports["default"];

4669
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -78,6 +78,7 @@ describe("MiniMeToken test", () => {
to: ethConnector.accounts[ 2 ], to: ethConnector.accounts[ 2 ],
from: ethConnector.accounts[ 1 ], from: ethConnector.accounts[ 1 ],
amount: 2, amount: 2,
extraGas: 30000,
}, cb); }, cb);
}, },
(cb) => { (cb) => {
@ -116,6 +117,7 @@ describe("MiniMeToken test", () => {
spender: ethConnector.accounts[ 3 ], spender: ethConnector.accounts[ 3 ],
amount: 2, amount: 2,
from: ethConnector.accounts[ 2 ], from: ethConnector.accounts[ 2 ],
extraGas: 20000,
}, cb); }, cb);
}, },
(cb) => { (cb) => {
@ -261,7 +263,6 @@ describe("MiniMeToken test", () => {
}, },
], done); ], done);
}); });
it("Should Create the clone token", (done) => { it("Should Create the clone token", (done) => {
async.series([ async.series([
(cb) => { (cb) => {
@ -312,6 +313,14 @@ describe("MiniMeToken test", () => {
}, },
], done); ], done);
}).timeout(6000000); }).timeout(6000000);
it("Should mine one block to take effect clone", (done) => {
miniMeToken.transfer({
to: ethConnector.accounts[ 1 ],
from: ethConnector.accounts[ 1 ],
amount: 1,
extraGas: 30000,
}, done);
});
it("Should move tokens in the clone token from 2 to 3", (done) => { it("Should move tokens in the clone token from 2 to 3", (done) => {
async.series([ async.series([
(cb) => { (cb) => {
@ -319,7 +328,7 @@ describe("MiniMeToken test", () => {
to: ethConnector.accounts[ 2 ], to: ethConnector.accounts[ 2 ],
amount: 4, amount: 4,
from: ethConnector.accounts[ 1 ], from: ethConnector.accounts[ 1 ],
gas: 200000, extraGas: 200000,
}, cb); }, cb);
}, },
(cb) => { (cb) => {
@ -405,6 +414,27 @@ describe("MiniMeToken test", () => {
}, },
], done); ], done);
}).timeout(6000000); }).timeout(6000000);
it("Should create tokens in the child token", (done) => {
async.series([
(cb) => {
miniMeTokenClone.generateTokens({
owner: ethConnector.accounts[ 1 ],
amount: 10,
from: ethConnector.accounts[ 0 ],
extraGas: 300000,
}, cb);
},
(cb) => {
miniMeTokenClone.getState((err, _st) => {
assert.ifError(err);
assert.equal(_st.totalSupply, 17);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 12);
assert.equal(_st.balances[ ethConnector.accounts[ 2 ] ], 5);
cb();
});
},
], done);
});
function log(S) { function log(S) {
if (verbose) { if (verbose) {