dhj
dhj

Reputation: 4805

Contract returns totalSupply from Solidity as BigNumber but need to validate it without the decimals

Solidity returns totalSupply() as 1 trillion + the 9 decimals as 1trillion concat with 9 zeroes.

So when I try to do a mocha test to check the supply is 1T, it fails because the number has 9 extra zeroes at the end and no decimal point.

So how to change

BigNumber { value: "1000000000000000000000" } to 1000000000000 so my test passes.

This is my test which fails;

        it('Should correctly set totalSupply to: 1T', async () => {
            const totalSupply = await hardhatToken.totalSupply();
            var tokenBalance = totalSupply.toString();
            console.log(tokenBalance);
            console.log(ethers.BigNumber.from(totalSupply, 9));
            expect(totalSupply).should.be.bignumber.equal(1000000000000);
        });

I have the BN.js library but I cant work out the process! I'd like to do it properly without just chopping off the last 9 digits as there will be other tests to write with similar issues.

Upvotes: 1

Views: 1283

Answers (1)

Abdullah Jan
Abdullah Jan

Reputation: 86

I write my test in a way that I calculate decimals separately and In assertion, I concatenate decimals zeros with value.

it('Should correctly set totalSupply to: 1T', async () => {
    const totalSupply = await hardhatToken.totalSupply();
    const decimals = ethers.BigNumber.from(10).pow(9);

    expect(totalSupply).to.equal(
        ethers.BigNumber.from(1_000_000_000_000).mul(decimals)
    );
});

Upvotes: 2

Related Questions