I wanted to create an inflationary token using ERC20Capped
but ran into issues because I can not access _cap
... any suggestions ?
https://github.com/OpenZeppelin/openzeppelin-contracts/issues/4459
That capped contract code is maybe 20 lines of code, you could just copy it and modify it to your needs
2 Likes
This is a bit of an oxymoron - either it's inflationary, or it's capped.
Namely, the idea about _cap
is that it is set once (upon deployment) and remains constant forever, serving as a guarantee that it never inflates beyond a certain point.
If it's not constant (for example, if it can be reconfigured by an admin), then this feature becomes worthless, and token holders cannot rely on that guarantee.
I found a solution and there is a (for example) a 2% inflation/year "set in stone" and nobody can change it after deployment.
opened 01:29PM - 14 Jul 23 UTC
closed 02:38PM - 14 Jul 23 UTC
I would like to create a token which has 2% inflation per year, based on `ERC20C… apped`
I would still use the `_cap` which is initialzed in `ERC20Capped` but would then override `cap()`
Surprisingly, i can not access `_cap` to do the math.
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
// import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Capped.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
contract COIN is ERC20Capped, AccessControl {
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
uint256 public constant SECONDS_PER_YEAR = 36525 days / 100;
uint256 public constant INFLATION = 2000; // 2.000 %
uint256 public immutable DEPLOYMENT_TIME;
constructor() ERC20("inflationary COIN", "COIN") ERC20Capped(100000000) {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(MINTER_ROLE, msg.sender);
DEPLOYMENT_TIME = block.timestamp;
}
/**
* @dev Returns the cap on the token's total supply
* @dev ERROR : _cap not accessible here ! - TODO
*/
function cap() public view override returns (uint256) {
return _cap + _cap * (block.timestamp - DEPLOYMENT_TIME) * INFLATION / SECONDS_PER_YEAR / 100 / 1000;
}
function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
_mint(to, amount);
}
}
```