Web3js – Fixing Error: Cannot Find Module ‘bignumber.js’ in Solidity 0.5.x

solidity-0.5.xweb3js

I'm trying to test this smart contract but I can't seem to get the beyond this error, I'm using the latest truffle suite v5.1.16, and web3 v1.2.6.

KTokenSale.test.js

const BigNumber = web3.BigNumber;

require('chai')
  .use(require('chai-bignumber')(BigNumber))
  .should();

  const KToken = artifacts.require('KToken');
  const KTokenCrowdsale = artifacts.require('KTokenCrowdsale');

  contract('KTokenCrowdsale', function([_, wallet, investor1, investor2]) {
    beforeEach(async function () {
        this.name = 'TestToken';
        this.symbol = 'TT';
        this.decimals = 16;

        this.token = await KToken.new(
            this.name,
            this.symbol,
            this.decimals
            );

        this.rate = 850;
        this.wallet = wallet;

        this.crowdsale = await KToken.new(
            this.rate,
            this.wallet,
            this.token.address
            );
    });
  });

  contract('crowdsale', function() {
    it('tracks the rate', async function() {
        const rate = await this.crowdsale.rate();
        rate.should.be.bignumber.equal(this.rate);
    });

    it('tracks the wallet', async function() {
        const wallet = await this.crowdsale.wallet();
        wallet.should.equal(this.wallet);
    });

    it('tracks the token', async function() {
        const token = await this.crowdsale.token();
        token.should.equal(this.token.address);
    });
  });

Smart contract code being tested:

pragma solidity ^0.5.11;

import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";
import "@openzeppelin/contracts/crowdsale/validation/CappedCrowdsale.sol";

contract KTokenCrowdsale is Crowdsale, CappedCrowdsale {
    uint256 public investorMinCap = 7000000000000000000;
    uint256 public investorMaxCap = 350000000000000000000;
    mapping(address => uint256) public contributions;

    constructor(
        uint256 _rate,
        address payable _wallet,
        IERC20 _token,
        uint256 _cap
    )
    Crowdsale(_rate, _wallet, _token)
    CappedCrowdsale(_cap) public {}
}

Best Answer

It's web3.BigNumber on web3.js v0.x, and web3.utils.BN on web3.js v1.x.

Note that bignumber.js handles non-integer values, which means that it can:

  1. Take non-integer input (either as Number type or as String type)
  2. Generate non-integer results (via functions 'div' and 'dividedBy')

This, while bn.js doesn't handle non-integer values, as clearly stated:

Note: decimals are not supported in this library.

Since Solidity doesn't support non-integer values, web3.js dev-team has ultimately decided to replace BigNumber with BN instead of truncating every non-integer value which a web3 user passes to a web3 function (thus leaving the user unaware of potential errors in his/her code).

Related Topic