|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +import {IERC6909} from "../../interfaces/draft-IERC6909.sol"; |
| 6 | +import {Context} from "../../utils/Context.sol"; |
| 7 | +import {IERC165, ERC165} from "../../utils/introspection/ERC165.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @dev Implementation of ERC-6909. |
| 11 | + * See https://eips.ethereum.org/EIPS/eip-6909 |
| 12 | + */ |
| 13 | +contract ERC6909 is Context, ERC165, IERC6909 { |
| 14 | + mapping(address owner => mapping(uint256 id => uint256)) private _balances; |
| 15 | + |
| 16 | + mapping(address owner => mapping(address operator => bool)) private _operatorApprovals; |
| 17 | + |
| 18 | + mapping(address owner => mapping(address spender => mapping(uint256 id => uint256))) private _allowances; |
| 19 | + |
| 20 | + error ERC6909InsufficientBalance(address sender, uint256 balance, uint256 needed, uint256 id); |
| 21 | + error ERC6909InsufficientAllowance(address spender, uint256 allowance, uint256 needed, uint256 id); |
| 22 | + error ERC6909InvalidApprover(address approver); |
| 23 | + error ERC6909InvalidReceiver(address receiver); |
| 24 | + error ERC6909InvalidSender(address sender); |
| 25 | + error ERC6909InvalidSpender(address spender); |
| 26 | + |
| 27 | + /// @inheritdoc IERC165 |
| 28 | + function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) { |
| 29 | + return interfaceId == type(IERC6909).interfaceId || super.supportsInterface(interfaceId); |
| 30 | + } |
| 31 | + |
| 32 | + /// @inheritdoc IERC6909 |
| 33 | + function balanceOf(address owner, uint256 id) public view virtual override returns (uint256) { |
| 34 | + return _balances[owner][id]; |
| 35 | + } |
| 36 | + |
| 37 | + /// @inheritdoc IERC6909 |
| 38 | + function allowance(address owner, address spender, uint256 id) public view virtual override returns (uint256) { |
| 39 | + return _allowances[owner][spender][id]; |
| 40 | + } |
| 41 | + |
| 42 | + /// @inheritdoc IERC6909 |
| 43 | + function isOperator(address owner, address spender) public view virtual override returns (bool) { |
| 44 | + return _operatorApprovals[owner][spender]; |
| 45 | + } |
| 46 | + |
| 47 | + /// @inheritdoc IERC6909 |
| 48 | + function approve(address spender, uint256 id, uint256 amount) public virtual override returns (bool) { |
| 49 | + _approve(_msgSender(), spender, id, amount); |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + /// @inheritdoc IERC6909 |
| 54 | + function setOperator(address spender, bool approved) public virtual override returns (bool) { |
| 55 | + _setOperator(_msgSender(), spender, approved); |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + /// @inheritdoc IERC6909 |
| 60 | + function transfer(address receiver, uint256 id, uint256 amount) public virtual override returns (bool) { |
| 61 | + _transfer(_msgSender(), receiver, id, amount); |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + /// @inheritdoc IERC6909 |
| 66 | + function transferFrom( |
| 67 | + address sender, |
| 68 | + address receiver, |
| 69 | + uint256 id, |
| 70 | + uint256 amount |
| 71 | + ) public virtual override returns (bool) { |
| 72 | + address caller = _msgSender(); |
| 73 | + if (sender != caller && !isOperator(sender, caller)) { |
| 74 | + _spendAllowance(sender, caller, id, amount); |
| 75 | + } |
| 76 | + _transfer(sender, receiver, id, amount); |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @dev Creates `amount` of token `id` and assigns them to `account`, by transferring it from address(0). |
| 82 | + * Relies on the `_update` mechanism |
| 83 | + * |
| 84 | + * Emits a {Transfer} event with `from` set to the zero address. |
| 85 | + * |
| 86 | + * NOTE: This function is not virtual, {_update} should be overridden instead. |
| 87 | + */ |
| 88 | + function _mint(address to, uint256 id, uint256 amount) internal { |
| 89 | + if (to == address(0)) { |
| 90 | + revert ERC6909InvalidReceiver(address(0)); |
| 91 | + } |
| 92 | + _update(address(0), to, id, amount); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @dev Moves `amount` of token `id` from `from` to `to` without checking for approvals. |
| 97 | + * |
| 98 | + * This internal function is equivalent to {transfer}, and can be used to |
| 99 | + * e.g. implement automatic token fees, slashing mechanisms, etc. |
| 100 | + * |
| 101 | + * Emits a {Transfer} event. |
| 102 | + * |
| 103 | + * NOTE: This function is not virtual, {_update} should be overridden instead. |
| 104 | + */ |
| 105 | + function _transfer(address from, address to, uint256 id, uint256 amount) internal { |
| 106 | + if (from == address(0)) { |
| 107 | + revert ERC6909InvalidSender(address(0)); |
| 108 | + } |
| 109 | + if (to == address(0)) { |
| 110 | + revert ERC6909InvalidReceiver(address(0)); |
| 111 | + } |
| 112 | + _update(from, to, id, amount); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @dev Destroys a `amount` of token `id` from `account`. |
| 117 | + * Relies on the `_update` mechanism. |
| 118 | + * |
| 119 | + * Emits a {Transfer} event with `to` set to the zero address. |
| 120 | + * |
| 121 | + * NOTE: This function is not virtual, {_update} should be overridden instead |
| 122 | + */ |
| 123 | + function _burn(address from, uint256 id, uint256 amount) internal { |
| 124 | + if (from == address(0)) { |
| 125 | + revert ERC6909InvalidSender(address(0)); |
| 126 | + } |
| 127 | + _update(from, address(0), id, amount); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @dev Transfers `amount` of token `id` from `from` to `to`, or alternatively mints (or burns) if `from` |
| 132 | + * (or `to`) is the zero address. All customizations to transfers, mints, and burns should be done by overriding |
| 133 | + * this function. |
| 134 | + * |
| 135 | + * Emits a {Transfer} event. |
| 136 | + */ |
| 137 | + function _update(address from, address to, uint256 id, uint256 amount) internal virtual { |
| 138 | + address caller = _msgSender(); |
| 139 | + |
| 140 | + if (from != address(0)) { |
| 141 | + uint256 fromBalance = _balances[from][id]; |
| 142 | + if (fromBalance < amount) { |
| 143 | + revert ERC6909InsufficientBalance(from, fromBalance, amount, id); |
| 144 | + } |
| 145 | + unchecked { |
| 146 | + // Overflow not possible: amount <= fromBalance. |
| 147 | + _balances[from][id] = fromBalance - amount; |
| 148 | + } |
| 149 | + } |
| 150 | + if (to != address(0)) { |
| 151 | + _balances[to][id] += amount; |
| 152 | + } |
| 153 | + |
| 154 | + emit Transfer(caller, from, to, id, amount); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @dev Sets `amount` as the allowance of `spender` over the `owner`'s `id` tokens. |
| 159 | + * |
| 160 | + * This internal function is equivalent to `approve`, and can be used to e.g. set automatic allowances for certain |
| 161 | + * subsystems, etc. |
| 162 | + * |
| 163 | + * Emits an {Approval} event. |
| 164 | + * |
| 165 | + * Requirements: |
| 166 | + * |
| 167 | + * - `owner` cannot be the zero address. |
| 168 | + * - `spender` cannot be the zero address. |
| 169 | + */ |
| 170 | + function _approve(address owner, address spender, uint256 id, uint256 amount) internal virtual { |
| 171 | + if (owner == address(0)) { |
| 172 | + revert ERC6909InvalidApprover(address(0)); |
| 173 | + } |
| 174 | + if (spender == address(0)) { |
| 175 | + revert ERC6909InvalidSpender(address(0)); |
| 176 | + } |
| 177 | + _allowances[owner][spender][id] = amount; |
| 178 | + emit Approval(owner, spender, id, amount); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @dev Approve `spender` to operate on all of `owner`'s tokens |
| 183 | + * |
| 184 | + * This internal function is equivalent to `setOperator`, and can be used to e.g. set automatic allowances for |
| 185 | + * certain subsystems, etc. |
| 186 | + * |
| 187 | + * Emits an {OperatorSet} event. |
| 188 | + * |
| 189 | + * Requirements: |
| 190 | + * |
| 191 | + * - `owner` cannot be the zero address. |
| 192 | + * - `spender` cannot be the zero address. |
| 193 | + */ |
| 194 | + function _setOperator(address owner, address spender, bool approved) internal virtual { |
| 195 | + if (owner == address(0)) { |
| 196 | + revert ERC6909InvalidApprover(address(0)); |
| 197 | + } |
| 198 | + if (spender == address(0)) { |
| 199 | + revert ERC6909InvalidSpender(address(0)); |
| 200 | + } |
| 201 | + _operatorApprovals[owner][spender] = approved; |
| 202 | + emit OperatorSet(owner, spender, approved); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @dev Updates `owner`'s allowance for `spender` based on spent `amount`. |
| 207 | + * |
| 208 | + * Does not update the allowance value in case of infinite allowance. |
| 209 | + * Revert if not enough allowance is available. |
| 210 | + * |
| 211 | + * Does not emit an {Approval} event. |
| 212 | + */ |
| 213 | + function _spendAllowance(address owner, address spender, uint256 id, uint256 amount) internal virtual { |
| 214 | + uint256 currentAllowance = allowance(owner, spender, id); |
| 215 | + if (currentAllowance < type(uint256).max) { |
| 216 | + if (currentAllowance < amount) { |
| 217 | + revert ERC6909InsufficientAllowance(spender, currentAllowance, amount, id); |
| 218 | + } |
| 219 | + unchecked { |
| 220 | + _allowances[owner][spender][id] = currentAllowance - amount; |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments