Skip to content

Commit 026f0c4

Browse files
committed
implemented ERC-1155 version
1 parent 4a0a32c commit 026f0c4

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.20;
3+
4+
import {Module} from "../../../Module.sol";
5+
import {Role} from "../../../Role.sol";
6+
7+
import {BeforeApproveForAllCallback} from "../../../callback/BeforeApproveForAllCallback.sol";
8+
9+
import {BeforeBatchTransferCallbackERC1155} from "../../../callback/BeforeBatchTransferCallbackERC1155.sol";
10+
import {BeforeTransferCallbackERC1155} from "../../../callback/BeforeTransferCallbackERC1155.sol";
11+
import {OperatorAllowlistEnforced} from "@imtbl/contracts/allowlist/OperatorAllowlistEnforced.sol";
12+
13+
library ImmutableAllowlistStorage {
14+
15+
/// @custom:storage-location erc7201:token.immutableallowlist
16+
bytes32 public constant IMMUTABLE_ALLOWLIST_STORAGE_POSITION =
17+
keccak256(abi.encode(uint256(keccak256("token.immutableAllowlist.ERC1155")) - 1)) & ~bytes32(uint256(0xff));
18+
19+
struct Data {
20+
address operatorAllowlistRegistry;
21+
}
22+
23+
function data() internal pure returns (Data storage data_) {
24+
bytes32 position = IMMUTABLE_ALLOWLIST_STORAGE_POSITION;
25+
assembly {
26+
data_.slot := position
27+
}
28+
}
29+
30+
}
31+
32+
contract ImmutableAllowlistERC1155 is
33+
Module,
34+
BeforeApproveForAllCallback,
35+
BeforeTransferCallbackERC1155,
36+
BeforeBatchTransferCallbackERC1155,
37+
OperatorAllowlistEnforced
38+
{
39+
40+
/*//////////////////////////////////////////////////////////////
41+
ERRORS
42+
//////////////////////////////////////////////////////////////*/
43+
44+
/// @notice Emitted when an unauthorized approval is attempted.
45+
error OperatorAllowlistUnauthorizedApproval(address operator);
46+
47+
/// @notice Emitted when an unauthorized transfer is attempted.
48+
error OperatorAllowlistUnauthorizedTransfer(address from, address to, address operator);
49+
50+
/// @notice Emitted when the operator allowlist is not set.
51+
error OperatorAllowlistNotSet();
52+
53+
/*//////////////////////////////////////////////////////////////
54+
MODULE CONFIG
55+
//////////////////////////////////////////////////////////////*/
56+
57+
/// @notice Returns all implemented callback and module functions.
58+
function getModuleConfig() external pure override returns (ModuleConfig memory config) {
59+
config.callbackFunctions = new CallbackFunction[](3);
60+
config.fallbackFunctions = new FallbackFunction[](2);
61+
62+
config.callbackFunctions[1] = CallbackFunction(this.beforeApproveForAll.selector);
63+
config.callbackFunctions[2] = CallbackFunction(this.beforeTransferERC1155.selector);
64+
config.callbackFunctions[3] = CallbackFunction(this.beforeBatchTransferERC1155.selector);
65+
66+
config.fallbackFunctions[0] =
67+
FallbackFunction({selector: this.setOperatorAllowlistRegistry.selector, permissionBits: Role._MANAGER_ROLE});
68+
config.fallbackFunctions[1] =
69+
FallbackFunction({selector: this.getOperatorAllowlistRegistry.selector, permissionBits: 0});
70+
71+
config.requiredInterfaces = new bytes4[](1);
72+
config.requiredInterfaces[0] = 0x80ac58cd; // ERC1155
73+
74+
config.registerInstallationCallback = true;
75+
}
76+
77+
/*//////////////////////////////////////////////////////////////
78+
MODIFIERS
79+
//////////////////////////////////////////////////////////////*/
80+
81+
modifier isOperatorAllowlistSet() {
82+
if (_immutableAllowlistStorage().operatorAllowlistRegistry == address(0)) {
83+
revert OperatorAllowlistNotSet();
84+
}
85+
_;
86+
}
87+
88+
/*//////////////////////////////////////////////////////////////
89+
CALLBACK FUNCTIONS
90+
//////////////////////////////////////////////////////////////*/
91+
92+
/// @notice Callback function for ERC1155.setApprovalForAll
93+
function beforeApproveForAll(address _from, address _to, bool _approved)
94+
external
95+
override
96+
isOperatorAllowlistSet
97+
validateApproval(_to)
98+
returns (bytes memory)
99+
{}
100+
101+
/// @notice Callback function for ERC1155.transferFrom/safeTransferFrom
102+
function beforeTransferERC1155(address _from, address _to, uint256 _id, uint256 _value)
103+
external
104+
override
105+
isOperatorAllowlistSet
106+
validateTransfer(_from, _to)
107+
returns (bytes memory)
108+
{}
109+
110+
/// @notice Callback function for ERC1155.transferFrom/safeTransferFrom
111+
function beforeBatchTransferERC1155(address _from, address _to, uint256[] calldata _ids, uint256[] calldata _values)
112+
external
113+
override
114+
isOperatorAllowlistSet
115+
validateTransfer(_from, _to)
116+
returns (bytes memory)
117+
{}
118+
119+
/// @dev Called by a Core into an Module during the installation of the Module.
120+
function onInstall(bytes calldata data) external {
121+
address registry = abi.decode(data, (address));
122+
_immutableAllowlistStorage().operatorAllowlistRegistry = registry;
123+
}
124+
125+
/// @dev Called by a Core into an Module during the uninstallation of the Module.
126+
function onUninstall(bytes calldata data) external {}
127+
128+
/*//////////////////////////////////////////////////////////////
129+
Encode install / uninstall data
130+
//////////////////////////////////////////////////////////////*/
131+
132+
/// @dev Returns bytes encoded install params, to be sent to `onInstall` function
133+
function encodeBytesOnInstall(address operatorAllowlistRegistry) external pure returns (bytes memory) {
134+
return abi.encode(operatorAllowlistRegistry);
135+
}
136+
137+
/// @dev Returns bytes encoded uninstall params, to be sent to `onUninstall` function
138+
function encodeBytesOnUninstall() external pure returns (bytes memory) {
139+
return "";
140+
}
141+
142+
/*//////////////////////////////////////////////////////////////
143+
FALLBACK FUNCTIONS
144+
//////////////////////////////////////////////////////////////*/
145+
146+
/// @notice Set the operator allowlist registry address
147+
function setOperatorAllowlistRegistry(address newRegistry) external {
148+
_immutableAllowlistStorage().operatorAllowlistRegistry = newRegistry;
149+
}
150+
151+
/// @notice Get the current operator allowlist registry address
152+
function getOperatorAllowlistRegistry() external view returns (address) {
153+
return _immutableAllowlistStorage().operatorAllowlistRegistry;
154+
}
155+
156+
/*//////////////////////////////////////////////////////////////
157+
INTERNAL FUNCTIONS
158+
//////////////////////////////////////////////////////////////*/
159+
160+
function _immutableAllowlistStorage() internal pure returns (ImmutableAllowlistStorage.Data storage) {
161+
return ImmutableAllowlistStorage.data();
162+
}
163+
164+
}

0 commit comments

Comments
 (0)