Help Regarding ERC20Capped extension

Hello,

I used wizard to create my token. I customized the decimal and everything as per my need. However, I was unable to implement ERC20Capped.sol in my contract. I read several topics here and documentation on-site, but there seems to be some problem from my side. So can someone provide a guide for the dummy to implement it? I want to cap my minting max to 5.3 million and the initial token amount to be 10k.

Here I am attaching the code of my token. Rest are libraries of openzeppeline so I guess I don't have to add em.

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

import "@openzeppelin/contracts@4.3.2/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@4.3.2/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts@4.3.2/access/AccessControl.sol";
import "@openzeppelin/contracts@4.3.2/token/ERC20/extensions/draft-ERC20Permit.sol";
import "@openzeppelin/contracts@4.3.2/token/ERC20/extensions/ERC20Votes.sol";


    contract GodToken is ERC20, ERC20Burnable, AccessControl, ERC20Permit, ERC20Votes {
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

    constructor() ERC20("God Token", "GDTN") ERC20Permit("God Token") {
        _mint(msg.sender, 10000 * 10 ** decimals());
        _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _setupRole(MINTER_ROLE, msg.sender);
    }

    function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
        _mint(to, amount);
    }
    

    // The following functions are overrides required by Solidity.

    function _afterTokenTransfer(address from, address to, uint256 amount)
        internal
        override(ERC20, ERC20Votes)
    {
        super._afterTokenTransfer(from, to, amount);
    }

    function _mint(address to, uint256 amount)
        internal
        override(ERC20, ERC20Votes)
    {
        super._mint(to, amount);
    }

    function _burn(address account, uint256 amount)
        internal
        override(ERC20, ERC20Votes)
    {
        super._burn(account, amount);
    }
}

1 Like

You can implement your own version of a cap by only allowing mint to work if the total supply of the token is less than 5.3mil tokens.

I understand what you are saying but I don't have much coding background. Can you show an example?

replace your _mint function with this

function _mint(address to, uint256 amount)
    internal
    override(ERC20, ERC20Votes)
{
    require(totalSupply() + amount <= 5300000 * 10 ** decimals(), "Max number of tokens minted");
    super._mint(to, amount);
}

Damn. That worked like charm. Thanks a lot, man. Now all I have to add is fee functionality to the token. Can you help me with that? For every transaction, 0.9% of fees should be sent to the dev's account. Also, this code can be added to the liquidity pool right? Coz the last contract I made had an issue with liquidity. I wasn't able to add liquidity to it.

1 Like

I could but I would advise against fee on transfer tokens as it breaks many smart contracts (uniswap v2, v3 etc.)

Ohh okay. Thanks a lot for the help. Much appreciated.

1 Like

Thank you very much

AYM

Hello [STYJ], It worked for me too but It will be more helpful for me if you please explain why you use decimals() here,(Help Regarding ERC20Capped extension - #4 by STYJ) require(totalSupply() + amount <= 5300000 * 10 ** decimals(). Complete noob !

Hi Faizan, you may have already figured this one out, but for other's sake, if you look at the ERC20 contract, the function decimals does the following:

function decimals() public view virtual override returns (uint8) {
        return 18;
    }

Therefore the require statement that you cited is:

Total supply + amount needs to be smaller than or equal to 5300000 * 10 **18; 18 being the number of decimals set for the token.

Thank you for the response. I understand it.