|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol"; |
| 5 | +import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; |
| 6 | +import {IRule} from "../../../interfaces/IRule.sol"; |
| 7 | +import {RuleInterfaceId} from "../../../modules/library/RuleInterfaceId.sol"; |
| 8 | +import {RuleMintAllowanceInvariantStorage} from "./abstract/RuleMintAllowanceInvariantStorage.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @title RuleMintAllowance |
| 12 | + * @notice Rule that enforces per-minter mint allowances set by the contract admin. |
| 13 | + * The admin grants each minter address a maximum amount they may mint in total. |
| 14 | + * Each mint deducts from the minter's remaining allowance. |
| 15 | + * Burns and regular transfers are unrestricted by this rule. |
| 16 | + */ |
| 17 | +contract RuleMintAllowance is AccessControl, RuleMintAllowanceInvariantStorage, IRule { |
| 18 | + bytes4 private constant RULE_ENGINE_INTERFACE_ID = 0x20c49ce7; |
| 19 | + bytes4 private constant ERC1404EXTEND_INTERFACE_ID = 0x78a8de7d; |
| 20 | + |
| 21 | + mapping(address minter => uint256 allowance) public mintAllowance; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param admin Address granted DEFAULT_ADMIN_ROLE |
| 25 | + */ |
| 26 | + constructor(address admin) { |
| 27 | + require(admin != address(0), "RuleMintAllowance: zero admin"); |
| 28 | + _grantRole(DEFAULT_ADMIN_ROLE, admin); |
| 29 | + } |
| 30 | + |
| 31 | + /* ============ ERC-165 ============ */ |
| 32 | + |
| 33 | + function supportsInterface(bytes4 interfaceId) public view virtual override(AccessControl, IERC165) returns (bool) { |
| 34 | + return interfaceId == RULE_ENGINE_INTERFACE_ID || interfaceId == ERC1404EXTEND_INTERFACE_ID |
| 35 | + || interfaceId == RuleInterfaceId.IRULE_INTERFACE_ID || AccessControl.supportsInterface(interfaceId); |
| 36 | + } |
| 37 | + |
| 38 | + /* ============ Admin ============ */ |
| 39 | + |
| 40 | + /** |
| 41 | + * @notice Set the mint allowance for a minter. |
| 42 | + * @param minter Address of the minter. |
| 43 | + * @param amount Maximum amount the minter is allowed to mint. |
| 44 | + */ |
| 45 | + function setMintAllowance(address minter, uint256 amount) external onlyRole(DEFAULT_ADMIN_ROLE) { |
| 46 | + mintAllowance[minter] = amount; |
| 47 | + emit MintAllowanceSet(minter, amount); |
| 48 | + } |
| 49 | + |
| 50 | + /* ============ IRule — state-changing ============ */ |
| 51 | + |
| 52 | + /** |
| 53 | + * @notice Called for transfers where no spender context is available. |
| 54 | + * Mint allowance cannot be enforced without a spender; passes through. |
| 55 | + */ |
| 56 | + function transferred(address from, address to, uint256 value) public view { |
| 57 | + // no-op: spender unknown, enforcement requires transferred(spender,...) |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @notice Called for every token operation (transfer, mint, burn) with spender context. |
| 62 | + * Deducts from the minter's allowance for mints; passes through for burns and transfers. |
| 63 | + */ |
| 64 | + function transferred(address spender, address from, address /* to */, uint256 value) public { |
| 65 | + if (from == address(0)) { |
| 66 | + uint256 allowance = mintAllowance[spender]; |
| 67 | + if (allowance < value) { |
| 68 | + revert RuleMintAllowance_InsufficientAllowance(spender, allowance, value); |
| 69 | + } |
| 70 | + mintAllowance[spender] = allowance - value; |
| 71 | + emit MintAllowanceConsumed(spender, value, mintAllowance[spender]); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /* ============ IRule — view ============ */ |
| 76 | + |
| 77 | + /** |
| 78 | + * @notice Returns TRANSFER_OK; without spender context mint allowance cannot be evaluated. |
| 79 | + */ |
| 80 | + function detectTransferRestriction(address, address, uint256) public pure override returns (uint8) { |
| 81 | + return uint8(REJECTED_CODE_BASE.TRANSFER_OK); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @notice Returns CODE_MINTER_INSUFFICIENT_ALLOWANCE when a minter's allowance would be |
| 86 | + * exceeded. Burns and regular transfers always return TRANSFER_OK. |
| 87 | + */ |
| 88 | + function detectTransferRestrictionFrom(address spender, address from, address, uint256 value) |
| 89 | + public |
| 90 | + view |
| 91 | + override |
| 92 | + returns (uint8) |
| 93 | + { |
| 94 | + if (from == address(0) && mintAllowance[spender] < value) { |
| 95 | + return CODE_MINTER_INSUFFICIENT_ALLOWANCE; |
| 96 | + } |
| 97 | + return uint8(REJECTED_CODE_BASE.TRANSFER_OK); |
| 98 | + } |
| 99 | + |
| 100 | + function canTransfer(address from, address to, uint256 value) public pure override returns (bool) { |
| 101 | + return detectTransferRestriction(from, to, value) == uint8(REJECTED_CODE_BASE.TRANSFER_OK); |
| 102 | + } |
| 103 | + |
| 104 | + function canTransferFrom(address spender, address from, address to, uint256 value) |
| 105 | + public |
| 106 | + view |
| 107 | + override |
| 108 | + returns (bool) |
| 109 | + { |
| 110 | + return detectTransferRestrictionFrom(spender, from, to, value) == uint8(REJECTED_CODE_BASE.TRANSFER_OK); |
| 111 | + } |
| 112 | + |
| 113 | + function canReturnTransferRestrictionCode(uint8 restrictionCode) external pure override returns (bool) { |
| 114 | + return restrictionCode == CODE_MINTER_INSUFFICIENT_ALLOWANCE; |
| 115 | + } |
| 116 | + |
| 117 | + function messageForTransferRestriction(uint8 restrictionCode) external pure override returns (string memory) { |
| 118 | + if (restrictionCode == CODE_MINTER_INSUFFICIENT_ALLOWANCE) { |
| 119 | + return TEXT_MINTER_INSUFFICIENT_ALLOWANCE; |
| 120 | + } |
| 121 | + return TEXT_CODE_NOT_FOUND; |
| 122 | + } |
| 123 | +} |
0 commit comments