ens-usernames/test/erc20token.js

78 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-05-19 20:00:16 +00:00
2018-05-20 01:37:58 +00:00
exports.Test = (contractsConfig, afterDeploy) => {
describe("ERC20Token", async function() {
this.timeout(0);
var ERC20Token;
var accountsArr;
before(function(done) {
contractsConfig["ERC20Receiver"] = {};
EmbarkSpec.deployAll(contractsConfig, async function(accounts) {
ERC20Token = Contract;
accountsArr = accounts;
await afterDeploy(accounts, Contract);
done()
});
2018-05-19 20:00:16 +00:00
});
2018-05-19 20:00:16 +00:00
it("should transfer 1 token", async function() {
let initialBalance0 = await ERC20Token.methods.balanceOf(accountsArr[0]).call();
let initialBalance1 = await ERC20Token.methods.balanceOf(accountsArr[1]).call();
await ERC20Token.methods.transfer(accountsArr[1],1).send({from: accountsArr[0]});
let result0 = await ERC20Token.methods.balanceOf(accountsArr[0]).call();
let result1 = await ERC20Token.methods.balanceOf(accountsArr[1]).call();
2018-05-20 01:37:58 +00:00
assert.equal(result0, +initialBalance0-1, "account 0 balance unexpected");
assert.equal(result1, +initialBalance1+1, "account 1 balance unexpected");
2018-05-19 20:00:16 +00:00
});
2018-05-19 20:00:16 +00:00
it("should set approved amount", async function() {
await ERC20Token.methods.approve(accountsArr[2],10000000).send({from: accountsArr[0]});
let result = await ERC20Token.methods.allowance(accountsArr[0], accountsArr[2]).call();
assert.equal(result, 10000000);
});
2018-05-19 20:00:16 +00:00
it("should consume allowance amount", async function() {
let initialAllowance = await ERC20Token.methods.allowance(accountsArr[0], accountsArr[2]).call();
await ERC20Token.methods.transferFrom(accountsArr[0], accountsArr[0],1).send({from: accountsArr[2]});
let result = await ERC20Token.methods.allowance(accountsArr[0], accountsArr[2]).call();
assert.equal(result, +initialAllowance-1);
});
2018-05-19 20:00:16 +00:00
it("should transfer approved amount", async function() {
let initialBalance0 = await ERC20Token.methods.balanceOf(accountsArr[0]).call();
let initialBalance1 = await ERC20Token.methods.balanceOf(accountsArr[1]).call();
await ERC20Token.methods.transferFrom(accountsArr[0], accountsArr[1],1).send({from: accountsArr[2]});
let result0 = await ERC20Token.methods.balanceOf(accountsArr[0]).call();
let result1 = await ERC20Token.methods.balanceOf(accountsArr[1]).call();
assert.equal(result0, +initialBalance0-1);
assert.equal(result1, +initialBalance1+1);
});
2018-05-19 20:00:16 +00:00
it("should unset approved amount", async function() {
await ERC20Token.methods.approve(accountsArr[2],0).send({from: accountsArr[0]});
let result = await ERC20Token.methods.allowance(accountsArr[0], accountsArr[2]).call();
assert.equal(result, 0);
});
2018-05-19 20:00:16 +00:00
it("should deposit approved amount to contract ERC20Receiver", async function() {
await ERC20Token.methods.approve(ERC20Receiver.address,10).send({from: accountsArr[0]});
await ERC20Receiver.methods.depositToken(ERC20Token.address).send({from: accountsArr[0]});
let result = await ERC20Receiver.methods.tokenBalanceOf(ERC20Token.address, accountsArr[0]).call();
assert.equal(result, 10, "ERC20Receiver.tokenBalanceOf("+ERC20Token.address+","+accountsArr[0]+") wrong");
});
2018-05-13 17:01:59 +00:00
2018-05-19 20:00:16 +00:00
it("should witdraw approved amount from contract ERC20Receiver", async function() {
let tokenBalance = await ERC20Receiver.methods.tokenBalanceOf(ERC20Token.address, accountsArr[0]).call();
await ERC20Receiver.methods.withdrawToken(ERC20Token.address, tokenBalance).send({from: accountsArr[0]});
tokenBalance = await ERC20Receiver.methods.tokenBalanceOf(ERC20Token.address, accountsArr[0]).call();
assert.equal(tokenBalance, 0, "ERC20Receiver.tokenBalanceOf("+ERC20Token.address+","+accountsArr[0]+") wrong");
});
2018-05-13 17:01:59 +00:00
2018-05-19 20:00:16 +00:00
//TODO: include checks for expected events fired
2018-05-13 17:01:59 +00:00
2018-05-19 20:00:16 +00:00
});
}