@lukso/lsp7-contracts 0.15.0 → 0.16.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 (39) hide show
  1. package/README.md +12 -2
  2. package/artifacts/ILSP7DigitalAsset.json +0 -112
  3. package/artifacts/LSP7Burnable.json +0 -11
  4. package/artifacts/LSP7BurnableInitAbstract.json +0 -16
  5. package/artifacts/LSP7CappedSupply.json +0 -11
  6. package/artifacts/LSP7CappedSupplyInitAbstract.json +0 -16
  7. package/artifacts/LSP7DigitalAsset.json +0 -11
  8. package/artifacts/LSP7DigitalAssetInitAbstract.json +0 -16
  9. package/artifacts/LSP7Mintable.json +2 -13
  10. package/artifacts/LSP7MintableInit.json +2 -13
  11. package/artifacts/LSP7Votes.json +1154 -0
  12. package/artifacts/LSP7VotesInitAbstract.json +1146 -0
  13. package/contracts/ILSP7DigitalAsset.sol +2 -9
  14. package/contracts/LSP7Constants.sol +10 -0
  15. package/contracts/LSP7DigitalAsset.sol +748 -27
  16. package/contracts/LSP7DigitalAssetInitAbstract.sol +752 -14
  17. package/contracts/extensions/LSP7CappedSupply.sol +1 -1
  18. package/contracts/extensions/LSP7CappedSupplyInitAbstract.sol +1 -1
  19. package/contracts/extensions/LSP7Votes.sol +416 -0
  20. package/contracts/extensions/LSP7VotesConstants.sol +8 -0
  21. package/contracts/extensions/LSP7VotesInitAbstract.sol +443 -0
  22. package/dist/index.cjs +5 -1
  23. package/dist/index.d.cts +5 -3
  24. package/dist/index.d.mts +5 -3
  25. package/dist/index.d.ts +5 -3
  26. package/dist/index.mjs +5 -1
  27. package/package.json +5 -6
  28. package/types/index.ts +3488 -1480
  29. package/contracts/LSP7DigitalAssetCore.sol +0 -744
  30. package/types/common.ts +0 -131
  31. package/types/contracts/ILSP7DigitalAsset.ts +0 -626
  32. package/types/contracts/LSP7DigitalAsset.ts +0 -698
  33. package/types/contracts/LSP7DigitalAssetInitAbstract.ts +0 -729
  34. package/types/contracts/extensions/LSP7Burnable.ts +0 -717
  35. package/types/contracts/extensions/LSP7BurnableInitAbstract.ts +0 -748
  36. package/types/contracts/extensions/LSP7CappedSupply.ts +0 -712
  37. package/types/contracts/extensions/LSP7CappedSupplyInitAbstract.ts +0 -743
  38. package/types/contracts/presets/LSP7Mintable.ts +0 -717
  39. package/types/contracts/presets/LSP7MintableInit.ts +0 -779
@@ -50,7 +50,7 @@ abstract contract LSP7CappedSupply is LSP7DigitalAsset {
50
50
  * @notice The maximum supply amount of tokens allowed to exist is `_TOKEN_SUPPLY_CAP`.
51
51
  *
52
52
  * @dev Get the maximum number of tokens that can exist to circulate. Once {totalSupply} reaches
53
- * reaches {totalSuuplyCap}, it is not possible to mint more tokens.
53
+ * reaches {totalSupplyCap}, it is not possible to mint more tokens.
54
54
  *
55
55
  * @return The maximum number of tokens that can exist in the contract.
56
56
  */
@@ -53,7 +53,7 @@ abstract contract LSP7CappedSupplyInitAbstract is LSP7DigitalAssetInitAbstract {
53
53
  /**
54
54
  * @notice The maximum supply amount of tokens allowed to exist is `_tokenSupplyCap`.
55
55
  * @dev Get the maximum number of tokens that can exist to circulate. Once {totalSupply} reaches
56
- * reaches {totalSuuplyCap}, it is not possible to mint more tokens.
56
+ * reaches {totalSupplyCap}, it is not possible to mint more tokens.
57
57
  *
58
58
  * @return The maximum number of tokens that can exist in the contract.
59
59
  */
@@ -0,0 +1,416 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {LSP7DigitalAsset} from "../LSP7DigitalAsset.sol";
5
+ import {
6
+ _TYPEID_LSP7_VOTESDELEGATOR,
7
+ _TYPEID_LSP7_VOTESDELEGATEE
8
+ } from "./LSP7VotesConstants.sol";
9
+ import {LSP1Utils} from "@lukso/lsp1-contracts/contracts/LSP1Utils.sol";
10
+ import {IERC5805} from "@openzeppelin/contracts/interfaces/IERC5805.sol";
11
+ import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
12
+ import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
13
+ import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
14
+ import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
15
+ import {Counters} from "@openzeppelin/contracts/utils/Counters.sol";
16
+
17
+ /**
18
+ * @dev Extension of LSP7 to support Compound-like voting and delegation. This version is more generic than Compound's,
19
+ * and supports token supply up to 2^224^ - 1, while COMP is limited to 2^96^ - 1.
20
+ *
21
+ * This extension keeps a history (checkpoints) of each account's vote power. Vote power can be delegated either
22
+ * by calling the {delegate} function directly, or by providing a signature to be used with {delegateBySig}. Voting
23
+ * power can be queried through the public accessors {getVotes} and {getPastVotes}.
24
+ *
25
+ * By default, token balance does not account for voting power. This makes transfers cheaper. The downside is that it
26
+ * requires users to delegate to themselves in order to activate checkpoints and have their voting power tracked.
27
+ */
28
+ abstract contract LSP7Votes is LSP7DigitalAsset, EIP712, IERC5805 {
29
+ using Counters for Counters.Counter;
30
+ mapping(address => Counters.Counter) private _nonces;
31
+
32
+ struct Checkpoint {
33
+ uint32 fromBlock;
34
+ uint224 votes;
35
+ }
36
+
37
+ bytes32 internal constant _DELEGATION_TYPEHASH =
38
+ keccak256("Delegation(address delegatee,uint256 nonce,uint256 expiry)");
39
+
40
+ mapping(address => address) private _delegates;
41
+ mapping(address => Checkpoint[]) private _checkpoints;
42
+ Checkpoint[] private _totalSupplyCheckpoints;
43
+
44
+ /**
45
+ * @dev Clock used for flagging checkpoints. Can be overridden to implement timestamp based checkpoints (and voting).
46
+ */
47
+ function clock() public view virtual override returns (uint48) {
48
+ return SafeCast.toUint48(block.number);
49
+ }
50
+
51
+ /**
52
+ * @dev Description of the clock
53
+ */
54
+ // solhint-disable-next-line func-name-mixedcase
55
+ function CLOCK_MODE() public view virtual override returns (string memory) {
56
+ // Check that the clock was not modified
57
+ require(clock() == block.number, "LSP7Votes: broken clock mode");
58
+ return "mode=blocknumber&from=default";
59
+ }
60
+
61
+ // solhint-disable-next-line func-name-mixedcase
62
+ function DOMAIN_SEPARATOR() external view returns (bytes32) {
63
+ return _domainSeparatorV4();
64
+ }
65
+
66
+ /**
67
+ * @dev Get the `pos`-th checkpoint for `account`.
68
+ */
69
+ function checkpoints(
70
+ address account,
71
+ uint32 pos
72
+ ) public view virtual returns (Checkpoint memory) {
73
+ return _checkpoints[account][pos];
74
+ }
75
+
76
+ /**
77
+ * @dev Get number of checkpoints for `account`.
78
+ */
79
+ function numCheckpoints(
80
+ address account
81
+ ) public view virtual returns (uint32) {
82
+ return SafeCast.toUint32(_checkpoints[account].length);
83
+ }
84
+
85
+ /**
86
+ * @dev Get the address `account` is currently delegating to.
87
+ */
88
+ function delegates(
89
+ address account
90
+ ) public view virtual override returns (address) {
91
+ return _delegates[account];
92
+ }
93
+
94
+ /**
95
+ * @dev Gets the current votes balance for `account`
96
+ */
97
+ function getVotes(
98
+ address account
99
+ ) public view virtual override returns (uint256) {
100
+ uint256 pos = _checkpoints[account].length;
101
+ unchecked {
102
+ return pos == 0 ? 0 : _checkpoints[account][pos - 1].votes;
103
+ }
104
+ }
105
+
106
+ /**
107
+ * @dev Retrieve the number of votes for `account` at the end of `timepoint`.
108
+ *
109
+ * @custom:requirements
110
+ * - `timepoint` must be in the past
111
+ */
112
+ function getPastVotes(
113
+ address account,
114
+ uint256 timepoint
115
+ ) public view virtual override returns (uint256) {
116
+ require(timepoint < clock(), "LSP7Votes: future lookup");
117
+ return _checkpointsLookup(_checkpoints[account], timepoint);
118
+ }
119
+
120
+ /**
121
+ * @dev Retrieve the `totalSupply` at the end of `timepoint`. Note, this value is the sum of all balances.
122
+ * It is NOT the sum of all the delegated votes!
123
+ *
124
+ * @custom:requirements
125
+ * - `timepoint` must be in the past
126
+ */
127
+ function getPastTotalSupply(
128
+ uint256 timepoint
129
+ ) public view virtual override returns (uint256) {
130
+ require(timepoint < clock(), "LSP7Votes: future lookup");
131
+ return _checkpointsLookup(_totalSupplyCheckpoints, timepoint);
132
+ }
133
+
134
+ /**
135
+ * @dev Lookup a value in a list of (sorted) checkpoints.
136
+ */
137
+ function _checkpointsLookup(
138
+ Checkpoint[] storage ckpts,
139
+ uint256 timepoint
140
+ ) private view returns (uint256) {
141
+ // We run a binary search to look for the last (most recent) checkpoint taken before (or at) `timepoint`.
142
+ //
143
+ // Initially we check if the block is recent to narrow the search range.
144
+ // During the loop, the index of the wanted checkpoint remains in the range [low-1, high).
145
+ // With each iteration, either `low` or `high` is moved towards the middle of the range to maintain the invariant.
146
+ // - If the middle checkpoint is after `timepoint`, we look in [low, mid)
147
+ // - If the middle checkpoint is before or equal to `timepoint`, we look in [mid+1, high)
148
+ // Once we reach a single value (when low == high), we've found the right checkpoint at the index high-1, if not
149
+ // out of bounds (in which case we're looking too far in the past and the result is 0).
150
+ // Note that if the latest checkpoint available is exactly for `timepoint`, we end up with an index that is
151
+ // past the end of the array, so we technically don't find a checkpoint after `timepoint`, but it works out
152
+ // the same.
153
+ uint256 length = ckpts.length;
154
+
155
+ uint256 low = 0;
156
+ uint256 high = length;
157
+
158
+ if (length > 5) {
159
+ uint256 mid = length - Math.sqrt(length);
160
+ if (_unsafeAccess(ckpts, mid).fromBlock > timepoint) {
161
+ high = mid;
162
+ } else {
163
+ low = mid + 1;
164
+ }
165
+ }
166
+
167
+ while (low < high) {
168
+ uint256 mid = Math.average(low, high);
169
+ if (_unsafeAccess(ckpts, mid).fromBlock > timepoint) {
170
+ high = mid;
171
+ } else {
172
+ low = mid + 1;
173
+ }
174
+ }
175
+
176
+ unchecked {
177
+ return high == 0 ? 0 : _unsafeAccess(ckpts, high - 1).votes;
178
+ }
179
+ }
180
+
181
+ /**
182
+ * @dev Delegate votes from the sender to `delegatee`.
183
+ */
184
+ function delegate(address delegatee) public virtual override {
185
+ _delegate(msg.sender, delegatee);
186
+ }
187
+
188
+ /**
189
+ * @dev Delegates votes from signer to `delegatee`
190
+ */
191
+ function delegateBySig(
192
+ address delegatee,
193
+ uint256 nonce,
194
+ uint256 expiry,
195
+ uint8 v,
196
+ bytes32 r,
197
+ bytes32 s
198
+ ) public virtual override {
199
+ // solhint-disable-next-line not-rely-on-time
200
+ require(block.timestamp <= expiry, "LSP7Votes: signature expired");
201
+ address signer = ECDSA.recover(
202
+ _hashTypedDataV4(
203
+ keccak256(
204
+ abi.encode(_DELEGATION_TYPEHASH, delegatee, nonce, expiry)
205
+ )
206
+ ),
207
+ v,
208
+ r,
209
+ s
210
+ );
211
+ require(nonce == _useNonce(signer), "LSP7Votes: invalid nonce");
212
+ _delegate(signer, delegatee);
213
+ }
214
+
215
+ /**
216
+ * @dev Maximum token supply. Defaults to `type(uint224).max` (2^224^ - 1).
217
+ */
218
+ function _maxSupply() internal view virtual returns (uint224) {
219
+ return type(uint224).max;
220
+ }
221
+
222
+ /**
223
+ * @dev Transfers, mints, or burns voting units. To register a mint, `from` should be zero. To register a burn, `to`
224
+ * should be zero. Total supply of voting units will be adjusted with mints and burns.
225
+ */
226
+ function _transferVotingUnits(
227
+ address from,
228
+ address to,
229
+ uint256 amount
230
+ ) internal virtual {
231
+ if (from == address(0)) {
232
+ _writeCheckpoint(_totalSupplyCheckpoints, _add, amount);
233
+ }
234
+ if (to == address(0)) {
235
+ _writeCheckpoint(_totalSupplyCheckpoints, _subtract, amount);
236
+ }
237
+ _moveVotingPower(delegates(from), delegates(to), amount);
238
+ }
239
+
240
+ /**
241
+ * @dev Move voting power when tokens are transferred.
242
+ *
243
+ * @custom:events
244
+ * - {DelegateVotesChanged} when voting power is removed from source address
245
+ * - {DelegateVotesChanged} when voting power is added to destination address
246
+ */
247
+ function _update(
248
+ address from,
249
+ address to,
250
+ uint256 value,
251
+ bool force,
252
+ bytes memory data
253
+ ) internal virtual override {
254
+ super._update(from, to, value, force, data);
255
+ if (from == address(0)) {
256
+ uint256 supply = totalSupply();
257
+ uint256 cap = _maxSupply();
258
+ require(
259
+ supply <= cap,
260
+ "LSP7Votes: total supply risks overflowing votes"
261
+ );
262
+ }
263
+ _transferVotingUnits(from, to, value);
264
+ }
265
+
266
+ /**
267
+ * @dev Change delegation for `delegator` to `delegatee`.
268
+ *
269
+ * @custom:events
270
+ * - {DelegateChanged}
271
+ * - {DelegateVotesChanged}
272
+ */
273
+ function _delegate(address delegator, address delegatee) internal virtual {
274
+ address currentDelegate = delegates(delegator);
275
+ uint256 delegatorBalance = balanceOf(delegator);
276
+ _delegates[delegator] = delegatee;
277
+
278
+ emit DelegateChanged(delegator, currentDelegate, delegatee);
279
+
280
+ _moveVotingPower(currentDelegate, delegatee, delegatorBalance);
281
+
282
+ // Notify the delegator if it's not address(0)
283
+ if (delegator != address(0)) {
284
+ bytes memory delegatorNotificationData = abi.encode(
285
+ msg.sender,
286
+ delegatee,
287
+ delegatorBalance
288
+ );
289
+ LSP1Utils.notifyUniversalReceiver(
290
+ delegator,
291
+ _TYPEID_LSP7_VOTESDELEGATOR,
292
+ delegatorNotificationData
293
+ );
294
+ }
295
+
296
+ // Only notify the new delegatee if it's not address(0) and if there's actual voting power
297
+ if (delegatee != address(0) && delegatorBalance > 0) {
298
+ bytes memory delegateeNotificationData = abi.encode(
299
+ msg.sender,
300
+ delegator,
301
+ delegatorBalance
302
+ );
303
+
304
+ LSP1Utils.notifyUniversalReceiver(
305
+ delegatee,
306
+ _TYPEID_LSP7_VOTESDELEGATEE,
307
+ delegateeNotificationData
308
+ );
309
+ }
310
+ }
311
+
312
+ /**
313
+ * @dev Moves voting power from one address to another.
314
+ *
315
+ * @custom:events
316
+ * - {DelegateVotesChanged} when voting power is removed from source address.
317
+ * - {DelegateVotesChanged} when voting power is added to destination address
318
+ */
319
+ function _moveVotingPower(
320
+ address src,
321
+ address dst,
322
+ uint256 amount
323
+ ) private {
324
+ if (src != dst && amount > 0) {
325
+ if (src != address(0)) {
326
+ (uint256 oldWeight, uint256 newWeight) = _writeCheckpoint(
327
+ _checkpoints[src],
328
+ _subtract,
329
+ amount
330
+ );
331
+ emit DelegateVotesChanged(src, oldWeight, newWeight);
332
+ }
333
+
334
+ if (dst != address(0)) {
335
+ (uint256 oldWeight, uint256 newWeight) = _writeCheckpoint(
336
+ _checkpoints[dst],
337
+ _add,
338
+ amount
339
+ );
340
+ emit DelegateVotesChanged(dst, oldWeight, newWeight);
341
+ }
342
+ }
343
+ }
344
+
345
+ function _writeCheckpoint(
346
+ Checkpoint[] storage ckpts,
347
+ function(uint256, uint256) view returns (uint256) op,
348
+ uint256 delta
349
+ ) private returns (uint256 oldWeight, uint256 newWeight) {
350
+ uint256 pos = ckpts.length;
351
+
352
+ unchecked {
353
+ Checkpoint memory oldCkpt = pos == 0
354
+ ? Checkpoint(0, 0)
355
+ : _unsafeAccess(ckpts, pos - 1);
356
+
357
+ oldWeight = oldCkpt.votes;
358
+ newWeight = op(oldWeight, delta);
359
+
360
+ if (pos > 0 && oldCkpt.fromBlock == clock()) {
361
+ _unsafeAccess(ckpts, pos - 1).votes = SafeCast.toUint224(
362
+ newWeight
363
+ );
364
+ } else {
365
+ ckpts.push(
366
+ Checkpoint({
367
+ fromBlock: SafeCast.toUint32(clock()),
368
+ votes: SafeCast.toUint224(newWeight)
369
+ })
370
+ );
371
+ }
372
+ }
373
+ }
374
+
375
+ function _add(uint256 a, uint256 b) private pure returns (uint256) {
376
+ return a + b;
377
+ }
378
+
379
+ function _subtract(uint256 a, uint256 b) private pure returns (uint256) {
380
+ return a - b;
381
+ }
382
+
383
+ /**
384
+ * @dev Access an element of the array without performing bounds check. The position is assumed to be within bounds.
385
+ */
386
+ function _unsafeAccess(
387
+ Checkpoint[] storage ckpts,
388
+ uint256 pos
389
+ ) private pure returns (Checkpoint storage result) {
390
+ // solhint-disable-next-line no-inline-assembly
391
+ assembly {
392
+ mstore(0, ckpts.slot)
393
+ result.slot := add(keccak256(0, 0x20), pos)
394
+ }
395
+ }
396
+
397
+ /**
398
+ * @dev Consumes a nonce.
399
+ *
400
+ * Returns the current value and increments nonce.
401
+ */
402
+ function _useNonce(
403
+ address owner
404
+ ) internal virtual returns (uint256 current) {
405
+ Counters.Counter storage nonce = _nonces[owner];
406
+ current = nonce.current();
407
+ nonce.increment();
408
+ }
409
+
410
+ /**
411
+ * @dev Reads the current nonce
412
+ */
413
+ function nonces(address owner) public view virtual returns (uint256) {
414
+ return _nonces[owner].current();
415
+ }
416
+ }
@@ -0,0 +1,8 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.4;
3
+
4
+ // keccak256('LSP7Tokens_VotesDelegatorNotification')
5
+ bytes32 constant _TYPEID_LSP7_VOTESDELEGATOR = 0x6117a486162c4ba8e38d646ef52b1e0e1be6bef05a980c041e232eba8c95e16f;
6
+
7
+ // keccak256('LSP7Tokens_VotesDelegateeNotification')
8
+ bytes32 constant _TYPEID_LSP7_VOTESDELEGATEE = 0x72cad372b29cde295ff0839b7b194597766b88f5fad4f7d6aef013e0c55dc492;