Solidity

如何使用 truffle 控制台發送超過 10^15 個 ERC20 令牌?

  • November 8, 2021

我正在嘗試測試我的合約,但每次我嘗試發送超過 10^15 時都會收到溢出錯誤,而我知道我的錢包可以發送更多。有什麼解決辦法嗎?

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);
   }
}

^ 代幣合約程式碼。

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)'
}

^錯誤資訊和原始命令。

在為交易目的處理 truffle 控制台時,使用字元串值(如地址值),因為數字太大,JavaScript 無法處理。這個問題在openzeppelin論壇上討論過。

代替:

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

採用:

token.approve('0xB8b7E4982b71828Dc1FF9b7e3548CdfFaAD6Afbd', '100000000000000000')

引用自:https://ethereum.stackexchange.com/questions/112500