Browse Source

Updated sendTransaction, added tests. (#1528)

* Renamed sendTransaction to send, improved API.

* Added send tests.

* Now using promisified web3
Nicolás Venturo 6 years ago
parent
commit
c0bda4db88

+ 19 - 0
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);
+    }
+}

+ 7 - 11
test/helpers/sendTransaction.js → 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,
 };

+ 3 - 3
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));
   });
 });

+ 70 - 0
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]));
+    });
+  });
+});

+ 4 - 4
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);

+ 2 - 2
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',

+ 2 - 2
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',