How to add max supply to a mintable ERC20 token on Polygon chain

Good afternoon,

I am having some trouble figuring out how to add a maximum supply to a mintable token for a yield farm.

I am using Remix.

The token is forked from Gatorswap

you can set up a variable with the maximum supply and check on mint if current supply + mint is <= max supply?

another way is to premint total supply and reward from masterchef transfers instead of mints.

1 Like

Good morning,

I am rather new to coding and im still struggling with some stuff,

Could you be so kind to paste me the code necessary to implement the maximum supply with the mint function?

Masterchef is calling the mint from the token adress, so that function should be added in to token contract or the masterchef?

Do you mean you want to a capped token? you can have a look at this token:
openzeppelin-contracts/ERC20Capped.sol | OpenZeppelin/openzeppelin-contracts

1 Like

yea thats a good code for cap.

While using [ERC20Capped](https://docs.openzeppelin.com/contracts/4.x/api/token/erc20#ERC20Capped), I'm facing an override issue. Contract is below:

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

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";

contract XactToken is ERC20, Ownable, ERC20Capped, ERC20Permit {
    constructor() ERC20("XactToken", "XACT") ERC20Capped(10000000000  * 10 ** decimals()) ERC20Permit("XactToken") {
        _mint(msg.sender, 100000000 * 10 ** decimals());
    }

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}```

...and the compiler error is as follows:

Derived contract must override function "_mint". Two or more base classes define function with same name and parameter types.


I can see that the `_mint` function of `ERC20Capped` is already overriding that of `ERC20`. But, I'm wondering why getting this error.

I added:

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

to the contract generated by:

But I'm definitely missing something. Where do I define the cap?

1 Like

Hey @carboncoffee,

The thread is a bit old, so I'm answering but feel free to open another support request if someone has a different question.

The ERC20Capped receives the cap during construction, and should be used like:

contract Token is ERC20, ERC20Capped {
  constructor(string memory name, string memory symbol, uint256 cap) ERC20(name, symbol) ERC20Capped(cap) {}

  /// @dev For testing purposes. Note that access control is missing here and anyone can mint infinite tokens with this function setup !!!
  function mint(address account, uint256 amount) public {
      _mint(account, amount);
  }

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

Make sure to fully understand how inheritance works, and always write tests to validate the behavior you expect.