|
| 1 | +pragma solidity ^0.8.20; |
| 2 | + |
| 3 | +contract MintAllowanceUpgradeable { |
| 4 | + struct MintAllowanceStorage { |
| 5 | + mapping(address => uint256) _mintAllowance; |
| 6 | + uint256 _maxMintAllowance; |
| 7 | + } |
| 8 | + |
| 9 | + //keccak256("Monerium.MintAllowanceStorage") |
| 10 | + bytes32 private constant MintAllowanceStorageLocation = |
| 11 | + 0xb337526095403ef89c7becef1792605e55dadf16cfa1d0df874fad9581a6937d; |
| 12 | + |
| 13 | + function _getMintAllowanceStorage() |
| 14 | + private |
| 15 | + pure |
| 16 | + returns (MintAllowanceStorage storage $) |
| 17 | + { |
| 18 | + assembly { |
| 19 | + $.slot := MintAllowanceStorageLocation |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @dev Emitted when allowance is set. |
| 25 | + * @param account The address of the account. |
| 26 | + * @param amount The amount of allowance. |
| 27 | + */ |
| 28 | + event MintAllowance(address indexed account, uint256 amount); |
| 29 | + |
| 30 | + /** |
| 31 | + * @dev Emitted when max allowance is set. |
| 32 | + * @param amount The amount of allowance. |
| 33 | + */ |
| 34 | + event MaxMintAllowance(uint256 amount); |
| 35 | + |
| 36 | + modifier onlyAllowedMinter(address account, uint256 amount) { |
| 37 | + MintAllowanceStorage storage s = _getMintAllowanceStorage(); |
| 38 | + require( |
| 39 | + s._mintAllowance[account] >= amount, |
| 40 | + "MintAllowance: not allowed to mint more than allowed" |
| 41 | + ); |
| 42 | + _; |
| 43 | + } |
| 44 | + |
| 45 | + function getMintAllowance(address account) public view returns (uint256) { |
| 46 | + MintAllowanceStorage storage s = _getMintAllowanceStorage(); |
| 47 | + return s._mintAllowance[account]; |
| 48 | + } |
| 49 | + |
| 50 | + function _setMintAllowance(address account, uint256 amount) internal { |
| 51 | + MintAllowanceStorage storage s = _getMintAllowanceStorage(); |
| 52 | + |
| 53 | + require( |
| 54 | + amount <= s._maxMintAllowance, |
| 55 | + "MintAllowance: cannot set allowance higher than max" |
| 56 | + ); |
| 57 | + |
| 58 | + s._mintAllowance[account] = amount; |
| 59 | + |
| 60 | + emit MintAllowance(account, amount); |
| 61 | + } |
| 62 | + |
| 63 | + function _useMintAllowance(address account, uint256 amount) internal { |
| 64 | + MintAllowanceStorage storage s = _getMintAllowanceStorage(); |
| 65 | + |
| 66 | + require( |
| 67 | + s._mintAllowance[account] >= amount, |
| 68 | + "MintAllowance: not allowed to mint more than allowed" |
| 69 | + ); |
| 70 | + |
| 71 | + s._mintAllowance[account] -= amount; |
| 72 | + |
| 73 | + emit MintAllowance(account, s._mintAllowance[account]); |
| 74 | + } |
| 75 | + |
| 76 | + function getMaxMintAllowance() public view returns (uint256) { |
| 77 | + MintAllowanceStorage storage s = _getMintAllowanceStorage(); |
| 78 | + return s._maxMintAllowance; |
| 79 | + } |
| 80 | + |
| 81 | + function _setMaxMintAllowance(uint256 amount) internal { |
| 82 | + MintAllowanceStorage storage s = _getMintAllowanceStorage(); |
| 83 | + |
| 84 | + s._maxMintAllowance = amount; |
| 85 | + |
| 86 | + emit MaxMintAllowance(amount); |
| 87 | + } |
| 88 | +} |
0 commit comments