0

I am trying to test my contract but everytime I try to send more than 10^15 I get an overflow error while I know my wallet can send more than that. Any solutions?

pragma solidity 0.8.0;

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

contract NGE is ERC20 {
    address public owner;
    mapping(address => bool) public haveAccess;
    constructor() ERC20("Neon", "NGE") {
        _mint(msg.sender, 1000000*10**16);
        owner = msg.sender;
    }

    function giveAccess(address to) public returns(bool){
        require(msg.sender == owner, "only the owner can give access to this contract");
        haveAccess[msg.sender] = true;
        return(true);
    }

    function minter(address to, uint256 amount) public{
        require(msg.sender == owner || haveAccess[msg.sender] == true);
        _mint(to, amount);
    }
}

^ token contract code.

truffle(bscTestnet)> token.approve('0xB8b7E4982b71828Dc1FF9b7e3548CdfFaAD6Afbd', 10**16)
Uncaught:
Error: overflow (fault="overflow", operation="BigNumber.from", value=10000000000000000, code=NUMERIC_FAULT, version=bignumber/5.0.8)
    at evalmachine.<anonymous>:0:7
    at sigintHandlersWrap (vm.js:273:12)
    at Script.runInContext (vm.js:142:14)
    at runScript (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:270:1)
    at Console.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:285:1)
    at bound (domain.js:421:15)
    at REPLServer.runBound [as eval] (domain.js:432:12)
    at REPLServer.onLine (repl.js:909:10)
    at REPLServer.emit (events.js:400:28)
    at REPLServer.emit (domain.js:475:12)
    at REPLServer.Interface._onLine (readline.js:434:10)
    at REPLServer.Interface._line (readline.js:791:8)
    at REPLServer.Interface._ttyWrite (readline.js:1136:14)
    at REPLServer.self._ttyWrite (repl.js:1004:9)
    at ReadStream.onkeypress (readline.js:265:10)
    at ReadStream.emit (events.js:400:28)
    at ReadStream.emit (domain.js:475:12)
    at emitKeys (internal/readline/utils.js:358:14)
    at emitKeys.next (<anonymous>)
    at ReadStream.onData (readline.js:1270:36) {
  reason: 'overflow',
  code: 'NUMERIC_FAULT',
  fault: 'overflow',
  operation: 'BigNumber.from',
  value: 10000000000000000,
  hijackedStack: 'Error: overflow (fault="overflow", operation="BigNumber.from", value=10000000000000000, code=NUMERIC_FAULT, version=bignumber/5.0.8)\n' +
    '    at Logger.makeError (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/logger/lib.esm/index.js:166:1)\n' +
    '    at Logger.throwError (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/logger/lib.esm/index.js:175:1)\n' +
    '    at throwFault (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js:273:1)\n' +
    '    at Function.from (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js:178:1)\n' +
    '    at NumberCoder.encode (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/number.js:13:17)\n' +
    '    at /usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/array.js:41:1\n' +
    '    at Array.forEach (<anonymous>)\n' +
    '    at pack (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/array.js:27:1)\n' +
    '    at TupleCoder.encode (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/coders/tuple.js:19:16)\n' +
    '    at AbiCoder.encode (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/@ethersproject/abi/lib.esm/abi-coder.js:82:1)\n' +
    '    at ABICoder.encodeParameters (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth-abi/src/index.js:145:1)\n' +
    '    at /usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:531:1\n' +
    '    at Array.map (<anonymous>)\n' +
    '    at Object._encodeMethodABI (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/src/index.js:530:10)\n' +
    '    at /usr/local/lib/node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:181:1\n' +
    '    at runMicrotasks (<anonymous>)\n' +
    '    at processTicksAndRejections (internal/process/task_queues.js:95:5)'
}

^error message and original command.

1 Answer 1

1

While dealing with truffle console for transactions purposes use string values like address values, because of number is too large for JavaScript to handle. This issue is discussed at openzeppelin forum.

instead of:

token.approve('0xB8b7E4982b71828Dc1FF9b7e3548CdfFaAD6Afbd', 10**16)

use:

token.approve('0xB8b7E4982b71828Dc1FF9b7e3548CdfFaAD6Afbd', '100000000000000000')
1
  • Or use the more readable form : '1'+'0'.repeat(16) or a const gwei variable in bignumber : const GWEI = BN.from('10').pow('18'); that you then multiply/divide as needed, in BN : const ten_ethers = GWEI.mul(10) Nov 8, 2021 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.