From 16535fbb87136eb22edc591e3e40d87826b33baa Mon Sep 17 00:00:00 2001 From: Elie Date: Mon, 23 Apr 2018 14:39:34 +0300 Subject: [PATCH] Update ERC827.sol to not use function overloading (#871) * Update ERC827.sol to not use function overloading * updated tests for erc827 function name changes * fixed broken test * removed findMethod from erc827 tests that is no longer necessary --- contracts/token/ERC827/ERC827.sol | 6 +- contracts/token/ERC827/ERC827Token.sol | 10 +-- test/token/ERC827/ERC827Token.js | 117 +++++-------------------- 3 files changed, 29 insertions(+), 104 deletions(-) diff --git a/contracts/token/ERC827/ERC827.sol b/contracts/token/ERC827/ERC827.sol index a0ee180366f..83187f90f4b 100644 --- a/contracts/token/ERC827/ERC827.sol +++ b/contracts/token/ERC827/ERC827.sol @@ -12,9 +12,9 @@ import "../ERC20/ERC20.sol"; * @dev approvals. */ contract ERC827 is ERC20 { - function approve(address _spender, uint256 _value, bytes _data) public returns (bool); - function transfer(address _to, uint256 _value, bytes _data) public returns (bool); - function transferFrom( + function approveAndCall( address _spender, uint256 _value, bytes _data) public returns (bool); + function transferAndCall( address _to, uint256 _value, bytes _data) public returns (bool); + function transferFromAndCall( address _from, address _to, uint256 _value, diff --git a/contracts/token/ERC827/ERC827Token.sol b/contracts/token/ERC827/ERC827Token.sol index 28dbfceeaec..28e15d08f49 100644 --- a/contracts/token/ERC827/ERC827Token.sol +++ b/contracts/token/ERC827/ERC827Token.sol @@ -34,7 +34,7 @@ contract ERC827Token is ERC827, StandardToken { * * @return true if the call function was executed successfully */ - function approve(address _spender, uint256 _value, bytes _data) public returns (bool) { + function approveAndCall(address _spender, uint256 _value, bytes _data) public returns (bool) { require(_spender != address(this)); super.approve(_spender, _value); @@ -54,7 +54,7 @@ contract ERC827Token is ERC827, StandardToken { * * @return true if the call function was executed successfully */ - function transfer(address _to, uint256 _value, bytes _data) public returns (bool) { + function transferAndCall(address _to, uint256 _value, bytes _data) public returns (bool) { require(_to != address(this)); super.transfer(_to, _value); @@ -74,7 +74,7 @@ contract ERC827Token is ERC827, StandardToken { * * @return true if the call function was executed successfully */ - function transferFrom( + function transferFromAndCall( address _from, address _to, uint256 _value, @@ -103,7 +103,7 @@ contract ERC827Token is ERC827, StandardToken { * @param _addedValue The amount of tokens to increase the allowance by. * @param _data ABI-encoded contract call to call `_spender` address. */ - function increaseApproval(address _spender, uint _addedValue, bytes _data) public returns (bool) { + function increaseApprovalAndCall(address _spender, uint _addedValue, bytes _data) public returns (bool) { require(_spender != address(this)); super.increaseApproval(_spender, _addedValue); @@ -126,7 +126,7 @@ contract ERC827Token is ERC827, StandardToken { * @param _subtractedValue The amount of tokens to decrease the allowance by. * @param _data ABI-encoded contract call to call `_spender` address. */ - function decreaseApproval(address _spender, uint _subtractedValue, bytes _data) public returns (bool) { + function decreaseApprovalAndCall(address _spender, uint _subtractedValue, bytes _data) public returns (bool) { require(_spender != address(this)); super.decreaseApproval(_spender, _subtractedValue); diff --git a/test/token/ERC827/ERC827Token.js b/test/token/ERC827/ERC827Token.js index f82f3fdf0cc..7302d3a2df0 100644 --- a/test/token/ERC827/ERC827Token.js +++ b/test/token/ERC827/ERC827Token.js @@ -4,8 +4,6 @@ var Message = artifacts.require('MessageHelper'); var ERC827TokenMock = artifacts.require('ERC827TokenMock'); var BigNumber = web3.BigNumber; -var _ = require('lodash'); -var ethjsABI = require('ethjs-abi'); require('chai') .use(require('chai-as-promised')) .use(require('chai-bignumber')(BigNumber)) @@ -14,15 +12,6 @@ require('chai') contract('ERC827 Token', function (accounts) { 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 () { token = await ERC827TokenMock.new(accounts[0], 100); }); @@ -94,13 +83,7 @@ contract('ERC827 Token', function (accounts) { }); it('should increase by 50 then decrease by 10', async function () { - const abiMethod = findMethod(token.abi, 'increaseApproval', 'address,uint256'); - const increaseApprovalData = ethjsABI.encodeMethod(abiMethod, - [accounts[1], 50] - ); - await token.sendTransaction( - { from: accounts[0], data: increaseApprovalData } - ); + await token.increaseApproval(accounts[1], 50); let postIncrease = await token.allowance(accounts[0], accounts[1]); preApproved.plus(50).should.be.bignumber.equal(postIncrease); await token.decreaseApproval(accounts[1], 10); @@ -135,13 +118,8 @@ contract('ERC827 Token', function (accounts) { const extraData = message.contract.showMessage.getData( web3.toHex(123456), 666, 'Transfer Done' ); - const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes'); - const transferData = ethjsABI.encodeMethod(abiMethod, - [message.contract.address, 100, extraData] - ); - const transaction = await token.sendTransaction( - { from: accounts[0], data: transferData } - ); + + const transaction = await token.transferAndCall(message.contract.address, 100, extraData); assert.equal(2, transaction.receipt.logs.length); @@ -159,13 +137,7 @@ contract('ERC827 Token', function (accounts) { web3.toHex(123456), 666, 'Transfer Done' ); - const abiMethod = findMethod(token.abi, 'approve', 'address,uint256,bytes'); - const approveData = ethjsABI.encodeMethod(abiMethod, - [message.contract.address, 100, extraData] - ); - const transaction = await token.sendTransaction( - { from: accounts[0], data: approveData } - ); + const transaction = await token.approveAndCall(message.contract.address, 100, extraData); assert.equal(2, transaction.receipt.logs.length); @@ -188,13 +160,7 @@ contract('ERC827 Token', function (accounts) { await token.allowance(accounts[0], message.contract.address) ); - const abiMethod = findMethod(token.abi, 'increaseApproval', 'address,uint256,bytes'); - const increaseApprovalData = ethjsABI.encodeMethod(abiMethod, - [message.contract.address, 50, extraData] - ); - const transaction = await token.sendTransaction( - { from: accounts[0], data: increaseApprovalData } - ); + const transaction = await token.increaseApprovalAndCall(message.contract.address, 50, extraData); assert.equal(2, transaction.receipt.logs.length); @@ -218,13 +184,7 @@ contract('ERC827 Token', function (accounts) { web3.toHex(123456), 666, 'Transfer Done' ); - const abiMethod = findMethod(token.abi, 'decreaseApproval', 'address,uint256,bytes'); - const decreaseApprovalData = ethjsABI.encodeMethod(abiMethod, - [message.contract.address, 60, extraData] - ); - const transaction = await token.sendTransaction( - { from: accounts[0], data: decreaseApprovalData } - ); + const transaction = await token.decreaseApprovalAndCall(message.contract.address, 60, extraData); assert.equal(2, transaction.receipt.logs.length); @@ -248,13 +208,9 @@ contract('ERC827 Token', function (accounts) { await token.allowance(accounts[0], accounts[1]) ); - const abiMethod = findMethod(token.abi, 'transferFrom', 'address,address,uint256,bytes'); - const transferFromData = ethjsABI.encodeMethod(abiMethod, - [accounts[0], message.contract.address, 100, extraData] - ); - const transaction = await token.sendTransaction( - { from: accounts[1], data: transferFromData } - ); + const transaction = await token.transferFromAndCall(accounts[0], message.contract.address, 100, extraData, { + from: accounts[1], + }); assert.equal(2, transaction.receipt.logs.length); @@ -268,13 +224,8 @@ contract('ERC827 Token', function (accounts) { const extraData = message.contract.fail.getData(); - const abiMethod = findMethod(token.abi, 'approve', 'address,uint256,bytes'); - const approveData = ethjsABI.encodeMethod(abiMethod, - [message.contract.address, 10, extraData] - ); - await token.sendTransaction( - { from: accounts[0], data: approveData } - ).should.be.rejectedWith(EVMRevert); + await token.approveAndCall(message.contract.address, 10, extraData) + .should.be.rejectedWith(EVMRevert); // approval should not have gone through so allowance is still 0 new BigNumber(0).should.be.bignumber @@ -286,13 +237,8 @@ contract('ERC827 Token', function (accounts) { const extraData = message.contract.fail.getData(); - const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes'); - const transferData = ethjsABI.encodeMethod(abiMethod, - [message.contract.address, 10, extraData] - ); - await token.sendTransaction( - { from: accounts[0], data: transferData } - ).should.be.rejectedWith(EVMRevert); + await token.transferAndCall(message.contract.address, 10, extraData) + .should.be.rejectedWith(EVMRevert); // transfer should not have gone through, so balance is still 0 new BigNumber(0).should.be.bignumber @@ -305,14 +251,8 @@ contract('ERC827 Token', function (accounts) { const extraData = message.contract.fail.getData(); await token.approve(accounts[1], 10, { from: accounts[2] }); - - const abiMethod = findMethod(token.abi, 'transferFrom', 'address,address,uint256,bytes'); - const transferFromData = ethjsABI.encodeMethod(abiMethod, - [accounts[2], message.contract.address, 10, extraData] - ); - await token.sendTransaction( - { from: accounts[1], data: transferFromData } - ).should.be.rejectedWith(EVMRevert); + await token.transferFromAndCall(accounts[2], message.contract.address, 10, extraData, { from: accounts[1] }) + .should.be.rejectedWith(EVMRevert); // transferFrom should have failed so balance is still 0 but allowance is 10 new BigNumber(10).should.be.bignumber @@ -328,13 +268,8 @@ contract('ERC827 Token', function (accounts) { web3.toHex(123456), 666, 'Transfer Done' ); - const abiMethod = findMethod(token.abi, 'approve', 'address,uint256,bytes'); - const approveData = ethjsABI.encodeMethod(abiMethod, - [token.contract.address, 100, extraData] - ); - await token.sendTransaction( - { from: accounts[0], data: approveData } - ).should.be.rejectedWith(EVMRevert); + await token.approveAndCall(token.contract.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 () { @@ -344,13 +279,8 @@ contract('ERC827 Token', function (accounts) { web3.toHex(123456), 666, 'Transfer Done' ); - const abiMethod = findMethod(token.abi, 'transfer', 'address,uint256,bytes'); - const transferData = ethjsABI.encodeMethod(abiMethod, - [token.contract.address, 100, extraData] - ); - await token.sendTransaction( - { from: accounts[0], data: transferData } - ).should.be.rejectedWith(EVMRevert); + await token.transferAndCall(token.contract.address, 100, extraData) + .should.be.rejectedWith(EVMRevert); }); it('should fail transferFrom (with data) when using token contract address as receiver', async function () { @@ -362,13 +292,8 @@ contract('ERC827 Token', function (accounts) { await token.approve(accounts[1], 1, { from: accounts[0] }); - const abiMethod = findMethod(token.abi, 'transferFrom', 'address,address,uint256,bytes'); - const transferFromData = ethjsABI.encodeMethod(abiMethod, - [accounts[0], token.contract.address, 1, extraData] - ); - await token.sendTransaction( - { from: accounts[1], data: transferFromData } - ).should.be.rejectedWith(EVMRevert); + await token.transferFromAndCall(accounts[0], token.contract.address, 1, extraData, { from: accounts[1] }) + .should.be.rejectedWith(EVMRevert); }); }); });