Updated sendTransaction, added tests. (#1528)

* Renamed sendTransaction to send, improved API.

* Added send tests.

* Now using promisified web3
This commit is contained in:
Nicolás Venturo
2018-12-01 02:47:32 -03:00
committed by GitHub
parent 88e8c7c94b
commit c0bda4db88
7 changed files with 107 additions and 22 deletions

View File

@ -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,
};

View File

@ -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));
});
});

View File

@ -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]));
});
});
});