@lukso/lsp7-contracts 0.16.8 → 0.17.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +55 -5
  3. package/artifacts/IAccessControlExtended.json +285 -0
  4. package/artifacts/ILSP7CappedBalance.json +27 -0
  5. package/artifacts/ILSP7CappedSupply.json +27 -0
  6. package/artifacts/ILSP7DigitalAsset.json +6 -3
  7. package/artifacts/ILSP7Mintable.json +62 -0
  8. package/artifacts/ILSP7NonTransferable.json +110 -0
  9. package/artifacts/ILSP7Revokable.json +75 -0
  10. package/artifacts/LSP7Burnable.json +7 -4
  11. package/artifacts/LSP7BurnableInitAbstract.json +7 -4
  12. package/artifacts/LSP7CappedBalanceAbstract.json +1159 -0
  13. package/artifacts/LSP7CappedBalanceInitAbstract.json +1167 -0
  14. package/artifacts/{LSP7CappedSupply.json → LSP7CappedSupplyAbstract.json} +8 -15
  15. package/artifacts/LSP7CappedSupplyInitAbstract.json +7 -14
  16. package/artifacts/LSP7CustomizableToken.json +1604 -0
  17. package/artifacts/LSP7CustomizableTokenInit.json +1601 -0
  18. package/artifacts/LSP7DigitalAsset.json +6 -3
  19. package/artifacts/LSP7DigitalAssetInitAbstract.json +6 -3
  20. package/artifacts/LSP7Mintable.json +376 -5
  21. package/artifacts/LSP7MintableAbstract.json +1186 -0
  22. package/artifacts/LSP7MintableInit.json +369 -5
  23. package/artifacts/LSP7MintableInitAbstract.json +1194 -0
  24. package/artifacts/LSP7NonTransferableAbstract.json +1236 -0
  25. package/artifacts/LSP7NonTransferableInitAbstract.json +1244 -0
  26. package/artifacts/LSP7RevokableAbstract.json +1186 -0
  27. package/artifacts/LSP7RevokableInitAbstract.json +1194 -0
  28. package/artifacts/LSP7Votes.json +66 -4
  29. package/artifacts/LSP7VotesInitAbstract.json +66 -4
  30. package/contracts/LSP7DigitalAsset.sol +56 -65
  31. package/contracts/LSP7DigitalAssetInitAbstract.sol +49 -71
  32. package/contracts/extensions/AccessControlExtended/AccessControlExtendedAbstract.sol +368 -0
  33. package/contracts/extensions/AccessControlExtended/AccessControlExtendedConstants.sol +13 -0
  34. package/contracts/extensions/AccessControlExtended/AccessControlExtendedErrors.sol +23 -0
  35. package/contracts/extensions/AccessControlExtended/AccessControlExtendedInitAbstract.sol +389 -0
  36. package/contracts/extensions/AccessControlExtended/IAccessControlExtended.sol +51 -0
  37. package/contracts/extensions/{LSP7Burnable.sol → LSP7Burnable/LSP7Burnable.sol} +2 -2
  38. package/contracts/extensions/{LSP7BurnableInitAbstract.sol → LSP7Burnable/LSP7BurnableInitAbstract.sol} +2 -4
  39. package/contracts/extensions/LSP7CappedBalance/ILSP7CappedBalance.sol +11 -0
  40. package/contracts/extensions/LSP7CappedBalance/LSP7CappedBalanceAbstract.sol +129 -0
  41. package/contracts/extensions/LSP7CappedBalance/LSP7CappedBalanceErrors.sol +10 -0
  42. package/contracts/extensions/LSP7CappedBalance/LSP7CappedBalanceInitAbstract.sol +179 -0
  43. package/contracts/extensions/LSP7CappedSupply/ILSP7CappedSupply.sol +11 -0
  44. package/contracts/extensions/LSP7CappedSupply/LSP7CappedSupplyAbstract.sol +56 -0
  45. package/contracts/extensions/LSP7CappedSupply/LSP7CappedSupplyErrors.sol +6 -0
  46. package/contracts/extensions/LSP7CappedSupply/LSP7CappedSupplyInitAbstract.sol +96 -0
  47. package/contracts/extensions/LSP7Mintable/ILSP7Mintable.sol +27 -0
  48. package/contracts/extensions/LSP7Mintable/LSP7MintableAbstract.sol +105 -0
  49. package/contracts/extensions/LSP7Mintable/LSP7MintableErrors.sol +5 -0
  50. package/contracts/extensions/LSP7Mintable/LSP7MintableInitAbstract.sol +156 -0
  51. package/contracts/extensions/LSP7NonTransferable/ILSP7NonTransferable.sol +48 -0
  52. package/contracts/extensions/LSP7NonTransferable/LSP7NonTransferableAbstract.sol +183 -0
  53. package/contracts/extensions/LSP7NonTransferable/LSP7NonTransferableErrors.sol +14 -0
  54. package/contracts/extensions/LSP7NonTransferable/LSP7NonTransferableInitAbstract.sol +240 -0
  55. package/contracts/extensions/LSP7Revokable/ILSP7Revokable.sol +28 -0
  56. package/contracts/extensions/LSP7Revokable/LSP7RevokableAbstract.sol +130 -0
  57. package/contracts/extensions/LSP7Revokable/LSP7RevokableErrors.sol +4 -0
  58. package/contracts/extensions/LSP7Revokable/LSP7RevokableInitAbstract.sol +183 -0
  59. package/contracts/extensions/{LSP7Votes.sol → LSP7Votes/LSP7Votes.sol} +37 -20
  60. package/contracts/extensions/LSP7Votes/LSP7VotesErrors.sol +24 -0
  61. package/contracts/extensions/{LSP7VotesInitAbstract.sol → LSP7Votes/LSP7VotesInitAbstract.sol} +37 -21
  62. package/contracts/presets/LSP7CustomizableToken.sol +262 -0
  63. package/contracts/presets/LSP7CustomizableTokenConstants.sol +32 -0
  64. package/contracts/presets/LSP7CustomizableTokenInit.sol +294 -0
  65. package/contracts/presets/LSP7Mintable.sol +14 -23
  66. package/contracts/presets/LSP7MintableInit.sol +16 -11
  67. package/dist/abi.cjs +7519 -54
  68. package/dist/abi.d.cts +11109 -338
  69. package/dist/abi.d.mts +11109 -338
  70. package/dist/abi.d.ts +11109 -338
  71. package/dist/abi.mjs +7503 -54
  72. package/dist/constants.cjs +21 -0
  73. package/dist/constants.d.cts +12 -1
  74. package/dist/constants.d.mts +12 -1
  75. package/dist/constants.d.ts +12 -1
  76. package/dist/constants.mjs +16 -1
  77. package/package.json +33 -9
  78. package/contracts/extensions/LSP7CappedSupply.sol +0 -83
  79. package/contracts/extensions/LSP7CappedSupplyInitAbstract.sol +0 -86
  80. package/contracts/presets/ILSP7Mintable.sol +0 -32
  81. package/contracts/presets/LSP7MintableInitAbstract.sol +0 -57
  82. /package/contracts/extensions/{LSP7VotesConstants.sol → LSP7Votes/LSP7VotesConstants.sol} +0 -0
@@ -0,0 +1,368 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ // interfaces
5
+ import {
6
+ IAccessControl
7
+ } from "@openzeppelin/contracts/access/IAccessControl.sol";
8
+ import {
9
+ IAccessControlEnumerable
10
+ } from "@openzeppelin/contracts/access/IAccessControlEnumerable.sol";
11
+ import {IAccessControlExtended} from "./IAccessControlExtended.sol";
12
+
13
+ // modules
14
+ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
15
+
16
+ // libraries
17
+ import {
18
+ EnumerableSet
19
+ } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
20
+
21
+ // constants
22
+ import {
23
+ _INTERFACEID_ACCESSCONTROL,
24
+ _INTERFACEID_ACCESSCONTROLENUMERABLE,
25
+ _INTERFACEID_ACCESSCONTROLEXTENDED
26
+ } from "./AccessControlExtendedConstants.sol";
27
+
28
+ // errors
29
+ import {
30
+ AccessControlUnauthorizedAccount,
31
+ AccessControlBadConfirmation,
32
+ AccessControlCannotSetAdminForDefaultAdminRole
33
+ } from "./AccessControlExtendedErrors.sol";
34
+
35
+ /**
36
+ * @title AccessControlExtendedAbstract
37
+ * @dev Abstract contract implementing OZ-compatible role management with reverse lookups
38
+ * and auxiliary data storage. Uses EnumerableSet composition (NOT OZ AccessControl inheritance)
39
+ *
40
+ * Provides:
41
+ * - Standard OZ {IAccessControl} functions: hasRole, grantRole, revokeRole, renounceRole, getRoleAdmin
42
+ * - Standard OZ {IAccessControlEnumerable} functions: getRoleMember, getRoleMemberCount
43
+ * - Extended functions: rolesOf, getRoleMembers
44
+ * - Explicit role checks for every role-gated function
45
+ * - DEFAULT_ADMIN_ROLE as root admin for granting and revoking roles
46
+ * - Automatic transfer of all roles (and their auxiliary data, if any) between old and new on ownership transfer
47
+ */
48
+ abstract contract AccessControlExtendedAbstract is
49
+ IAccessControlExtended,
50
+ Ownable
51
+ {
52
+ using EnumerableSet for EnumerableSet.AddressSet;
53
+ using EnumerableSet for EnumerableSet.Bytes32Set;
54
+
55
+ // --- Constants
56
+
57
+ /// @dev The default admin role. Value is `bytes32(0)` per OZ convention.
58
+ bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
59
+
60
+ // --- Storage
61
+
62
+ /// @dev Mapping from role to its admin role.
63
+ mapping(bytes32 role => bytes32 adminRole) private _roleAdmins;
64
+
65
+ /// @dev Forward lookup: role -> set of member addresses.
66
+ mapping(bytes32 role => EnumerableSet.AddressSet members)
67
+ private _roleMembers;
68
+
69
+ /// @dev Reverse lookup: address -> set of roles held.
70
+ mapping(address account => EnumerableSet.Bytes32Set rolesAssigned)
71
+ private _addressRoles;
72
+
73
+ // --- Modifier
74
+
75
+ /**
76
+ * @dev Modifier that checks the caller has `role`.
77
+ * Reverts with {AccessControlUnauthorizedAccount} if the check fails.
78
+ */
79
+ modifier onlyRole(bytes32 role) {
80
+ _checkRole(role);
81
+ _;
82
+ }
83
+
84
+ // --- Constructor
85
+
86
+ /**
87
+ * @dev Grants `DEFAULT_ADMIN_ROLE` to the contract owner so to allow it to administer roles to other addresses after deployment.
88
+ * This will also make it appear inside the enumerations (getRoleMember, rolesOf, getRoleMembers).
89
+ */
90
+ constructor() {
91
+ _grantRole(DEFAULT_ADMIN_ROLE, owner());
92
+ }
93
+
94
+ // --- ERC-165
95
+
96
+ /**
97
+ * @dev Returns true for {IAccessControl}, {IAccessControlEnumerable} and {IAccessControlExtended}.
98
+ */
99
+ function supportsInterface(
100
+ bytes4 interfaceId
101
+ ) public view virtual returns (bool) {
102
+ return
103
+ interfaceId == _INTERFACEID_ACCESSCONTROL ||
104
+ interfaceId == _INTERFACEID_ACCESSCONTROLENUMERABLE ||
105
+ interfaceId == _INTERFACEID_ACCESSCONTROLEXTENDED;
106
+ }
107
+
108
+ // --- IAccessControl
109
+
110
+ /// @inheritdoc IAccessControl
111
+ function hasRole(
112
+ bytes32 role,
113
+ address account
114
+ ) public view virtual returns (bool) {
115
+ return _hasRole(role, account);
116
+ }
117
+
118
+ /// @inheritdoc IAccessControl
119
+ function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
120
+ return _roleAdmins[role];
121
+ }
122
+
123
+ /**
124
+ * @inheritdoc IAccessControlExtended
125
+ *
126
+ * @dev Sets `adminRole` as the admin of `role`. Available for extensions to configure custom admin hierarchies.
127
+ *
128
+ * @custom:warning
129
+ * - DO NOT expose this function without `onlyOwner` or `onlyRole(DEFAULT_ADMIN_ROLE)` access control.
130
+ * - Be aware that calling `setRoleAdmin(X, X)` creates a self-admin where nobody can grant role `X` unless someone already holds role `X`.
131
+ *
132
+ * @custom:requirements
133
+ * - `role` cannot be the `DEFAULT_ADMIN_ROLE`.
134
+ * - The caller must hold the `DEFAULT_ADMIN_ROLE`.
135
+ *
136
+ * @custom:events {RoleAdminChanged} with the previous and new admin roles.
137
+ */
138
+ function setRoleAdmin(
139
+ bytes32 role,
140
+ bytes32 adminRole
141
+ ) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
142
+ _setRoleAdmin(role, adminRole);
143
+ }
144
+
145
+ /**
146
+ * @inheritdoc IAccessControl
147
+ * @dev Grants `role` to `account`.
148
+ *
149
+ * @custom:requirements The caller must hold the admin role for `role`.
150
+ */
151
+ function grantRole(
152
+ bytes32 role,
153
+ address account
154
+ ) public virtual onlyRole(getRoleAdmin(role)) {
155
+ _grantRole(role, account);
156
+ }
157
+
158
+ /**
159
+ * @inheritdoc IAccessControl
160
+ * @dev Revokes `role` from `account`. The caller must hold the admin role for `role`.
161
+ *
162
+ * @custom:warning `DEFAULT_ADMIN_ROLE` cannot be removed from the current owner to prevent lockout.
163
+ */
164
+ function revokeRole(
165
+ bytes32 role,
166
+ address account
167
+ ) public virtual onlyRole(getRoleAdmin(role)) {
168
+ require(
169
+ !(role == DEFAULT_ADMIN_ROLE && account == owner()),
170
+ AccessControlUnauthorizedAccount(account, role)
171
+ );
172
+ _revokeRole(role, account);
173
+ }
174
+
175
+ /**
176
+ * @inheritdoc IAccessControl
177
+ * @dev Allows `msg.sender` to renounce their own `role`. The `callerConfirmation`
178
+ * parameter must equal `msg.sender` to prevent accidental renouncement (OZ pattern).
179
+ * Renouncing triggers data cleanup.
180
+ *
181
+ * @custom:warning The current owner cannot renounce `DEFAULT_ADMIN_ROLE`
182
+ * to prevent locking the contract out of role administration.
183
+ *
184
+ * @custom:events Emits {RoleRevoked} if `msg.sender` currently holds `role` and successfully revokes it for itself.
185
+ */
186
+ function renounceRole(
187
+ bytes32 role,
188
+ address callerConfirmation
189
+ ) public virtual {
190
+ require(
191
+ callerConfirmation == msg.sender,
192
+ AccessControlBadConfirmation()
193
+ );
194
+ require(
195
+ !(role == DEFAULT_ADMIN_ROLE && msg.sender == owner()),
196
+ AccessControlUnauthorizedAccount(msg.sender, role)
197
+ );
198
+ _revokeRole(role, msg.sender);
199
+ }
200
+
201
+ // --- IAccessControlEnumerable
202
+
203
+ /// @inheritdoc IAccessControlEnumerable
204
+ function getRoleMember(
205
+ bytes32 role,
206
+ uint256 index
207
+ ) public view virtual returns (address) {
208
+ return _roleMembers[role].at(index);
209
+ }
210
+
211
+ /// @inheritdoc IAccessControlEnumerable
212
+ function getRoleMemberCount(
213
+ bytes32 role
214
+ ) public view virtual returns (uint256) {
215
+ return _roleMembers[role].length();
216
+ }
217
+
218
+ // --- IAccessControlExtended
219
+
220
+ /// @inheritdoc IAccessControlExtended
221
+ function rolesOf(
222
+ address account
223
+ ) public view virtual returns (bytes32[] memory) {
224
+ return _addressRoles[account].values();
225
+ }
226
+
227
+ /**
228
+ * @notice Returns all members that hold `role`.
229
+ *
230
+ * @dev Convenience function that returns the full membership array in a single call.
231
+ * Equivalent to calling {getRoleMember} for each index from `0` to `getRoleMemberCount(role) - 1`.
232
+ *
233
+ * @param role The role identifier to query members for.
234
+ * @return An array of addresses that currently hold the specified role.
235
+ *
236
+ * @custom:warning This function copies the entire role membership set into memory.
237
+ * For roles with a large number of members, this may consume a significant amount of gas. If calling this function on-chain, consider calling `{getRoleMember}` repeatedly, using `getRoleMemberCount` to know as max index.
238
+ * This function is primarily intended for off-chain usage.
239
+ */
240
+ function getRoleMembers(
241
+ bytes32 role
242
+ ) public view virtual returns (address[] memory) {
243
+ return _roleMembers[role].values();
244
+ }
245
+
246
+ // --- Internal functions
247
+
248
+ /**
249
+ * @dev Grants `role` to `account`. No-op if the account already holds the role
250
+ * (matching OZ behavior). Updates both forward and reverse lookups.
251
+ *
252
+ * @custom:events {RoleGranted} if the role was newly granted.
253
+ */
254
+ function _grantRole(bytes32 role, address account) internal virtual {
255
+ bool added = _roleMembers[role].add(account);
256
+
257
+ if (added) {
258
+ _addressRoles[account].add(role);
259
+ emit RoleGranted({
260
+ role: role,
261
+ account: account,
262
+ sender: msg.sender
263
+ });
264
+ }
265
+ }
266
+
267
+ /**
268
+ * @dev Revokes `role` from `account`. No-op if the account does not hold the role.
269
+ *
270
+ * @custom:events Emits {RoleRevoked} if the role was revoked.
271
+ */
272
+ function _revokeRole(bytes32 role, address account) internal virtual {
273
+ bool removed = _roleMembers[role].remove(account);
274
+
275
+ if (removed) {
276
+ _addressRoles[account].remove(role);
277
+ emit RoleRevoked({
278
+ role: role,
279
+ account: account,
280
+ sender: msg.sender
281
+ });
282
+ }
283
+ }
284
+
285
+ /**
286
+ * @dev Checks that `msg.sender` has `role`. Reverts with
287
+ * {AccessControlUnauthorizedAccount} if the check fails.
288
+ *
289
+ * @custom:warning Overriding this function changes the behavior of the {onlyRole} modifier.
290
+ */
291
+ function _checkRole(bytes32 role) internal view virtual {
292
+ _checkRole(role, msg.sender);
293
+ }
294
+
295
+ /**
296
+ * @dev Checks that `account` has `role`.
297
+ *
298
+ * Reverts with {AccessControlUnauthorizedAccount} if the account does not
299
+ * explicitly hold the role.
300
+ */
301
+ function _checkRole(bytes32 role, address account) internal view virtual {
302
+ require(
303
+ _hasRole(role, account),
304
+ AccessControlUnauthorizedAccount(account, role)
305
+ );
306
+ }
307
+
308
+ function _hasRole(
309
+ bytes32 role,
310
+ address account
311
+ ) internal view virtual returns (bool) {
312
+ return _roleMembers[role].contains(account);
313
+ }
314
+
315
+ function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
316
+ require(
317
+ role != DEFAULT_ADMIN_ROLE,
318
+ AccessControlCannotSetAdminForDefaultAdminRole()
319
+ );
320
+
321
+ bytes32 previousAdminRole = getRoleAdmin(role);
322
+ _roleAdmins[role] = adminRole;
323
+
324
+ emit RoleAdminChanged({
325
+ role: role,
326
+ previousAdminRole: previousAdminRole,
327
+ newAdminRole: adminRole
328
+ });
329
+ }
330
+
331
+ // --- Ownership sync
332
+
333
+ /**
334
+ * @dev Overrides `_transferOwnership` to automatically transfer ALL roles held by
335
+ * the old owner to the new owner. This includes `DEFAULT_ADMIN_ROLE` and any other
336
+ * custom roles the old owner was assigned.
337
+ *
338
+ * For each role held by the old owner:
339
+ * 1. The role is revoked from the old owner.
340
+ * 2. The role is granted to the new owner (if not already held).
341
+ *
342
+ * @custom:info When renouncing ownership, roles are only removed from the old owner. Roles are not passed to `address(0)` (being the `newOwner` in the case of renounce ownership).
343
+ *
344
+ * @custom:warning
345
+ * - Gas cost scales linearly with the number of roles the old owner holds.
346
+ * - If the old owner holds a large number of roles, the transaction may approach or exceed
347
+ * the block gas limit and fail. Avoid assigning too many roles to the owner to ensure
348
+ * ownership transfers remain callable.
349
+ */
350
+ function _transferOwnership(address newOwner) internal virtual override {
351
+ address oldOwner = owner();
352
+ Ownable._transferOwnership(newOwner);
353
+
354
+ // Snapshot the old owner's roles before mutating storage (values() returns a memory copy)
355
+ bytes32[] memory oldOwnerRoles = _addressRoles[oldOwner].values();
356
+
357
+ for (uint256 ii = 0; ii < oldOwnerRoles.length; ++ii) {
358
+ bytes32 role = oldOwnerRoles[ii];
359
+
360
+ _revokeRole(role, oldOwner);
361
+
362
+ // exclude case when renouncing ownership
363
+ if (newOwner != address(0)) {
364
+ _grantRole(role, newOwner);
365
+ }
366
+ }
367
+ }
368
+ }
@@ -0,0 +1,13 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ /// @dev ERC-165 interface ID for IAccessControl from OpenZeppelin.
5
+ bytes4 constant _INTERFACEID_ACCESSCONTROL = 0x7965db0b;
6
+
7
+ /// @dev ERC-165 interface ID for IAccessControlEnumerable from OpenZeppelin.
8
+ bytes4 constant _INTERFACEID_ACCESSCONTROLENUMERABLE = 0x5a05180f;
9
+
10
+ /// @dev ERC-165 interface ID for IAccessControlExtended.
11
+ /// Computed as XOR of selectors:
12
+ /// getRoleMembers(bytes32) ^ rolesOf(address) ^ setRoleAdmin(bytes32,bytes32) ^ getRoleAdmin(bytes32)
13
+ bytes4 constant _INTERFACEID_ACCESSCONTROLEXTENDED = 0x90832245;
@@ -0,0 +1,23 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ /**
5
+ * @dev Reverts when an address attempts a role-gated action and is missing the `neededRole`.
6
+ *
7
+ * @param account The address that attempted the action.
8
+ * @param neededRole The role that was required.
9
+ */
10
+ error AccessControlUnauthorizedAccount(address account, bytes32 neededRole);
11
+
12
+ /**
13
+ * @dev Reverts when the caller of the `renounceRole` function is not the expected one.
14
+ * NOTE: Do not confuse with {AccessControlUnauthorizedAccount}.
15
+ */
16
+ error AccessControlBadConfirmation();
17
+
18
+ /**
19
+ * @dev Reverts when trying to change the admin of the `DEFAULT_ADMIN_ROLE`.
20
+ * The `DEFAULT_ADMIN_ROLE` is its own admin, meaning only accounts with the `DEFAULT_ADMIN_ROLE` can grant or revoke it.
21
+ * This hierarchy cannot be changed.
22
+ */
23
+ error AccessControlCannotSetAdminForDefaultAdminRole();