@lukso/lsp7-contracts 0.16.6 → 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 +56 -6
  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 +39 -15
  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,389 @@
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 {
15
+ OwnableUpgradeable
16
+ } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
17
+
18
+ // libraries
19
+ import {
20
+ EnumerableSet
21
+ } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
22
+
23
+ // constants
24
+ import {
25
+ _INTERFACEID_ACCESSCONTROL,
26
+ _INTERFACEID_ACCESSCONTROLENUMERABLE,
27
+ _INTERFACEID_ACCESSCONTROLEXTENDED
28
+ } from "./AccessControlExtendedConstants.sol";
29
+
30
+ // errors
31
+ import {
32
+ AccessControlUnauthorizedAccount,
33
+ AccessControlBadConfirmation,
34
+ AccessControlCannotSetAdminForDefaultAdminRole
35
+ } from "./AccessControlExtendedErrors.sol";
36
+
37
+ /**
38
+ * @title AccessControlExtendedInitAbstract
39
+ * @dev Proxy/initializable variant of {AccessControlExtendedAbstract}. Uses
40
+ * `__AccessControlExtended_init` / `__AccessControlExtended_init_unchained`
41
+ * instead of a constructor, both guarded by `onlyInitializing`.
42
+ */
43
+ abstract contract AccessControlExtendedInitAbstract is
44
+ IAccessControlExtended,
45
+ OwnableUpgradeable
46
+ {
47
+ using EnumerableSet for EnumerableSet.AddressSet;
48
+ using EnumerableSet for EnumerableSet.Bytes32Set;
49
+
50
+ // --- Constants
51
+
52
+ /// @dev The default admin role. Value is `bytes32(0)` per OZ convention.
53
+ bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
54
+
55
+ // --- Storage
56
+
57
+ /// @dev Mapping from role to its admin role.
58
+ mapping(bytes32 role => bytes32 adminRole) private _roleAdmins;
59
+
60
+ /// @dev Forward lookup: role -> set of member addresses.
61
+ mapping(bytes32 role => EnumerableSet.AddressSet members)
62
+ private _roleMembers;
63
+
64
+ /// @dev Reverse lookup: address -> set of roles held.
65
+ mapping(address account => EnumerableSet.Bytes32Set rolesAssigned)
66
+ private _addressRoles;
67
+
68
+ // --- Modifier
69
+
70
+ /**
71
+ * @dev Modifier that checks the caller has `role`.
72
+ * Reverts with {AccessControlUnauthorizedAccount} if the check fails.
73
+ */
74
+ modifier onlyRole(bytes32 role) {
75
+ _checkRole(role);
76
+ _;
77
+ }
78
+
79
+ // --- Initializer
80
+
81
+ /**
82
+ * @dev Chained initializer that grants DEFAULT_ADMIN_ROLE to the initial owner,
83
+ * so they appear in enumeration (getRoleMember, rolesOf) and can administer roles from initialization.
84
+ */
85
+ function __AccessControlExtended_init() internal virtual onlyInitializing {
86
+ __AccessControlExtended_init_unchained();
87
+ }
88
+
89
+ /**
90
+ * @dev Standalone initializer. Only grants DEFAULT_ADMIN_ROLE to the owner.
91
+ * Use when the LSP7 base is already initialized through another path.
92
+ */
93
+ function __AccessControlExtended_init_unchained()
94
+ internal
95
+ virtual
96
+ onlyInitializing
97
+ {
98
+ _grantRole(DEFAULT_ADMIN_ROLE, owner());
99
+ }
100
+
101
+ // --- ERC-165
102
+
103
+ /**
104
+ * @dev Returns true for {IAccessControl}, {IAccessControlEnumerable} and {IAccessControlExtended}.
105
+ */
106
+ function supportsInterface(
107
+ bytes4 interfaceId
108
+ ) public view virtual returns (bool) {
109
+ return
110
+ interfaceId == _INTERFACEID_ACCESSCONTROL ||
111
+ interfaceId == _INTERFACEID_ACCESSCONTROLENUMERABLE ||
112
+ interfaceId == _INTERFACEID_ACCESSCONTROLEXTENDED;
113
+ }
114
+
115
+ // --- IAccessControl
116
+
117
+ /// @inheritdoc IAccessControl
118
+ function hasRole(
119
+ bytes32 role,
120
+ address account
121
+ ) public view virtual returns (bool) {
122
+ return _hasRole(role, account);
123
+ }
124
+
125
+ /// @inheritdoc IAccessControl
126
+ function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
127
+ return _roleAdmins[role];
128
+ }
129
+
130
+ /**
131
+ * @inheritdoc IAccessControlExtended
132
+ *
133
+ * @dev Sets `adminRole` as the admin of `role`. Available for extensions
134
+ * to configure custom admin hierarchies.
135
+ *
136
+ * @custom:warning
137
+ * - DO NOT expose this function without `onlyOwner` or `onlyRole(DEFAULT_ADMIN_ROLE)` access control.
138
+ * - Be aware that calling `setRoleAdmin(X, X)` creates a self-admin where nobody can grant role `X` unless someone already holds role `X`.
139
+ *
140
+ * @custom:requirements
141
+ * - `role` cannot be the `DEFAULT_ADMIN_ROLE`.
142
+ * - The caller must hold the `DEFAULT_ADMIN_ROLE`.
143
+ *
144
+ * @custom:events {RoleAdminChanged} with the previous and new admin roles.
145
+ */
146
+ function setRoleAdmin(
147
+ bytes32 role,
148
+ bytes32 adminRole
149
+ ) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
150
+ _setRoleAdmin(role, adminRole);
151
+ }
152
+
153
+ /**
154
+ * @inheritdoc IAccessControl
155
+ * @dev Grants `role` to `account`.
156
+ *
157
+ * @custom:requirements The caller must hold the admin role for `role`.
158
+ */
159
+ function grantRole(
160
+ bytes32 role,
161
+ address account
162
+ ) public virtual onlyRole(getRoleAdmin(role)) {
163
+ _grantRole(role, account);
164
+ }
165
+
166
+ /**
167
+ * @inheritdoc IAccessControl
168
+ * @dev Revokes `role` from `account`. The caller must hold the admin role for `role`.
169
+ *
170
+ * @custom:warning `DEFAULT_ADMIN_ROLE` cannot be removed from the current owner to prevent lockout.
171
+ */
172
+ function revokeRole(
173
+ bytes32 role,
174
+ address account
175
+ ) public virtual onlyRole(getRoleAdmin(role)) {
176
+ require(
177
+ !(role == DEFAULT_ADMIN_ROLE && account == owner()),
178
+ AccessControlUnauthorizedAccount(account, role)
179
+ );
180
+ _revokeRole(role, account);
181
+ }
182
+
183
+ /**
184
+ * @inheritdoc IAccessControl
185
+ * @dev Allows `msg.sender` to renounce their own `role`. The `callerConfirmation`
186
+ * parameter must equal `msg.sender` to prevent accidental renouncement (OZ pattern).
187
+ * Renouncing triggers data cleanup.
188
+ *
189
+ * @custom:warning The current owner cannot renounce `DEFAULT_ADMIN_ROLE`
190
+ * to prevent locking the contract out of role administration.
191
+ *
192
+ * @custom:events Emits {RoleRevoked} if `msg.sender` currently holds `role` and successfully revokes it for itself.
193
+ */
194
+ function renounceRole(
195
+ bytes32 role,
196
+ address callerConfirmation
197
+ ) public virtual {
198
+ require(
199
+ callerConfirmation == msg.sender,
200
+ AccessControlBadConfirmation()
201
+ );
202
+ require(
203
+ !(role == DEFAULT_ADMIN_ROLE && msg.sender == owner()),
204
+ AccessControlUnauthorizedAccount(msg.sender, role)
205
+ );
206
+ _revokeRole(role, msg.sender);
207
+ }
208
+
209
+ // --- IAccessControlEnumerable
210
+
211
+ /// @inheritdoc IAccessControlEnumerable
212
+ function getRoleMember(
213
+ bytes32 role,
214
+ uint256 index
215
+ ) public view virtual returns (address) {
216
+ return _roleMembers[role].at(index);
217
+ }
218
+
219
+ /// @inheritdoc IAccessControlEnumerable
220
+ function getRoleMemberCount(
221
+ bytes32 role
222
+ ) public view virtual returns (uint256) {
223
+ return _roleMembers[role].length();
224
+ }
225
+
226
+ // --- IAccessControlExtended
227
+
228
+ /// @inheritdoc IAccessControlExtended
229
+ function rolesOf(
230
+ address account
231
+ ) public view virtual returns (bytes32[] memory) {
232
+ return _addressRoles[account].values();
233
+ }
234
+
235
+ /**
236
+ * @notice Returns all members that hold `role`.
237
+ *
238
+ * @dev Convenience function that returns the full membership array in a single call.
239
+ * Equivalent to calling {getRoleMember} for each index from `0` to `getRoleMemberCount(role) - 1`.
240
+ *
241
+ * @param role The role identifier to query members for.
242
+ * @return An array of addresses that currently hold the specified role.
243
+ *
244
+ * @custom:warning This function copies the entire role membership set into memory.
245
+ * 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.
246
+ * This function is primarily intended for off-chain usage.
247
+ */
248
+ function getRoleMembers(
249
+ bytes32 role
250
+ ) public view virtual returns (address[] memory) {
251
+ return _roleMembers[role].values();
252
+ }
253
+
254
+ // --- Internal functions
255
+
256
+ /**
257
+ * @dev Grants `role` to `account`. No-op if the account already holds the role
258
+ * (matching OZ behavior). Updates both forward and reverse lookups.
259
+ *
260
+ * @custom:events {RoleGranted} if the role was newly granted.
261
+ */
262
+ function _grantRole(bytes32 role, address account) internal virtual {
263
+ bool added = _roleMembers[role].add(account);
264
+
265
+ if (added) {
266
+ _addressRoles[account].add(role);
267
+ emit RoleGranted({
268
+ role: role,
269
+ account: account,
270
+ sender: msg.sender
271
+ });
272
+ }
273
+ }
274
+
275
+ /**
276
+ * @dev Revokes `role` from `account`. No-op if the account does not hold the role.
277
+ * Auto-clears auxiliary data if any exists.
278
+ *
279
+ * @custom:events Emits {RoleRevoked} if the role was revoked.
280
+ */
281
+ function _revokeRole(bytes32 role, address account) internal virtual {
282
+ bool removed = _roleMembers[role].remove(account);
283
+
284
+ if (removed) {
285
+ _addressRoles[account].remove(role);
286
+ emit RoleRevoked({
287
+ role: role,
288
+ account: account,
289
+ sender: msg.sender
290
+ });
291
+ }
292
+ }
293
+
294
+ /**
295
+ * @dev Checks that `msg.sender` has `role`. Reverts with
296
+ * {AccessControlUnauthorizedAccount} if the check fails.
297
+ *
298
+ * @custom:warning Overriding this function changes the behavior of the {onlyRole} modifier.
299
+ */
300
+ function _checkRole(bytes32 role) internal view virtual {
301
+ _checkRole(role, msg.sender);
302
+ }
303
+
304
+ /**
305
+ * @dev Checks that `account` has `role`.
306
+ *
307
+ * Reverts with {AccessControlUnauthorizedAccount} if the account does not
308
+ * explicitly hold the role.
309
+ */
310
+ function _checkRole(bytes32 role, address account) internal view virtual {
311
+ require(
312
+ _hasRole(role, account),
313
+ AccessControlUnauthorizedAccount(account, role)
314
+ );
315
+ }
316
+
317
+ function _hasRole(
318
+ bytes32 role,
319
+ address account
320
+ ) internal view virtual returns (bool) {
321
+ return _roleMembers[role].contains(account);
322
+ }
323
+
324
+ function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
325
+ require(
326
+ role != DEFAULT_ADMIN_ROLE,
327
+ AccessControlCannotSetAdminForDefaultAdminRole()
328
+ );
329
+
330
+ bytes32 previousAdminRole = getRoleAdmin(role);
331
+ _roleAdmins[role] = adminRole;
332
+
333
+ emit RoleAdminChanged({
334
+ role: role,
335
+ previousAdminRole: previousAdminRole,
336
+ newAdminRole: adminRole
337
+ });
338
+ }
339
+
340
+ // --- Ownership sync
341
+
342
+ /**
343
+ * @dev Overrides `_transferOwnership` to automatically transfer ALL roles held by
344
+ * the old owner to the new owner. This includes `DEFAULT_ADMIN_ROLE` and any other
345
+ * custom roles the old owner was assigned.
346
+ *
347
+ * For each role held by the old owner:
348
+ * 1. The role is revoked from the old owner (including clearing auxiliary data).
349
+ * 2. The role is granted to the new owner (if not already held).
350
+ *
351
+ * @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).
352
+ *
353
+ * @custom:warning
354
+ * - Gas cost scales linearly with the number of roles the old owner holds.
355
+ * - Auxiliary role data set on the old owner is transferred to the new owner if ownership is transferred.
356
+ * - Transferring ownership to self will still clear then restore the old owner's auxiliary role data.
357
+ * - If the old owner holds a large number of roles, the transaction may approach or exceed
358
+ * the block gas limit and fail. Avoid assigning too many roles to the owner to ensure
359
+ * ownership transfers remain callable.
360
+ */
361
+ function _transferOwnership(address newOwner) internal virtual override {
362
+ address oldOwner = owner();
363
+ OwnableUpgradeable._transferOwnership(newOwner);
364
+
365
+ // Snapshot the old owner's roles before mutating storage (values() returns a memory copy)
366
+ bytes32[] memory oldOwnerRoles = _addressRoles[oldOwner].values();
367
+
368
+ for (uint256 ii = 0; ii < oldOwnerRoles.length; ++ii) {
369
+ bytes32 role = oldOwnerRoles[ii];
370
+
371
+ _revokeRole(role, oldOwner);
372
+
373
+ // exclude case when renouncing ownership
374
+ if (newOwner != address(0)) {
375
+ _grantRole(role, newOwner);
376
+ }
377
+ }
378
+ }
379
+
380
+ /**
381
+ * @dev This empty reserved space is put in place to allow future versions to add new
382
+ * variables without shifting down storage in the inheritance chain.
383
+ * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
384
+ *
385
+ * @custom:info The size of the `__gap` array is calculated so that the amount of storage used by the contract
386
+ * always adds up to the same number (in this case 50 storage slots).
387
+ */
388
+ uint256[47] private __gap;
389
+ }
@@ -0,0 +1,51 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ // interfaces
5
+ import {
6
+ IAccessControlEnumerable
7
+ } from "@openzeppelin/contracts/access/IAccessControlEnumerable.sol";
8
+
9
+ /**
10
+ * @title IAccessControlExtended
11
+ *
12
+ * @dev Interface extending IAccessControlEnumerable with reverse role lookups and function to set admin role for a given role.
13
+ * Inherits all functions from {IAccessControl} and {IAccessControlEnumerable}.
14
+ *
15
+ * @custom:info This interface only include the setter function `setRoleAdmin(bytes32,bytes32)`. The getter `getRoleAdmin(bytes32)` is inherited from {IAccessControl}
16
+ * (itself inherited through {IAccessControlEnumerable}). This ensures that the selector `getRoleAdmin(bytes32)` is not used twice to calculate the final interface Id.
17
+ */
18
+ interface IAccessControlExtended is IAccessControlEnumerable {
19
+ /**
20
+ * @notice Sets a new admin role for `role`.
21
+ * @param role The role identifier being configured.
22
+ * @param adminRole The role that will become the new admin of `role`.
23
+ */
24
+ function setRoleAdmin(bytes32 role, bytes32 adminRole) external;
25
+
26
+ /**
27
+ * @notice Returns all members that hold `role`.
28
+ *
29
+ * @dev Convenience function that returns the full membership array in a single call.
30
+ * Equivalent to calling {getRoleMember} for each index from `0` to
31
+ * `getRoleMemberCount(role) - 1`.
32
+ *
33
+ * @param role The role identifier to query members for.
34
+ * @return An array of addresses that currently hold the specified role.
35
+ *
36
+ * @custom:warning This function copies the entire role membership set into memory.
37
+ * 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.
38
+ * This function is primarily intended for off-chain usage.
39
+ */
40
+ function getRoleMembers(
41
+ bytes32 role
42
+ ) external view returns (address[] memory);
43
+
44
+ /**
45
+ * @notice Returns all roles assigned to `account`.
46
+ * @dev Uses a reverse lookup to enumerate all roles held by a given address.
47
+ * @param account The address to query roles for.
48
+ * @return An array of role identifiers assigned to the account.
49
+ */
50
+ function rolesOf(address account) external view returns (bytes32[] memory);
51
+ }
@@ -1,8 +1,8 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.4;
2
+ pragma solidity ^0.8.27;
3
3
 
4
4
  // modules
5
- import {LSP7DigitalAsset} from "../LSP7DigitalAsset.sol";
5
+ import {LSP7DigitalAsset} from "../../LSP7DigitalAsset.sol";
6
6
 
7
7
  /**
8
8
  * @title LSP7 token extension that allows token holders to destroy both
@@ -1,10 +1,8 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.4;
2
+ pragma solidity ^0.8.27;
3
3
 
4
4
  // modules
5
- import {
6
- LSP7DigitalAssetInitAbstract
7
- } from "../LSP7DigitalAssetInitAbstract.sol";
5
+ import {LSP7DigitalAssetInitAbstract} from "../../LSP7DigitalAssetInitAbstract.sol";
8
6
 
9
7
  /**
10
8
  * @title LSP7 token extension that allows token holders to destroy both
@@ -0,0 +1,11 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ /// @title ILSP7CappedBalance
5
+ /// @dev Interface for an LSP7 token extension that enforces a per-address balance cap, with exemptions for allowlisted addresses.
6
+ interface ILSP7CappedBalance {
7
+ /// @notice Retrieves the maximum token balance allowed per address.
8
+ /// @dev Returns the immutable balance cap set during contract deployment.
9
+ /// @return The maximum token balance allowed for any single address.
10
+ function tokenBalanceCap() external view returns (uint256);
11
+ }
@@ -0,0 +1,129 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ // modules
5
+ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
6
+ import {LSP7DigitalAsset} from "../../LSP7DigitalAsset.sol";
7
+ import {
8
+ AccessControlExtendedAbstract
9
+ } from "../AccessControlExtended/AccessControlExtendedAbstract.sol";
10
+
11
+ // interfaces
12
+ import {ILSP7CappedBalance} from "./ILSP7CappedBalance.sol";
13
+
14
+ // errors
15
+ import {LSP7CappedBalanceExceeded} from "./LSP7CappedBalanceErrors.sol";
16
+
17
+ /// @title LSP7CappedBalanceAbstract
18
+ /// @dev Abstract contract implementing a per-address balance cap for LSP7 tokens, with role base access control and exemptions.
19
+ /// Inherits from LSP7AllowlistAbstract to integrate allowlist functionality.
20
+ abstract contract LSP7CappedBalanceAbstract is
21
+ ILSP7CappedBalance,
22
+ LSP7DigitalAsset,
23
+ AccessControlExtendedAbstract
24
+ {
25
+ /// @notice The dead address is also commonly used for burning tokens as an alternative to address(0).
26
+ address internal constant _DEAD_ADDRESS =
27
+ 0x000000000000000000000000000000000000dEaD;
28
+
29
+ /// @notice The immutable maximum token balance allowed per address.
30
+ uint256 internal immutable _TOKEN_BALANCE_CAP;
31
+
32
+ /// @dev keccak256("UNCAPPED_BALANCE_ROLE")
33
+ bytes32 public constant UNCAPPED_BALANCE_ROLE =
34
+ 0x975773d1e0a917a74b57f36a377f439ffff6271648aebdbff75a52ab58eb7bad;
35
+
36
+ /// @notice Initializes the contract with a token balance cap.
37
+ /// @dev Sets the immutable balance cap. If set, grants the initial uncapped balance role exemption to the contract owner.
38
+ /// @param tokenBalanceCap_ The maximum balance allowed per token holder (in wei). Set to 0 to disable.
39
+ constructor(uint256 tokenBalanceCap_) {
40
+ _TOKEN_BALANCE_CAP = tokenBalanceCap_;
41
+
42
+ if (tokenBalanceCap_ != 0) {
43
+ _grantRole(UNCAPPED_BALANCE_ROLE, owner());
44
+ }
45
+ }
46
+
47
+ /// @inheritdoc ILSP7CappedBalance
48
+ function tokenBalanceCap() public view virtual override returns (uint256) {
49
+ return _TOKEN_BALANCE_CAP;
50
+ }
51
+
52
+ function supportsInterface(
53
+ bytes4 interfaceId
54
+ )
55
+ public
56
+ view
57
+ virtual
58
+ override(AccessControlExtendedAbstract, LSP7DigitalAsset)
59
+ returns (bool)
60
+ {
61
+ return
62
+ AccessControlExtendedAbstract.supportsInterface(interfaceId) ||
63
+ LSP7DigitalAsset.supportsInterface(interfaceId);
64
+ }
65
+
66
+ /// @notice Checks if a token transfer complies with the balance cap.
67
+ /// @dev The address(0) is not subject to balance cap checks as this address is used for burning tokens. Reverts with {LSP7CappedBalanceExceeded} if the recipient's balance after receiving tokens would exceed the maximum allowed balance.
68
+ /// @param from The address sending the tokens.
69
+ /// @param to The address receiving the tokens.
70
+ /// @param amount The amount of tokens being transferred.
71
+ function _tokenBalanceCapCheck(
72
+ address from,
73
+ address to,
74
+ uint256 amount,
75
+ bool /* force */,
76
+ bytes memory /* data */
77
+ ) internal virtual {
78
+ // self-transfers do not increase the balance of the recipient, so we skip the check
79
+ if (from == to) return;
80
+
81
+ // Address(0) and 0x0000...dead addresses are used for burning tokens
82
+ if (to == address(0) || to == _DEAD_ADDRESS) return;
83
+
84
+ // Do not check for addresses exempted from balance cap
85
+ if (hasRole(UNCAPPED_BALANCE_ROLE, to)) return;
86
+
87
+ uint256 maxBalanceAllowed = tokenBalanceCap();
88
+ bool isBalanceCapEnabled = maxBalanceAllowed != 0;
89
+
90
+ if (!isBalanceCapEnabled) return;
91
+
92
+ require(
93
+ (balanceOf(to) + amount) <= maxBalanceAllowed,
94
+ LSP7CappedBalanceExceeded(
95
+ to,
96
+ amount,
97
+ balanceOf(to),
98
+ maxBalanceAllowed
99
+ )
100
+ );
101
+ }
102
+
103
+ /// @notice Hook called before a token transfer to enforce balance cap restrictions.
104
+ /// @dev Bypasses balance cap checks for allowlisted recipients. Applies cap checks for non-allowlisted recipients.
105
+ /// @param from The address sending the tokens.
106
+ /// @param to The address receiving the tokens.
107
+ /// @param amount The amount of tokens being transferred.
108
+ /// @param force Whether to force the transfer.
109
+ /// @param data Additional data for the transfer.
110
+ function _beforeTokenTransfer(
111
+ address from,
112
+ address to,
113
+ uint256 amount,
114
+ bool force,
115
+ bytes memory data
116
+ ) internal virtual override {
117
+ _tokenBalanceCapCheck(from, to, amount, force, data);
118
+ super._beforeTokenTransfer(from, to, amount, force, data);
119
+ }
120
+
121
+ function _transferOwnership(
122
+ address newOwner
123
+ ) internal virtual override(AccessControlExtendedAbstract, Ownable) {
124
+ // restore default admin hierarchy so a previously-installed custom admin
125
+ // cannot grant UNCAPPED_BALANCE_ROLE to new accounts post-transfer
126
+ _setRoleAdmin(UNCAPPED_BALANCE_ROLE, DEFAULT_ADMIN_ROLE);
127
+ super._transferOwnership(newOwner);
128
+ }
129
+ }
@@ -0,0 +1,10 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.27;
3
+
4
+ /// @dev Error thrown when a transfer would cause an address's balance to exceed the token balance cap.
5
+ error LSP7CappedBalanceExceeded(
6
+ address to,
7
+ uint256 tranferAmount,
8
+ uint256 balanceOf,
9
+ uint256 tokenBalanceCap
10
+ );