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

BN-Chai assertions fail #501

Closed
LayneHaber opened this issue Apr 30, 2019 · 7 comments
Closed

BN-Chai assertions fail #501

LayneHaber opened this issue Apr 30, 2019 · 7 comments
Labels
discussion Questions, feedback and general information.

Comments

@LayneHaber
Copy link

Switching over to use the native BigNumber library from this package, and even though it wraps the bn.ts, it seems that it doesn't work with the bn-chai assertions.

My guess is it has something to do with the casting that happens in the constructor in your package, but not natively in the bn package (though I'm really not sure).

@ricmoo
Copy link
Member

ricmoo commented Apr 30, 2019

Oh, the BN.js objects are only used internally during actual maths operations. Outside of that, the BigNumber only keeps a normalized hex string representation.

You should be able to add any methods you need to the BigNumber prototype yourself though. What method are you trying to use?

@LayneHaber
Copy link
Author

I was using the equal assert (more or less) like this in the tests:

const should = require("chai");
const BN = require("bn.js");

should
  .use(require("chai-as-promised"))
  .use(require("bn-chai")(BN))
  .should();


describe("eq", () => {
  it("should work", () => {
    const test1 = ethers.utils.bigNumberify("2")
    const test2 = ethers.utils.bigNumberify("2")
    test1.should.be.eq.BN(test2)
  })
})

@ricmoo
Copy link
Member

ricmoo commented May 6, 2019

Wow... So, Chai looks insane, and I don't know that API or syntax at all. :p

Is it easy to write plugins? Once I get v5 up, I would fully welcome a plugin for Chai with ethers, but that code above most certainly will not work, as the BN is not accessible in any sense like that. For now you will need to use something more akin to: assert(test1.eq(test2)), which is what I assume that .should.be.eq is doing? Out of curiosity, what is the advantage of that over the assert?

@ricmoo ricmoo added the discussion Questions, feedback and general information. label May 6, 2019
@LayneHaber
Copy link
Author

I'll throw it on the list, and if it becomes enough of an add-on for me, I'll let you know if I get around to it.

I've never written a plugin for chai, but I don't think its too hard. And I agree! I was surprised this worked as well, but these tests were passing before making the switch to ethers :). As far as I understand it, the only real advantage of the should syntax is readability.

Using the more traditional assert. is what we switched to (and we stopped using the bn-chai ), so its not mission critical.

@ricmoo
Copy link
Member

ricmoo commented May 24, 2019

Awesome possum!

I'm going to close this now, but if you ever get around to opening a PR for this, feel free to add it as an ancillary package for v5. I'll get docs up soon on how to do that; once I figure out the best way to prevent the namespace from becoming too cluttered. Depending on how it works, it may also be a candidate for the @ethersproject/tests package.

Thanks! :)

@ricmoo ricmoo closed this as completed May 24, 2019
@wighawag
Copy link

wighawag commented Sep 5, 2019

Hey, was just looking at issues related to BN as I am used to that lib.
Would like to know why ethers.js decided to wrap bn.js and not use it directly ?

@ricmoo
Copy link
Member

ricmoo commented Sep 5, 2019

@wighawag Heya! It's a very common question, feel free to peruse the issues (just search for BigNumber) as many of them cover various reasons.

Here is one from a while ago that hits most of the bullet points, I believe: #312 (comment)

tl;dr: immutability, control over used/exposed functionality (to simplify testing coverage and facilitate swapping out the underlying implementation if needed, or if switching to another ECC library) and in general I try to keep external dependencies un-exposed, so it is easier/possible to safeguard against certain attack vectors.

I'll be including a larger explanation in the v5 documentation. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

3 participants