From c0bda4db884ac026c31701a0d9aade9e7342cff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Sat, 1 Dec 2018 02:47:32 -0300 Subject: [PATCH] Updated sendTransaction, added tests. (#1528) * Renamed sendTransaction to send, improved API. * Added send tests. * Now using promisified web3 --- contracts/mocks/Acknowledger.sol | 19 +++++ test/helpers/{sendTransaction.js => send.js} | 18 ++--- test/helpers/test/balanceDifference.test.js | 6 +- test/helpers/test/send.test.js | 70 +++++++++++++++++++ test/payment/PaymentSplitter.test.js | 8 +-- test/token/ERC721/ERC721.behavior.js | 4 +- .../ERC721/ERC721PausedToken.behavior.js | 4 +- 7 files changed, 107 insertions(+), 22 deletions(-) create mode 100644 contracts/mocks/Acknowledger.sol rename test/helpers/{sendTransaction.js => send.js} (67%) create mode 100644 test/helpers/test/send.test.js diff --git a/contracts/mocks/Acknowledger.sol b/contracts/mocks/Acknowledger.sol new file mode 100644 index 000000000..ff8f1ae04 --- /dev/null +++ b/contracts/mocks/Acknowledger.sol @@ -0,0 +1,19 @@ +pragma solidity ^0.4.24; + +contract Acknowledger { + event AcknowledgeFoo(uint256 a); + event AcknowledgeBarSingle(uint256 a); + event AcknowledgeBarDouble(uint256 a, uint256 b); + + function foo(uint256 a) public { + emit AcknowledgeFoo(a); + } + + function bar(uint256 a) public { + emit AcknowledgeBarSingle(a); + } + + function bar(uint256 a, uint256 b) public { + emit AcknowledgeBarDouble(a, b); + } +} diff --git a/test/helpers/sendTransaction.js b/test/helpers/send.js similarity index 67% rename from test/helpers/sendTransaction.js rename to test/helpers/send.js index 9d4f3068f..a01fd6bcf 100644 --- a/test/helpers/sendTransaction.js +++ b/test/helpers/send.js @@ -1,4 +1,5 @@ const ethjsABI = require('ethjs-abi'); +const { ethSendTransaction } = require('./web3'); function findMethod (abi, name, args) { for (let i = 0; i < abi.length; i++) { @@ -9,22 +10,17 @@ function findMethod (abi, name, args) { } } -function sendTransaction (target, name, argsTypes, argsValues, opts) { +async function transaction (target, name, argsTypes, argsValues, opts) { const abiMethod = findMethod(target.abi, name, argsTypes); const encodedData = ethjsABI.encodeMethod(abiMethod, argsValues); return target.sendTransaction(Object.assign({ data: encodedData }, opts)); } -function sendEther (from, to, value) { - web3.eth.sendTransaction({ - from: from, - to: to, - value: value, - gasPrice: 0, - }); +function ether (from, to, value) { + return ethSendTransaction({ from, to, value, gasPrice: 0 }); } + module.exports = { - findMethod, - sendTransaction, - sendEther, + ether, + transaction, }; diff --git a/test/helpers/test/balanceDifference.test.js b/test/helpers/test/balanceDifference.test.js index e4eddc7f5..98846db9d 100644 --- a/test/helpers/test/balanceDifference.test.js +++ b/test/helpers/test/balanceDifference.test.js @@ -1,5 +1,5 @@ const { balanceDifference } = require('../balanceDifference'); -const { sendEther } = require('../sendTransaction'); +const send = require('../send'); const { ether } = require('../ether'); const BigNumber = web3.BigNumber; @@ -10,13 +10,13 @@ require('chai') contract('balanceDifference', function ([sender, receiver]) { it('returns balance increments', async function () { (await balanceDifference(receiver, () => - sendEther(sender, receiver, ether(1))) + send.ether(sender, receiver, ether(1))) ).should.be.bignumber.equal(ether(1)); }); it('returns balance decrements', async function () { (await balanceDifference(sender, () => - sendEther(sender, receiver, ether(1))) + send.ether(sender, receiver, ether(1))) ).should.be.bignumber.equal(ether(-1)); }); }); diff --git a/test/helpers/test/send.test.js b/test/helpers/test/send.test.js new file mode 100644 index 000000000..c868626a1 --- /dev/null +++ b/test/helpers/test/send.test.js @@ -0,0 +1,70 @@ +const send = require('../send'); +const shouldFail = require('../shouldFail'); +const expectEvent = require('../expectEvent'); +const { ether } = require('../ether'); +const { ethGetBalance } = require('../web3'); + +const Acknowledger = artifacts.require('Acknowledger'); + +const BigNumber = web3.BigNumber; +require('chai') + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('send', function ([sender, receiver]) { + describe('ether', function () { + it('sends ether with no gas cost', async function () { + const value = ether(1); + + const initialSenderBalance = await ethGetBalance(sender); + const initialReceiverBalance = await ethGetBalance(receiver); + + await send.ether(sender, receiver, value); + + const finalSenderBalance = await ethGetBalance(sender); + const finalReceiverBalance = await ethGetBalance(receiver); + + finalSenderBalance.sub(initialSenderBalance).should.be.bignumber.equal(-value); + finalReceiverBalance.sub(initialReceiverBalance).should.be.bignumber.equal(value); + }); + + it('throws if the sender balance is insufficient', async function () { + const value = (await ethGetBalance(sender)).plus(1); + + await shouldFail(send.ether(sender, receiver, value)); + }); + }); + + describe('transaction', function () { + beforeEach(async function () { + this.acknowledger = await Acknowledger.new(); + }); + + it('calls a function from its signature ', async function () { + const { logs } = await send.transaction(this.acknowledger, 'foo', 'uint256', [3]); + expectEvent.inLogs(logs, 'AcknowledgeFoo', { a: 3 }); + }); + + it('calls overloaded functions with less arguments', async function () { + const { logs } = await send.transaction(this.acknowledger, 'bar', 'uint256', [3]); + expectEvent.inLogs(logs, 'AcknowledgeBarSingle', { a: 3 }); + }); + + it('calls overloaded functions with more arguments', async function () { + const { logs } = await send.transaction(this.acknowledger, 'bar', 'uint256,uint256', [3, 5]); + expectEvent.inLogs(logs, 'AcknowledgeBarDouble', { a: 3, b: 5 }); + }); + + it('throws if the number of arguments does not match', async function () { + await shouldFail(send.transaction(this.acknowledger, 'foo', 'uint256, uint256', [3, 5])); + }); + + it('throws if the method does not exist', async function () { + await shouldFail(send.transaction(this.acknowledger, 'baz', 'uint256', [3])); + }); + + it('throws if there is a mismatch in the number of types and values', async function () { + await shouldFail(send.transaction(this.acknowledger, 'foo', 'uint256', [3, 3])); + }); + }); +}); diff --git a/test/payment/PaymentSplitter.test.js b/test/payment/PaymentSplitter.test.js index 60e5654dc..34404536e 100644 --- a/test/payment/PaymentSplitter.test.js +++ b/test/payment/PaymentSplitter.test.js @@ -1,6 +1,6 @@ const { ethGetBalance } = require('../helpers/web3'); const expectEvent = require('../helpers/expectEvent'); -const { sendEther } = require('./../helpers/sendTransaction'); +const send = require('./../helpers/send'); const { ether } = require('../helpers/ether'); const { ZERO_ADDRESS } = require('./../helpers/constants'); @@ -60,7 +60,7 @@ contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpaye }); it('should accept payments', async function () { - await sendEther(owner, this.contract.address, amount); + await send.ether(owner, this.contract.address, amount); (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(amount); }); @@ -78,12 +78,12 @@ contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpaye }); it('should throw if non-payee want to claim', async function () { - await sendEther(payer1, this.contract.address, amount); + await send.ether(payer1, this.contract.address, amount); await shouldFail.reverting(this.contract.release(nonpayee1)); }); it('should distribute funds to payees', async function () { - await sendEther(payer1, this.contract.address, amount); + await send.ether(payer1, this.contract.address, amount); // receive funds const initBalance = await ethGetBalance(this.contract.address); diff --git a/test/token/ERC721/ERC721.behavior.js b/test/token/ERC721/ERC721.behavior.js index de58710bf..d350c3f6c 100644 --- a/test/token/ERC721/ERC721.behavior.js +++ b/test/token/ERC721/ERC721.behavior.js @@ -2,7 +2,7 @@ const expectEvent = require('../../helpers/expectEvent'); const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior'); const shouldFail = require('../../helpers/shouldFail'); const { ZERO_ADDRESS } = require('../../helpers/constants'); -const { sendTransaction } = require('../../helpers/sendTransaction'); +const send = require('../../helpers/send'); const ERC721ReceiverMock = artifacts.require('ERC721ReceiverMock.sol'); const BigNumber = web3.BigNumber; @@ -217,7 +217,7 @@ function shouldBehaveLikeERC721 ( describe('via safeTransferFrom', function () { const safeTransferFromWithData = function (from, to, tokenId, opts) { - return sendTransaction( + return send.transaction( this.token, 'safeTransferFrom', 'address,address,uint256,bytes', diff --git a/test/token/ERC721/ERC721PausedToken.behavior.js b/test/token/ERC721/ERC721PausedToken.behavior.js index 1d83608fa..d44bc0565 100644 --- a/test/token/ERC721/ERC721PausedToken.behavior.js +++ b/test/token/ERC721/ERC721PausedToken.behavior.js @@ -1,5 +1,5 @@ const shouldFail = require('../../helpers/shouldFail'); -const { sendTransaction } = require('../../helpers/sendTransaction'); +const send = require('../../helpers/send'); const { ZERO_ADDRESS } = require('../../helpers/constants'); const BigNumber = web3.BigNumber; @@ -36,7 +36,7 @@ function shouldBehaveLikeERC721PausedToken (owner, [recipient, operator]) { it('reverts when trying to safeTransferFrom with data', async function () { await shouldFail.reverting( - sendTransaction( + send.transaction( this.token, 'safeTransferFrom', 'address,address,uint256,bytes',