MyToken.sol mint() JavaScript "Error: overflow" on big numbers

I am testing MyToken.sol …
when call to mint( ) small amount (say 50000) of tokens it works fine, call to mint() a bigger amount it shows error like…
Uncaught: Error: overflow (fault=“overflow”, operation=“BigNumber.from”, value=555000000000000000000, code=NUMERIC_FAULT, version=bignumber/5.0.8) …

Please help , thanks in advance.

:computer: Environment

Openzeppelin 3.x example MyToken.sol

Truffle v5.1.53 (core: 5.1.53)
Solidity - ^0.6.2 (solc-js)
Node v14.15.1
Web3.js v1.2.9

:memo:Details

truffle(goerli)> token.mint(accounts[2],555000000000000000000,{from:accounts[2]})

error message below…

Uncaught:
Error: overflow (fault=“overflow”, operation=“BigNumber.from”, value=555000000000000000000, code=NUMERIC_FAULT, version=bignumber/5.0.8) at evalmachine.:0:7
at sigintHandlersWrap (vm.js:272:15)
at Script.runInContext (vm.js:139:14)
at runScript (/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:251:1)
at Console.interpret (/usr/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:266:1)
at bound (domain.js:430:14)
at REPLServer.runBound [as eval] (domain.js:443:12)
at REPLServer.onLine (repl.js:817:10)
at REPLServer.emit (events.js:315:20)
at REPLServer.EventEmitter.emit (domain.js:486:12)
at REPLServer.Interface._onLine (readline.js:337:10)
at REPLServer.Interface._line (readline.js:666:8)
at REPLServer.Interface._ttyWrite (readline.js:1010:14)
at REPLServer.self._ttyWrite (repl.js:907:9)
at ReadStream.onkeypress (readline.js:213:10)
at ReadStream.emit (events.js:315:20)
at ReadStream.EventEmitter.emit (domain.js:486:12)
at emitKeys (internal/readline/utils.js:345:14)
at emitKeys.next ()
at ReadStream.onData (readline.js:1144:36)
at ReadStream.emit (events.js:315:20)
at ReadStream.EventEmitter.emit (domain.js:486:12)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:284:9)
at ReadStream.Readable.push (_stream_readable.js:223:10)
at TTY.onStreamRead (internal/stream_base_commons.js:188:23)
at TTY.callbackTrampoline (internal/async_hooks.js:129:14) {
reason: ‘overflow’,
code: ‘NUMERIC_FAULT’,
fault: ‘overflow’,
operation: ‘BigNumber.from’,
value: 555000000000000000000,
hijackedStack: ‘Error: overflow (fault=“overflow”, operation=“BigNumber.from”, value=555000000000000000000, code=NUMERIC_FAULT, version=bignumber/5.0.8)\n’ +
’ at lib_esm_Logger.makeError (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/logger/lib.esm/index.js:166:1)\n’ +
’ at lib_esm_Logger.throwError (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/logger/lib.esm/index.js:175:1)\n’ +
’ at throwFault (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js:273:1)\n’ +
’ at Function.from (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js:178:1)\n’ +
’ at number_NumberCoder.encode (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/number.js:13:26)\n’ +
’ at /usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/array.js:41:1\n’ +
’ at Array.forEach ()\n’ +
’ at pack (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/array.js:27:1)\n’ +
’ at tuple_TupleCoder.encode (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/tuple.js:19:16)\n’ +
’ at abi_coder_AbiCoder.encode (/usr/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/abi-coder.js:82:1)\n’ +
’ at ABICoder.encodeParameters (/usr/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth-abi/src/index.js:145:1)\n’ +
’ at /usr/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:531:1\n’ +
’ at Array.map ()\n’ +
’ at Object._encodeMethodABI (/usr/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:530:10)\n’ +
’ at /usr/lib/node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:181:1\n’ +
’ at runMicrotasks ()\n’ +
’ at processTicksAndRejections (internal/process/task_queues.js:93:5)’
}

:1234: Code to reproduce

openzeppelin 3.x MyToken.sol

// contracts/MyToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import “@openzeppelin/contracts/access/AccessControl.sol”;
import “@openzeppelin/contracts/token/ERC20/ERC20.sol”;

contract MyToken is ERC20, AccessControl {
// Create a new role identifier for the minter role
bytes32 public constant MINTER_ROLE = keccak256(“MINTER_ROLE”);
bytes32 public constant BURNER_ROLE = keccak256(“BURNER_ROLE”);

constructor() public ERC20("MyToken", "TKN") {
    // Grant the contract deployer the default admin role: it will be able
    // to grant and revoke any roles
    //_setupRole(bytes32 role, address account)
    _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

function mint(address to, uint256 amount) public {
    require(hasRole(MINTER_ROLE, msg.sender), "Caller is not a minter");
    _mint(to, amount);
}

function burn(address from, uint256 amount) public {
    require(hasRole(BURNER_ROLE, msg.sender), "Caller is not a burner");
    _burn(from, amount);
}

}

1 Like

Hi @qoopme,

The number is too large for JavaScript to handle. You can use a Big Number library or enter as a string.

See: https://docs.openzeppelin.com/learn/deploying-and-interacting#querying-state and https://docs.ethers.io/v5/api/utils/bignumber/#BigNumber--notes

1 Like

It works with entering as a string , thank you.

I read the docs you posted but still don’t understand how to use Big Number. I should meet Big Number again on learning smart contract. Would you please show me the truffle console command of using Big Number , Thanks a lot.

You can use web3 to convert your numbers to Big number instance

token.mint(accounts[2], web3.utils.toBN("555000000000000000000"),{from:accounts[2]})
2 Likes

@abcoathup @DragonLord
Thanks! :grinning:

1 Like