Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

349
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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)
    );
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda