Add findMethod function in ERC827Token test

pull/518/head
AugustoL 7 years ago
parent 7bd95b1e31
commit e911b4d556
  1. 182
      test/ERC827Token.js

@ -4,6 +4,7 @@ var Message = artifacts.require('./mock/MessageHelper.sol');
var ERC827TokenMock = artifacts.require('./mock/ERC827TokenMock.sol'); var ERC827TokenMock = artifacts.require('./mock/ERC827TokenMock.sol');
var BigNumber = web3.BigNumber; var BigNumber = web3.BigNumber;
var _ = require('lodash');
var ethjsABI = require('ethjs-abi'); var ethjsABI = require('ethjs-abi');
require('chai') require('chai')
.use(require('chai-as-promised')) .use(require('chai-as-promised'))
@ -13,6 +14,15 @@ require('chai')
contract('ERC827 Token', function (accounts) { contract('ERC827 Token', function (accounts) {
let token; let token;
function findMethod (abi, name, args) {
for (var i = 0; i < abi.length; i++) {
const methodArgs = _.map(abi[i].inputs, 'type').join(',');
if ((abi[i].name === name) && (methodArgs === args)) {
return abi[i];
}
}
}
beforeEach(async function () { beforeEach(async function () {
token = await ERC827TokenMock.new(accounts[0], 100); token = await ERC827TokenMock.new(accounts[0], 100);
}); });
@ -111,86 +121,90 @@ contract('ERC827 Token', function (accounts) {
}); });
describe('Test ERC827 methods', function () { describe('Test ERC827 methods', function () {
it(
it('should return correct balances after transfer (with data) and show the event on receiver contract', async function () { 'should return correct balances after transfer (with data) and show the event on receiver contract'
const message = await Message.new(); , async function () {
const message = await Message.new();
const extraData = message.contract.showMessage.getData(
web3.toHex(123456), 666, 'Transfer Done' const extraData = message.contract.showMessage.getData(
); web3.toHex(123456), 666, 'Transfer Done'
);
// Use method #8 tranfer of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes');
const transferData = ethjsABI.encodeMethod(token.abi[8], const transferData = ethjsABI.encodeMethod(abiMethod,
[message.contract.address, 100, extraData] [message.contract.address, 100, extraData]
); );
const transaction = await token.sendTransaction( const transaction = await token.sendTransaction(
{ from: accounts[0], data: transferData } { from: accounts[0], data: transferData }
); );
assert.equal(2, transaction.receipt.logs.length); assert.equal(2, transaction.receipt.logs.length);
new BigNumber(100).should.be.bignumber.equal( new BigNumber(100).should.be.bignumber.equal(
await token.balanceOf(message.contract.address) await token.balanceOf(message.contract.address)
); );
}); });
it('should return correct allowance after approve (with data) and show the event on receiver contract', async function () { it(
const message = await Message.new(); 'should return correct allowance after approve (with data) and show the event on receiver contract'
, async function () {
const extraData = message.contract.showMessage.getData( const message = await Message.new();
web3.toHex(123456), 666, 'Transfer Done'
); const extraData = message.contract.showMessage.getData(
web3.toHex(123456), 666, 'Transfer Done'
// Use method #3 approve of the abi to encode the data tx );
const approveData = ethjsABI.encodeMethod(token.abi[3],
[message.contract.address, 100, extraData] const abiMethod = findMethod(token.abi, 'approve', 'address,uint256,bytes');
); const approveData = ethjsABI.encodeMethod(abiMethod,
const transaction = await token.sendTransaction( [message.contract.address, 100, extraData]
{ from: accounts[0], data: approveData } );
); const transaction = await token.sendTransaction(
{ from: accounts[0], data: approveData }
assert.equal(2, transaction.receipt.logs.length); );
new BigNumber(100).should.be.bignumber.equal( assert.equal(2, transaction.receipt.logs.length);
await token.allowance(accounts[0], message.contract.address)
); new BigNumber(100).should.be.bignumber.equal(
}); await token.allowance(accounts[0], message.contract.address)
);
it('should return correct balances after transferFrom (with data) and show the event on receiver contract', async function () { });
const message = await Message.new();
it(
const extraData = message.contract.showMessage.getData( 'should return correct balances after transferFrom (with data) and show the event on receiver contract'
web3.toHex(123456), 666, 'Transfer Done' , async function () {
); const message = await Message.new();
await token.approve(accounts[1], 100, { from: accounts[0] }); const extraData = message.contract.showMessage.getData(
web3.toHex(123456), 666, 'Transfer Done'
new BigNumber(100).should.be.bignumber.equal( );
await token.allowance(accounts[0], accounts[1])
); await token.approve(accounts[1], 100, { from: accounts[0] });
// Use method #7 transferFrom of the abi to encode the data tx new BigNumber(100).should.be.bignumber.equal(
const transferFromData = ethjsABI.encodeMethod(token.abi[7], await token.allowance(accounts[0], accounts[1])
[accounts[0], message.contract.address, 100, extraData] );
);
const transaction = await token.sendTransaction( const abiMethod = findMethod(token.abi, 'transferFrom', 'address,address,uint256,bytes');
{ from: accounts[1], data: transferFromData } const transferFromData = ethjsABI.encodeMethod(abiMethod,
); [accounts[0], message.contract.address, 100, extraData]
);
assert.equal(2, transaction.receipt.logs.length); const transaction = await token.sendTransaction(
{ from: accounts[1], data: transferFromData }
new BigNumber(100).should.be.bignumber.equal( );
await token.balanceOf(message.contract.address)
); assert.equal(2, transaction.receipt.logs.length);
});
new BigNumber(100).should.be.bignumber.equal(
await token.balanceOf(message.contract.address)
);
});
it('should fail inside approve (with data)', async function () { it('should fail inside approve (with data)', async function () {
const message = await Message.new(); const message = await Message.new();
const extraData = message.contract.fail.getData(); const extraData = message.contract.fail.getData();
// Use method #3 approve of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'approve', 'address,uint256,bytes');
const approveData = ethjsABI.encodeMethod(token.abi[3], const approveData = ethjsABI.encodeMethod(abiMethod,
[message.contract.address, 10, extraData] [message.contract.address, 10, extraData]
); );
await token.sendTransaction( await token.sendTransaction(
@ -207,8 +221,8 @@ contract('ERC827 Token', function (accounts) {
const extraData = message.contract.fail.getData(); const extraData = message.contract.fail.getData();
// Use method #8 tranfer of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes');
const transferData = ethjsABI.encodeMethod(token.abi[8], const transferData = ethjsABI.encodeMethod(abiMethod,
[message.contract.address, 10, extraData] [message.contract.address, 10, extraData]
); );
await token.sendTransaction( await token.sendTransaction(
@ -227,8 +241,8 @@ contract('ERC827 Token', function (accounts) {
await token.approve(accounts[1], 10, { from: accounts[2] }); await token.approve(accounts[1], 10, { from: accounts[2] });
// Use method #7 tranferFrom of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'transferFrom', 'address,address,uint256,bytes');
const transferFromData = ethjsABI.encodeMethod(token.abi[7], const transferFromData = ethjsABI.encodeMethod(abiMethod,
[accounts[2], message.contract.address, 10, extraData] [accounts[2], message.contract.address, 10, extraData]
); );
await token.sendTransaction( await token.sendTransaction(
@ -249,8 +263,8 @@ contract('ERC827 Token', function (accounts) {
web3.toHex(123456), 666, 'Transfer Done' web3.toHex(123456), 666, 'Transfer Done'
); );
// Use method #3 approve of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'approve', 'address,uint256,bytes');
const approveData = ethjsABI.encodeMethod(token.abi[3], const approveData = ethjsABI.encodeMethod(abiMethod,
[token.contract.address, 100, extraData] [token.contract.address, 100, extraData]
); );
await token.sendTransaction( await token.sendTransaction(
@ -265,8 +279,8 @@ contract('ERC827 Token', function (accounts) {
web3.toHex(123456), 666, 'Transfer Done' web3.toHex(123456), 666, 'Transfer Done'
); );
// Use method #8 tranfer of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes');
const transferData = ethjsABI.encodeMethod(token.abi[8], const transferData = ethjsABI.encodeMethod(abiMethod,
[token.contract.address, 100, extraData] [token.contract.address, 100, extraData]
); );
await token.sendTransaction( await token.sendTransaction(
@ -283,8 +297,8 @@ contract('ERC827 Token', function (accounts) {
await token.approve(accounts[1], 1, { from: accounts[0] }); await token.approve(accounts[1], 1, { from: accounts[0] });
// Use method #7 tranferFrom of the abi to encode the data tx const abiMethod = findMethod(token.abi, 'transferFrom', 'address,address,uint256,bytes');
const transferFromData = ethjsABI.encodeMethod(token.abi[7], const transferFromData = ethjsABI.encodeMethod(abiMethod,
[accounts[0], token.contract.address, 1, extraData] [accounts[0], token.contract.address, 1, extraData]
); );
await token.sendTransaction( await token.sendTransaction(

Loading…
Cancel
Save