minime/test/minimetoken_normal.js

414 lines
15 KiB
JavaScript
Raw Normal View History

const ethConnector = require("ethconnector");
const assert = require("assert"); // node.js core module
const async = require("async");
2016-11-08 13:12:24 +00:00
const MiniMeToken = require("../js/minimetoken");
2016-11-08 13:12:24 +00:00
const verbose = false;
2016-11-08 13:12:24 +00:00
// b[0] -> 0, 0, 0, 0
// b[1] -> 0,10, 0, 0
// b[2] -> 0, 8, 2, 0
// b[3] -> 0, 9, 1, 0
// b[4] -> 0, 6, 1, 0
2016-11-13 05:49:15 +00:00
// Clone token
2016-11-08 13:12:24 +00:00
// b[5] -> 0, 6, 1, 0
// b[6] -> 0, 2, 5. 0
describe("MiniMeToken test", () => {
let miniMeToken;
let miniMeTokenClone;
const b = [];
2016-11-08 13:12:24 +00:00
before((done) => {
ethConnector.init("testrpc", { gasLimit: 4000000 }, done);
2016-11-08 13:12:24 +00:00
});
it("should deploy all the contracts", (done) => {
MiniMeToken.deploy(ethConnector.web3, {
2016-11-08 13:12:24 +00:00
tokenName: "MiniMe Test Token",
decimalUnits: 18,
tokenSymbol: "MMT",
}, (err, _miniMeToken) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.ok(_miniMeToken.contract.address);
2016-11-08 13:12:24 +00:00
miniMeToken = _miniMeToken;
done();
});
2017-02-12 13:00:40 +00:00
}).timeout(20000);
it("Should generate tokens for address 1", (done) => {
2016-11-08 13:12:24 +00:00
async.series([
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 0 ] = _blockNumber;
log("b[0]->" + b[ 0 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.generateTokens({
owner: ethConnector.accounts[ 1 ],
amount: 10,
from: ethConnector.accounts[ 0 ],
}, cb);
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.getState((err, _st) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_st.totalSupply, 10);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 10);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 1 ] = _blockNumber;
log("b[1]->" + b[ 1 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
], done);
}).timeout(6000);
it("Should transfer tokens from address 1 to address 2", (done) => {
2016-11-08 13:12:24 +00:00
async.series([
(cb) => {
miniMeToken.transfer({
to: ethConnector.accounts[ 2 ],
from: ethConnector.accounts[ 1 ],
amount: 2,
}, cb);
2016-11-08 13:12:24 +00:00
},
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 2 ] = _blockNumber;
log("b[2]->" + b[ 2 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.getState((err, _st) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_st.totalSupply, 10);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 8);
assert.equal(_st.balances[ ethConnector.accounts[ 2 ] ], 2);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.contract.balanceOfAt(
ethConnector.accounts[ 1 ],
b[ 1 ],
(err, _balance) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance).toNumber(), 10);
2016-11-08 13:12:24 +00:00
cb();
});
},
], done);
}).timeout(6000);
it("Should allow and transfer tokens from address 2 to address 1 allowed to 3", (done) => {
async.series([
(cb) => {
miniMeToken.approve({
spender: ethConnector.accounts[ 3 ],
amount: 2,
from: ethConnector.accounts[ 2 ],
}, cb);
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.allowance({
owner: ethConnector.accounts[ 2 ],
spender: ethConnector.accounts[ 3 ],
}, (err, _allowed) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_allowed, 2);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.contract.transferFrom(
ethConnector.accounts[ 2 ],
ethConnector.accounts[ 1 ],
ethConnector.web3.toWei(1),
{
from: ethConnector.accounts[ 3 ],
gas: 200000,
}, (err) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.allowance({
owner: ethConnector.accounts[ 2 ],
spender: ethConnector.accounts[ 3 ],
}, (err, _allowed) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_allowed, 1);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 3 ] = _blockNumber;
log("b[3]->" + b[ 3 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.getState((err, _st) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_st.totalSupply, 10);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 9);
assert.equal(_st.balances[ ethConnector.accounts[ 2 ] ], 1);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 1 ], b[ 2 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 8);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 2 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 2);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 1 ], b[ 1 ],
(err, _balance) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 10);
2016-11-08 13:12:24 +00:00
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 1 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 0);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 1 ], b[ 0 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 0);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 0 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 0);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 1 ], 0,
(err, _balance) => {
2016-12-11 17:37:52 +00:00
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 0);
2016-12-11 17:37:52 +00:00
cb();
});
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 2 ], 0,
(err, _balance) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 0);
cb();
2016-11-08 13:12:24 +00:00
});
},
], done);
});
it("Should Destroy 3 tokens from 1 and 1 from 2", (done) => {
async.series([
(cb) => {
miniMeToken.destroyTokens({
owner: ethConnector.accounts[ 1 ],
amount: 3,
from: ethConnector.accounts[ 0 ],
}, cb);
2016-11-08 13:12:24 +00:00
},
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 4 ] = _blockNumber;
log("b[4]->" + b[ 4 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.getState((err, _st) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_st.totalSupply, 7);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 6);
2016-11-08 13:12:24 +00:00
cb();
});
},
], done);
});
it("Should Create the clone token", (done) => {
async.series([
(cb) => {
miniMeToken.createCloneToken({
cloneTokenName: "Clone Token 1",
cloneDecimalUnits: 18,
cloneTokenSymbol: "MMTc",
snapshotBlock: Number.MAX_SAFE_INTEGER,
transfersEnabled: true,
}, (err, _miniMeTokenClone) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
miniMeTokenClone = _miniMeTokenClone;
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 5 ] = _blockNumber;
log("b[5]->" + b[ 5 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeTokenClone.getState((err, _st) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_st.parentToken, miniMeToken.contract.address);
assert.equal(_st.parentSnapShotBlock, b[ 5 ]);
assert.equal(_st.totalSupply, 7);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 6);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeTokenClone.contract.totalSupplyAt(b[ 4 ], (err, _balance) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 7);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeTokenClone.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 4 ],
(err, _balance) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 1);
2016-11-08 13:12:24 +00:00
cb();
});
2016-11-08 13:12:24 +00:00
},
], done);
2017-02-12 13:00:40 +00:00
}).timeout(6000000);
it("Should move tokens in the clone token from 2 to 3", (done) => {
async.series([
(cb) => {
miniMeTokenClone.transfer({
to: ethConnector.accounts[ 2 ],
amount: 4,
from: ethConnector.accounts[ 1 ],
}, cb);
2016-11-08 13:12:24 +00:00
},
(cb) => {
ethConnector.web3.eth.getBlockNumber((err, _blockNumber) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
b[ 6 ] = _blockNumber;
log("b[6]->" + b[ 6 ]);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeTokenClone.getState((err, _st) => {
2016-11-08 13:12:24 +00:00
assert.ifError(err);
assert.equal(_st.totalSupply, 7);
assert.equal(_st.balances[ ethConnector.accounts[ 1 ] ], 2);
assert.equal(_st.balances[ ethConnector.accounts[ 2 ] ], 5);
2016-11-08 13:12:24 +00:00
cb();
});
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 1 ], b[ 5 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 6);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeToken.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 5 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 1);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeTokenClone.contract.balanceOfAt(ethConnector.accounts[ 1 ], b[ 5 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 6);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeTokenClone.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 5 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 1);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeTokenClone.contract.balanceOfAt(ethConnector.accounts[ 1 ], b[ 4 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 6);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeTokenClone.contract.balanceOfAt(ethConnector.accounts[ 2 ], b[ 4 ],
(err, _balance) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_balance), 1);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeTokenClone.contract.totalSupplyAt(b[ 5 ],
(err, _totalSupply) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_totalSupply), 7);
cb();
});
2016-11-08 13:12:24 +00:00
},
(cb) => {
miniMeTokenClone.contract.totalSupplyAt(b[ 4 ],
(err, _totalSupply) => {
assert.ifError(err);
assert.equal(ethConnector.web3.fromWei(_totalSupply), 7);
cb();
});
2016-11-08 13:12:24 +00:00
},
], done);
2016-11-08 13:12:24 +00:00
});
function log(S) {
if (verbose) {
console.log(S);
}
}
});