Solidity

錯誤:處理事務時出現 VM 異常:恢復 SafeMath:遷移眾籌合約時減法溢出

  • July 18, 2019

我正在使用 openzepplin-solidity 2.3 版開發代幣銷售 Dapp。

遷移時,我收到以下錯誤。

返回錯誤:處理事務時出現 VM 異常:恢復 SafeMath:減法溢出 – 給出的原因:SafeMath:減法溢出

我在用

Truffle v5.0.22 (core:

5.0.22) Solidity - 0.5.0 (solc-js)

Node v10.16.0

Web3.js v1.0.0-beta.37

是我在 Github 上的 Dapp 的連結。

我有 2 個眾籌合約和 1 個代幣合約。

SimpleToken.sol

pragma solidity 0.5.0;

import "../client/node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";
import "../client/node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

contract SimpleToken is ERC20Detailed, ERC20 {
   uint256 public totalSupply_;
   mapping(address => uint256) balances;

  constructor(string memory _name, string memory _symbol, uint8 _decimals,   uint256 _amount)
  ERC20Detailed(_name, _symbol, _decimals)
  public {
   require(_amount > 0, "Amount has to be greater then 0");
   totalSupply_ = _amount * (10 ** 18);
   balances[msg.sender] = totalSupply_;
   emit Transfer(address(0), msg.sender, totalSupply_);
   }

}

以下是

PresaleCrowdsale.sol

pragma solidity 0.5.0;

import "../client/node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "../client/node_modules/openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol";
import "../client/node_modules/openzeppelin-solidity/contracts/crowdsale/validation/WhitelistCrowdsale.sol";
import "../client/node_modules/openzeppelin-solidity/contracts/crowdsale/emission/AllowanceCrowdsale.sol";

contract PresaleCrowdsale is WhitelistCrowdsale, AllowanceCrowdsale{
   constructor(uint256 _rate, address payable _wallet, ERC20 _token, address _tokenWallet)
   Crowdsale(_rate, _wallet, _token)
   AllowanceCrowdsale(_tokenWallet)
   public {  }
}

我可以成功遷移代幣合約,但是在遷移眾籌合約時,我收到了safeMath 減法溢出錯誤

以下是遷移文件

const SimpleToken = artifacts.require("SimpleToken.sol");
const PresaleCrowdsale = artifacts.require("PresaleCrowdsale.sol");

 module.exports = (deployer, network, [owner]) => deployer
.then(() => deployer.deploy(PresaleCrowdsale, 10000, owner, SimpleToken.address, owner))
.then(() => SimpleToken.deployed())
.then(token => token.transfer(PresaleCrowdsale.address, '100000000000000000'));

編輯

添加第二個遷移文件並且傳輸不起作用。得到同樣的錯誤

恢復 SafeMath:減法溢出 – 給出的原因:SafeMath:減法溢出

const SimpleToken = artifacts.require("SimpleToken");
const GenericCrowdsale = artifacts.require("GenericCrowdsale");

module.exports =  async (deployer, network, [owner]) => {
await deployer.deploy(SimpleToken, "Tooploox", "TPX", 18, 21000000);

const now = Math.floor(Date.now() / 1000);

const day = 24 * 60 * 60;

const openingtime = now;

const closingtime = openingtime + 2 * day;

const rate = 1000;

await deployer.deploy(GenericCrowdsale, openingtime, closingtime, rate, owner, SimpleToken.address);

console.log("Transfering Token...");

const token = await SimpleToken.deployed();

await token.transfer(GenericCrowdsale.address, "20000000000000000000000000", {from: owner});
};

請幫我

您的 ERC20 合約實施存在問題。

__balances,_totalSupply是繼承的 ERC20 合約中的私有變數

適當的實施應該是

contract SimpleToken is ERC20Detailed, ERC20 {
   constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _amount)
   ERC20Detailed(_name, _symbol, _decimals)
   public {
       require(_amount > 0, "Amount has to be greater then 0");
       uint256 initialSupply = _amount.mul(10 ** uint256(_decimals));
       _mint(msg.sender, initialSupply);
   }
}

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