Skip to content

Commit

Permalink
test(tests): Update test to new dinamyc proxies and add ERC827Recieve…
Browse files Browse the repository at this point in the history
…r example
  • Loading branch information
AugustoL committed Jun 11, 2019
1 parent 69c8752 commit 683a3fa
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 137 deletions.
9 changes: 5 additions & 4 deletions test/ERC827/ERC20Migration.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import EVMRevert from '../helpers/EVMRevert';
var ERC20TokenMock = artifacts.require('ERC20TokenMock');
var ERC827Migratable = artifacts.require('ERC827Migratable');
const ERC20Mock = artifacts.require('ERC20Mock');
const ERC827Migratable = artifacts.require('ERC827Migratable');
const ERC827Proxy = artifacts.require('ERC827Proxy');

require('chai').use(require('chai-as-promised')).should();
const assert = require('chai').assert;
Expand All @@ -10,8 +11,8 @@ contract('ERC827Migratable', function (accounts) {
let erc20Token, erc827Token;

beforeEach(async function () {
erc20Token = await ERC20TokenMock.new(accounts[1], 100);
erc827Token = await ERC827Migratable.new(erc20Token.address);
erc20Token = await ERC20Mock.new(accounts[1], 100);
erc827Token = await ERC827Migratable.new(erc20Token.address, ERC827Proxy.bytecode);
await erc20Token.transfer(accounts[2], 10, { from: accounts[1] });
await erc20Token.transfer(accounts[3], 20, { from: accounts[1] });
await erc20Token.transfer(accounts[4], 30, { from: accounts[1] });
Expand Down
180 changes: 54 additions & 126 deletions test/ERC827/ERC827Token.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@

import EVMRevert from '../helpers/EVMRevert';
var Message = artifacts.require('MessageHelper');
var ERC827TokenMock = artifacts.require('ERC827Mock');
const ERC827Receiver = artifacts.require('ERC827Receiver');
const ERC827Mock = artifacts.require('ERC827Mock');
const ERC827Proxy = artifacts.require('ERC827Proxy');

require('chai').use(require('chai-as-promised')).should();
const assert = require('chai').assert;

contract('ERC827 Token', function (accounts) {
let token, message;
let token, receiverContract, extraData, extraDataFail;

beforeEach(async function () {
token = await ERC827TokenMock.new(accounts[0], 100);
token = await ERC827Mock.new(accounts[0], 100, ERC827Proxy.bytecode);
token.web3Instance = new web3.eth.Contract(token.abi, token.address);
message = await Message.new();
message.web3Instance = new web3.eth.Contract(message.abi, message.address);
receiverContract = await ERC827Receiver.new();
receiverContract.web3Instance = new web3.eth.Contract(receiverContract.abi, receiverContract.address);
extraDataFail = receiverContract.web3Instance.methods.fail().encodeABI();
extraData = receiverContract.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();
});

it('should return the correct totalSupply after construction', async function () {
Expand All @@ -22,18 +27,14 @@ contract('ERC827 Token', function (accounts) {
});

it('should return the correct allowance amount after approval', async function () {
let token = await ERC827TokenMock.new(accounts[0], 100);
await token.approve(accounts[1], 100);
let allowance = await token.allowance(accounts[0], accounts[1]);
assert.equal(allowance, 100);
assert.equal(await token.allowance(accounts[0], accounts[1]), 100);
});

it('should return correct balances after transfer', async function () {
await token.transfer(accounts[1], 100);
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
let balance1 = await token.balanceOf(accounts[1]);
assert.equal(balance1, 100);
assert.equal(await token.balanceOf(accounts[0]), 0);
assert.equal(await token.balanceOf(accounts[1]), 100);
});

it('should throw an error when trying to transfer more than balance', async function () {
Expand All @@ -43,12 +44,9 @@ contract('ERC827 Token', function (accounts) {
it('should return correct balances after transfering from another account', async function () {
await token.approve(accounts[1], 100);
await token.transferFrom(accounts[0], accounts[2], 100, { from: accounts[1] });
let balance0 = await token.balanceOf(accounts[0]);
assert.equal(balance0, 0);
let balance1 = await token.balanceOf(accounts[2]);
assert.equal(balance1, 100);
let balance2 = await token.balanceOf(accounts[1]);
assert.equal(balance2, 0);
assert.equal(await token.balanceOf(accounts[0]), 0);
assert.equal(await token.balanceOf(accounts[2]), 100);
assert.equal(await token.balanceOf(accounts[1]), 0);
});

it('should throw an error when trying to transfer more than allowed', async function () {
Expand Down Expand Up @@ -85,210 +83,140 @@ contract('ERC827 Token', function (accounts) {
it(
'should allow payment through transfer'
, async function () {
const extraData = message.web3Instance.methods.buyMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

const transaction = await token.transferAndCall(
message.address, 100, extraData, { from: accounts[0], value: 1000 }
receiverContract.address, 100, extraData, { from: accounts[0], value: 1000 }
);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.balanceOf(message.address), 100);
assert.equal(await web3.eth.getBalance(message.address), 1000);
assert.equal(3, transaction.receipt.rawLogs.length);
assert.equal(await token.balanceOf(receiverContract.address), 100);
assert.equal(await web3.eth.getBalance(receiverContract.address), 1000);
});

it(
'should allow payment through approve'
, async function () {
const extraData = message.web3Instance.methods.buyMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

const transaction = await token.approveAndCall(
message.address, 100, extraData, { from: accounts[0], value: 1000 }
receiverContract.address, 100, extraData, { from: accounts[0], value: 1000 }
);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.allowance(accounts[0], message.address), 100);
assert.equal(await web3.eth.getBalance(message.address), 1000);
assert.equal(3, transaction.receipt.rawLogs.length);
assert.equal(await token.allowance(accounts[0], receiverContract.address), 100);
assert.equal(await web3.eth.getBalance(receiverContract.address), 1000);
});

it(
'should allow payment through transferFrom'
, async function () {
const extraData = message.web3Instance.methods.buyMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approve(accounts[1], 100, { from: accounts[0] });

assert.equal(await token.allowance(accounts[0], accounts[1]), 100);

const transaction = await token.transferFromAndCall(
accounts[0], message.address, 100, extraData, { from: accounts[1], value: 1000 }
accounts[0], receiverContract.address, 100, extraData, { from: accounts[1], value: 1000 }
);

assert.equal(2, transaction.receipt.logs.length);

assert.equal(
await token.balanceOf(message.address), 100
);
assert.equal(
await web3.eth.getBalance(message.address), 1000
);
assert.equal(4, transaction.receipt.rawLogs.length);
assert.equal(await token.balanceOf(receiverContract.address), 100);
assert.equal(await web3.eth.getBalance(receiverContract.address), 1000);
});

it('should revert funds of failure inside approve (with data)', async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approveAndCall(
message.address, 10, extraData, { from: accounts[0], value: 1000 }
receiverContract.address, 10, extraDataFail, { from: accounts[0], value: 1000 }
).should.be.rejectedWith(EVMRevert);

// approval should not have gone through so allowance is still 0
assert.equal(await token.allowance(accounts[1], message.address), 0);
assert.equal(await web3.eth.getBalance(message.address), 0);
assert.equal(await token.allowance(accounts[1], receiverContract.address), 0);
assert.equal(await web3.eth.getBalance(receiverContract.address), 0);
});

it('should revert funds of failure inside transfer (with data)', async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.transferAndCall(
message.address, 10, extraData, { from: accounts[0], value: 1000 }
receiverContract.address, 10, extraDataFail, { from: accounts[0], value: 1000 }
).should.be.rejectedWith(EVMRevert);

// transfer should not have gone through, so balance is still 0
assert.equal(await token.balanceOf(message.address), 0);
assert.equal(await web3.eth.getBalance(message.address), 0);
assert.equal(await token.balanceOf(receiverContract.address), 0);
assert.equal(await web3.eth.getBalance(receiverContract.address), 0);
});

it('should revert funds of failure inside transferFrom (with data)', async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approve(accounts[1], 10, { from: accounts[2] });

await token.transferFromAndCall(
accounts[2], message.address, 10, extraData, { from: accounts[2], value: 1000 }
accounts[2], receiverContract.address, 10, extraData, { from: accounts[2], value: 1000 }
).should.be.rejectedWith(EVMRevert);

// transferFrom should have failed so balance is still 0 but allowance is 10
assert.equal(await token.allowance(accounts[2], accounts[1]), 10);
assert.equal(await token.balanceOf(message.address), 0);
assert.equal(await web3.eth.getBalance(message.address), 0);
assert.equal(await token.balanceOf(receiverContract.address), 0);
assert.equal(await web3.eth.getBalance(receiverContract.address), 0);
});

it(
'should return correct balances after transfer (with data) and show the event on receiver contract'
, async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

const transaction = await token.transferAndCall(message.address, 100, extraData);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.balanceOf(message.address), 100);
const transaction = await token.transferAndCall(receiverContract.address, 100, extraData);
assert.equal(3, transaction.receipt.rawLogs.length);
assert.equal(await token.balanceOf(receiverContract.address), 100);
});

it(
'should return correct allowance after approve (with data) and show the event on receiver contract'
, async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

const transaction = await token.approveAndCall(message.address, 100, extraData);

assert.equal(2, transaction.receipt.rawLogs.length);

assert.equal(await token.allowance(accounts[0], message.address), 100);
const transaction = await token.approveAndCall(receiverContract.address, 100, extraData);
assert.equal(3, transaction.receipt.rawLogs.length);
assert.equal(await token.allowance(accounts[0], receiverContract.address), 100);
});

it(
'should return correct balances after transferFrom (with data) and show the event on receiver contract'
, async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approve(accounts[1], 100, { from: accounts[0] });

assert.equal(await token.allowance(accounts[0], accounts[1]), 100);

const transaction = await token.transferFromAndCall(accounts[0], message.address, 100, extraData, {
const transaction = await token.transferFromAndCall(accounts[0], receiverContract.address, 100, extraData, {
from: accounts[1],
});

assert.equal(2, transaction.receipt.logs.length);

assert.equal(await token.balanceOf(message.address), 100);
assert.equal(4, transaction.receipt.rawLogs.length);
assert.equal(await token.balanceOf(receiverContract.address), 100);
});

it('should fail inside approve (with data)', async function () {
const extraData = message.web3Instance.methods.fail().encodeABI();

await token.approveAndCall(message.address, 10, extraData)
await token.approveAndCall(receiverContract.address, 10, extraDataFail)
.should.be.rejectedWith(EVMRevert);

// approval should not have gone through so allowance is still 0
assert.equal(await token.allowance(accounts[1], message.address), 0);
assert.equal(await token.allowance(accounts[1], receiverContract.address), 0);
});

it('should fail inside transfer (with data)', async function () {
const extraData = message.web3Instance.methods.fail().encodeABI();

await token.transferAndCall(message.address, 10, extraData)
await token.transferAndCall(receiverContract.address, 10, extraDataFail)
.should.be.rejectedWith(EVMRevert);

// transfer should not have gone through, so balance is still 0
assert.equal(await token.balanceOf(message.address), 0);
assert.equal(await token.balanceOf(receiverContract.address), 0);
});

it('should fail inside transferFrom (with data)', async function () {
const extraData = message.web3Instance.methods.fail().encodeABI();

await token.approve(accounts[1], 10, { from: accounts[2] });
await token.transferFromAndCall(accounts[2], message.address, 10, extraData, { from: accounts[1] })
await token.transferFromAndCall(accounts[2], receiverContract.address, 10, extraDataFail, { from: accounts[1] })
.should.be.rejectedWith(EVMRevert);

// transferFrom should have failed so balance is still 0 but allowance is 10
assert.equal(await token.allowance(accounts[2], accounts[1]), 10);
assert.equal(await token.balanceOf(message.address), 0);
assert.equal(await token.balanceOf(receiverContract.address), 0);
});

it('should fail approve (with data) when using token contract address as receiver', async function () {
const extraData = message.web3Instance.methods.fail().encodeABI();

await token.approveAndCall(token.address, 100, extraData, { from: accounts[0] })
.should.be.rejectedWith(EVMRevert);
});

it('should fail transfer (with data) when using token contract address as receiver', async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.transferAndCall(token.address, 100, extraData)
await token.transferAndCall(token.address, 100, extraData, { from: accounts[0] })
.should.be.rejectedWith(EVMRevert);
});

it('should fail transferFrom (with data) when using token contract address as receiver', async function () {
const extraData = message.web3Instance.methods.showMessage(
web3.utils.toHex(123456), 666, 'Transfer Done'
).encodeABI();

await token.approve(accounts[1], 1, { from: accounts[0] });

await token.transferFromAndCall(accounts[0], token.address, 1, extraData, { from: accounts[1] })
.should.be.rejectedWith(EVMRevert);
});
Expand Down
9 changes: 5 additions & 4 deletions test/examples/ClaimOtherTokensAttack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import EVMRevert from '../helpers/EVMRevert';
var ERC827TokenMock = artifacts.require('ERC827TokenMock');
var ERC20TokenMock = artifacts.require('ERC20TokenMock');
const ERC827Mock = artifacts.require('ERC827Mock');
const ERC20Mock = artifacts.require('ERC20Mock');
const ERC827Proxy = artifacts.require('ERC827Proxy');

require('chai')
.use(require('chai-as-promised'))
Expand All @@ -11,8 +12,8 @@ contract('ClaimOtherTokensAttack', function ([attacker, victim]) {
let erc827, erc20;

beforeEach(async function () {
erc827 = await ERC827TokenMock.new(attacker, 100);
erc20 = await ERC20TokenMock.new(victim, 100);
erc827 = await ERC827Mock.new(attacker, 100, ERC827Proxy.bytecode);
erc20 = await ERC20Mock.new(victim, 100);
erc20.web3Instance = new web3.eth.Contract(erc20.abi, erc20.address);
});

Expand Down
Loading

0 comments on commit 683a3fa

Please sign in to comment.