Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BigNumber support to expectEvent/inLogs (#1026) #1338

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions test/crowdsale/AllowanceCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const expectEvent = require('../helpers/expectEvent');
const { ether } = require('../helpers/ether');
const { assertRevert } = require('../helpers/assertRevert');
const { ethGetBalance } = require('../helpers/web3');

const BigNumber = web3.BigNumber;

const should = require('chai')
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();

Expand Down Expand Up @@ -40,13 +41,15 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW

describe('high-level purchase', function () {
it('should log purchase', async function () {
const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
const event = logs.find(e => e.event === 'TokensPurchased');
should.exist(event);
event.args.purchaser.should.equal(investor);
event.args.beneficiary.should.equal(investor);
event.args.value.should.be.bignumber.equal(value);
event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
const transaction = await this.crowdsale.sendTransaction({ value: value, from: investor });
expectEvent.inLogs(
transaction.logs,
'TokensPurchased',
{
purchaser: investor,
beneficiary: investor,
value: value,
amount: expectedTokenAmount });
});

it('should assign tokens to sender', async function () {
Expand Down
12 changes: 11 additions & 1 deletion test/helpers/expectEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function inLogs (logs, eventName, eventArgs = {}) {
let matches = true;

for (const [k, v] of Object.entries(eventArgs)) {
if (e.args[k] !== v) {
if (toSimpleValue(e.args[k]) !== toSimpleValue(v)) {
matches = false;
}
}
Expand All @@ -27,6 +27,16 @@ async function inTransaction (tx, eventName, eventArgs = {}) {
return inLogs(logs, eventName, eventArgs);
}

function toSimpleValue (value) {
return isBigNumber(value)
? value.toNumber()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may cause issues for large numbers, which JavaScript won't be able to properly represent. What if instead of converting to JS number types, we instead used should.equal and should.be.bignumber.equal respectively?

: value;
}

function isBigNumber (value) {
return value.constructor.name === 'BigNumber';
}

module.exports = {
inLogs,
inTransaction,
Expand Down