Add findMethod function in ERC827Token test
This commit is contained in:
@ -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,16 +121,16 @@ 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'
|
||||||
|
, async function () {
|
||||||
const message = await Message.new();
|
const message = await Message.new();
|
||||||
|
|
||||||
const extraData = message.contract.showMessage.getData(
|
const extraData = message.contract.showMessage.getData(
|
||||||
web3.toHex(123456), 666, 'Transfer Done'
|
web3.toHex(123456), 666, 'Transfer Done'
|
||||||
);
|
);
|
||||||
|
const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes');
|
||||||
// Use method #8 tranfer of the abi to encode the data tx
|
const transferData = ethjsABI.encodeMethod(abiMethod,
|
||||||
const transferData = ethjsABI.encodeMethod(token.abi[8],
|
|
||||||
[message.contract.address, 100, extraData]
|
[message.contract.address, 100, extraData]
|
||||||
);
|
);
|
||||||
const transaction = await token.sendTransaction(
|
const transaction = await token.sendTransaction(
|
||||||
@ -134,15 +144,17 @@ contract('ERC827 Token', function (accounts) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return correct allowance after approve (with data) and show the event on receiver contract', async function () {
|
it(
|
||||||
|
'should return correct allowance after approve (with data) and show the event on receiver contract'
|
||||||
|
, async function () {
|
||||||
const message = await Message.new();
|
const message = await Message.new();
|
||||||
|
|
||||||
const extraData = message.contract.showMessage.getData(
|
const extraData = message.contract.showMessage.getData(
|
||||||
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,
|
||||||
[message.contract.address, 100, extraData]
|
[message.contract.address, 100, extraData]
|
||||||
);
|
);
|
||||||
const transaction = await token.sendTransaction(
|
const transaction = await token.sendTransaction(
|
||||||
@ -156,7 +168,9 @@ contract('ERC827 Token', function (accounts) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return correct balances after transferFrom (with data) and show the event on receiver contract', async function () {
|
it(
|
||||||
|
'should return correct balances after transferFrom (with data) and show the event on receiver contract'
|
||||||
|
, async function () {
|
||||||
const message = await Message.new();
|
const message = await Message.new();
|
||||||
|
|
||||||
const extraData = message.contract.showMessage.getData(
|
const extraData = message.contract.showMessage.getData(
|
||||||
@ -169,8 +183,8 @@ contract('ERC827 Token', function (accounts) {
|
|||||||
await token.allowance(accounts[0], accounts[1])
|
await token.allowance(accounts[0], accounts[1])
|
||||||
);
|
);
|
||||||
|
|
||||||
// Use method #7 transferFrom 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], message.contract.address, 100, extraData]
|
[accounts[0], message.contract.address, 100, extraData]
|
||||||
);
|
);
|
||||||
const transaction = await token.sendTransaction(
|
const transaction = await token.sendTransaction(
|
||||||
@ -189,8 +203,8 @@ contract('ERC827 Token', function (accounts) {
|
|||||||
|
|
||||||
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(
|
||||||
|
|||||||
Reference in New Issue
Block a user