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

Conversation

jbogacz
Copy link
Contributor

@jbogacz jbogacz commented Sep 20, 2018

  • switched direct logs array check to expectEvent method in AllowanceCrowdsale.test.js

🚀 Description

Condition for equality check in expectEvent.inLogs() method was changed to verify equality even compared objects are structs. It fixes issue where BigNumber emitted to event was not possible to validate as follow:

const event = expectEvent.inLogs(currentSnapshotId.logs, 'Snapshot', { id: new BigNumber(1) });

Now it works fine.

  • [ +] 📘 I've reviewed the OpenZeppelin Contributor Guidelines
  • [ +] ✅ I've added tests where applicable to test my new functionality.
  • [ +] 📖 I've made sure that my contracts are well-documented.
  • [ +] 🎨 I've run the JS/Solidity linters and fixed any issues (npm run lint:fix).

@nventuro nventuro added kind:improvement tests Test suite and helpers. labels Sep 20, 2018
Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

Thanks @jbogacz, this is great!

I have a couple questions:

  1. is there a way to make this work without having to explicitly pass a BigNumber? e.g. { id: 1 } instead of { id: new BigNumber(1) }

  2. Can this be done without using lodash? We're in the process of removing it from some test files (Remove lodash from tests #1323), though it may be fine to leave it here.

@nventuro nventuro self-assigned this Sep 20, 2018
@jbogacz jbogacz force-pushed the fix/expectEvent-bignumber-support-#1026 branch from 48fae63 to aff6533 Compare September 21, 2018 18:07
@jbogacz
Copy link
Contributor Author

jbogacz commented Sep 21, 2018

Hey, I've pushed my recent changes as follow:

  1. Right now you can pass variable as { id: 1 } and alternatively { id: new BigNumber(1) } too.

  2. lodash is removed now.

I will take care #1232 nextly.

Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

Looking great @jbogacz! This will be extremely helpful, thanks!

I left a minor comment we should address before merging.

@@ -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?

@jbogacz
Copy link
Contributor Author

jbogacz commented Sep 22, 2018

Hey @nventuro , please take a look my recent update. To be honest I'm not so experienced with JS and more advanced import technics are tricky for me. Please take a look into chai-bignumber.js library, there is already isBigNumber() function which I copied. I'm wondering if there is a way to reuse this function from chai-bignumber.js instead of copying it?

@jbogacz jbogacz force-pushed the fix/expectEvent-bignumber-support-#1026 branch from 2d79786 to 9058931 Compare September 22, 2018 10:56
@jbogacz
Copy link
Contributor Author

jbogacz commented Sep 24, 2018

Hi, @nventuro I've created PR #1343 which introduces this fix and applies usage to truffle tests.

Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

This is great @jbogacz, thanks a lot! I think copying chai-bignumber's code is fine for now, since I don't see a way to call it from the outside, and we can always improve the helper later if needed :)

@nventuro nventuro merged commit 947de54 into OpenZeppelin:master Sep 26, 2018
@jbogacz
Copy link
Contributor Author

jbogacz commented Sep 27, 2018

Cool! Thanks!

@jbogacz jbogacz deleted the fix/expectEvent-bignumber-support-#1026 branch September 27, 2018 04:42
come-maiz pushed a commit that referenced this pull request Oct 21, 2018
* Add BigNumber support to expectEvent/inLogs (#1026)

* switched direct logs array check to expectEvent method in AllowanceCrowdsale.test.js

* Refactor expectEvent.inLogs function to use simple value number check

* Introduced should.be.bignumber method to compare BigNumber values

* Destructure transaction object to extract logs field

(cherry picked from commit 947de54)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Test suite and helpers.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants