I created a token contract using the OpenZepplin Wizard and would like to introduce a cap using this abstract contract.
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
The full contract is below. When I deploy, I'm able to mint as many tokens as I want. I would greatly appreciate any tips or links to examples, suggestions.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
// How do I integrated this:
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
/// @custom:security-contact me@example.com
contract MyTokenC is ERC20, ERC20Burnable, Pausable, Ownable, ERC20Permit {
constructor() ERC20("MyToken-C", "MTKC") ERC20Permit("MyToken-C") {}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override
{
super._beforeTokenTransfer(from, to, amount);
}
}
Environment
Polygon
Remix
OpenZepplin Wizard
I believe I figured it out, but any comments suggestions would be appreciated. The current draft is:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
contract MyTokenC is ERC20, ERC20Burnable, Pausable, Ownable, ERC20Permit, ERC20Capped {
constructor() ERC20("MyToken-C", "MTKC") ERC20Permit("MyToken-C") ERC20Capped(1000) {}
function pause() public onlyOwner {
_pause();
}
function unpause() public onlyOwner {
_unpause();
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
function _mint(address account, uint256 amount) internal override(ERC20, ERC20Capped) {
require(totalSupply() + amount <= cap(), "ERC20Capped: cap exceeded");
super._mint(account, amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount)
internal
whenNotPaused
override
{
super._beforeTokenTransfer(from, to, amount);
}
}
Hey @carboncoffee,
Seems like you've figured it out. However, you're re implementing the _mint
function, and although it works, you can use super.mint()
with the original ERC20Capped
, you can see how this plays out by referring to the Solidity Inheritance docs
The ERC20Capped receives the cap
during construction, and should be used like:
contract Token is ERC20, ..., ERC20Capped {
function _mint(address account, uint256 amount) internal override(ERC20Capped, ERC20) {
super._mint(account, amount);
}
}
Quick tested with the example I shared in the other thread and the cap work as expected.
Best
Thank you sincerely for getting back.
I deployed the contract, but the Mint function I implemented doesn't work on the main Polygon chain. When I use mint 1,000,000 the wallet balance shows as 0. ( I added the token to Metamask.) If I look at the OpenZepplin balance call it shows the balance as 1,000,000.
When I use the testnet the mint works fine. Any idea of what I did wrong?
Mint with error:
https://polygonscan.com/tx/0xf97015d40361a151741d02e1ab8a9209a89ef591063db4fc61ed996b25da7fdf
The contract is:
0xCA47A0513fe65314d018Ae3d4c5c786A8B04aB52
https://polygonscan.com/token/0xca47a0513fe65314d018ae3d4c5c786a8b04ab52?a=0x040B7eD99550906E879C86b3930bb8033D4568bf