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

153
Visualizações
How to do basic arithmetic in Truffle testing using web3.js

I'm currently using Truffle to test smart contracts, but having difficulty with basic arithmetics.

If I try to add two numbers:

const firstNumber = web3.utils.toWei('1', 'ether'); // 1000000000000000000
const sum = firstNumber + 100

The result is like when two strings are attached 1000000000000000000100.

I tried converting firstNumber.toString() or using const { toBN } = web3.utils; but all have the same result.

I tried using add or mul like some examples here, but my Truffle simply says those methods don't exist.

I'm using Truffle v5.3.14 (core: 5.3.14) and Web3.js v1.4.0.

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

0

'1' is a string so you will receive string from .toWei. You can convert String to BN and use .add. For example:

const firstNumber = web3.utils.toWei('1', 'ether'); // 1000000000000000000
const sum = web3.utils.toBN(firstNumber).add(web3.utils.toBN('100')).toString();
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