Error in test: Number can only safely store up to 53 bits

Can anyone hellp with this:

  1. Contract: simpletoken
    should assert true:
    Error: Number can only safely store up to 53 bits
1 Like

Hi @crypto_economics,

Can you give some more context on where this is happening? What testing tool are you running, what is in your test etc.

Are you able to share an example contract and test so that I can reproduce?

@abcoathup, thank you for getting back to me! Pls see the code for the SimpleToken and Test below:

// SPDX-License-Identifier: MIT
   pragma solidity ^0.6.0;

   import "../openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";

   /**
    * @title SimpleToken
    * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
    * Note they can later distribute these tokens as they wish using `transfer` and other
    * `ERC20` functions.
    */
   contract SimpleToken is ERC20 {
       uint8 public constant DECIMALS = 18;
       uint256 public constant INITIAL_SUPPLY = 100000 * (10 ** uint256(DECIMALS));

       /**
        * @dev Constructor that gives msg.sender all of existing tokens.
        */
       constructor () public ERC20 ("SimpleToken", "SIM") {
           _mint(msg.sender, INITIAL_SUPPLY);
       }
   }

//TEST

    var Token = artifacts.require("SimpleToken");contract('simpletoken', function(accounts) {
      it("should assert true", function() {
        var token;
        return Token.deployed().then(function(instance){
         token = instance;
         return token.totalSupply.call();
        }).then(function(result){
         assert.equal(result.toNumber(), 1000000, 'total supply is wrong');
        })
      });
    });
1 Like

Maybe you can have a try to call token.totalSupply().call(), besides this, you can have a check about this file: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/test/token/ERC20/ERC20.test.js

1 Like

Hi @crypto_economics,

Looking at your test you are trying to convert a BigNumber object to a JavaScript number which is too large for a JavaScript number hence why you get the error Number can only safely store up to 53 bits.

OpenZeppelin Test Helpers include bn.js that we can use in our tests.

I have created an example: Simple ERC20 token example which includes a check on the totalSupply (which is uint256 in Solidity).

1 Like

this still fails @abcoathup ,

contract >>

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract XcelPay is ERC20,Ownable {
    constructor(uint256 initialSupply) ERC20("XcelPay", "XCEL") public {
        _mint(msg.sender, initialSupply);       
    }
    
    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

testcase :

  it('Mints new Token ', async () => {
    const instace = await token.new(100000);  

    const mint_value = new BN("1000000000000000000");
    await instace.mint(accounts[0], mint_value);

  });
 Error: Number can only safely store up to 53 bits