Testing contracts

I’m writing tests for my smart-contract, mostly everything work as expected. However, there’s one thing that does not and I can’t understand why (I’m far from being a javascript expert, so please bear with me if I’m doing something stupid)

:1234: Code to reproduce

// This is a tracker on the default account
let senderTracker = await balance.tracker(defaultSender, "ether");

// later
console.log(`*** Before payment: ${await senderTracker.get() }`);

//call the payWinners function in my contract, it should also send a fee to default sender account
let sharedPot = await forecast.payWinners([accounts[2]]);
let fee = await senderTracker.delta();

console.log(`***  is BN: ${ Web3.utils.isBN(fee) }`);
console.log(`***  After payment: ${ await senderTracker.get() }`);
console.log(`***            Fee: ${ Web3.utils.fromWei(fee, "milliether") }`);

And in the console I get:

Pay Winners
*** Before payment: 99818042788
*** is BN: true
*** After payment: 99818484256
1) should pay the winner

6 passing (1s)
1 failing

  1. Pay Winners
    should pay the winner:
    Error: [number-to-bn] while converting number “3fffffff81afa7c” to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.

If the isBN() function returns true, how is it possible that I get this error saying that the value should be BN?

This may be a web3.js bug. As a workaround, you can use fee.toString():

Web3.utils.fromWei(fee.toString(), "milliether")

Hi, just make a test, it looks all work well.

const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');

let number = web3.utils.toBN("1000000000000000000"); // 1e18
console.log("number is: ", number);
console.log("number is bignumber:", web3.utils.isBN(number));

let readableNumber = web3.utils.fromWei(number, "milliether");
console.log("human readable number is:", readableNumber);

result is:

image

1 Like

This may be happening due to a mismatch of the BN version used by Test Helpers and Web3.js.

This shouldn’t be happening. @AlexS If you want to share your repository I can take a look at why that would be happening. It could be caused by a complex interaction between dependencies so it might be hard to reproduce in isolation without the full list of dependencies.

I found what the problem was:

When I defined the tracker, I used “ether” as units.
let senderTracker = await balance.tracker(defaultSender, "ether");

So then I tried to use fromWei(), with a number that was in ether. As soon as I changed the tracker units to “wei”, everything worked as expected.
Thanks for your help.