0

I am developing the token sale Dapp using openzepplin-solidity version 2.3.

While migrating, I got the following error.

Returned error: VM Exception while processing transaction: revert SafeMath: subtraction overflow -- Reason given: SafeMath: subtraction overflow

I am using

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

Here is the link of my Dapp on Github.
I have 2 crowdsale contracts and 1 token contract.

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

}

And following is

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 {  }
}

I can successfully migrating the token contract but while migrating the crowdsale contract I got the error in safeMath subtraction overflow

Following is the migration file

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

EDIT
Add 2nd Migration file and transfer is not working. got the same error

revert SafeMath: subtraction overflow -- Reason given: SafeMath: subtraction overflow

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

Please help me

  • my guess is you're transferring more tokens than available, thus the subtraction overflow – Micky Socaci Jul 16 at 8:25
0

You have an issue in your ERC20 contract implementation.

__balances, _totalSupply are private variables in the inherited ERC20 contract

The appropriate implementation should be

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

  • It's not working please read my edit in question – urjit on rails Jul 17 at 13:20
  • hey, yeah I have updated my answer – Samparsky Jul 18 at 9:44
  • Its working, Thanks Is this the new change in the openzeppelin 2.3? – urjit on rails Jul 18 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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