Creating Capped ERC20 gives error in contract constructor

I am deploying my contract on Ropsten test net using Truffle
Here is my Contract Sol file code:

//SPDX-License-Identifier: MIT

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Capped.sol";

contract ToughToken is ERC20, ERC20Pausable, ERC20Burnable, ERC20Capped  {
constructor() ERC20("Tough Token", "TVT") ERC20Capped(1000000000 * (10 ** uint256(decimals()))) public{
     _mint(msg.sender, 1000000 * (10 ** uint256(decimals())));
}

function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override(ERC20, ERC20Pausable, ERC20Capped) {
    super._beforeTokenTransfer(from, to, amount);
}
}

truffle-config.js

ropsten: {
        provider: () => new HDWalletProvider(infuraKey, `https://ropsten.infura.io/v3/myid`),
        network_id: 3,       // Ropsten's id
        gas: 7000000,        // Ropsten has a lower block limit than mainnet
        confirmations: 2,    // # of confs to wait between deployments. (default: 0)
        timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
        skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
        },
compilers: {
    solc: {
      version: "0.6.2",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    },
  },
1 Like

Hi @hussammustafa,

In the constructor we need to hard code the decimals as the function returns zero at this point:

ERC20Capped(1000000000 * (10**uint256(18)))

We can see this error when deploying to a local testnet

$ npx truffle migrate --network development

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      1600150853731
> Block gas limit: 6721975 (0x6691b7)


2_deploy.js
===========

   Deploying 'ToughToken'
   ----------------------

Error:  *** Deployment Failed ***

"ToughToken" hit a require or revert statement with the following reason given:
   * ERC20Capped: cap exceeded

    at /home/abcoathup/projects/forum/hussammustafa/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:364:1
    at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.1.44 (core: 5.1.44)
Node v10.21.0

I

Updating your token, it could look as follows:

ToughToken.sol

//SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Pausable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Capped.sol";

contract ToughToken is ERC20, ERC20Pausable, ERC20Burnable, ERC20Capped {
    constructor()
        public
        ERC20("Tough Token", "TVT")
        ERC20Capped(1000000000 * (10**uint256(18)))
    {
        _mint(msg.sender, 1000000 * (10**uint256(decimals())));
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual override(ERC20, ERC20Pausable, ERC20Capped) {
        super._beforeTokenTransfer(from, to, amount);
    }
}

As an aside, with no functionality to mint there is no need for a cap in your current code. The cap is the amount minted in the constructor.

If you want to create a mintable token, I suggest looking at the preset ERC20PresetMinterPauser contract:

Thanks @abcoathup for detailed answer for my query.

1 Like

Hi @hussammustafa,

Please ask all the questions that you need.

I suggest looking at Points to consider when creating a fungible token (ERC20, ERC777)

It would be great if you had a moment to introduce yourself.

1 Like