-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStakedLyxToken.sol
714 lines (605 loc) · 25.6 KB
/
StakedLyxToken.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;
// libraries
import { EnumerableSet } from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import { LSP1Utils } from "@lukso/lsp-smart-contracts/contracts/LSP1UniversalReceiver/LSP1Utils.sol";
import { OwnablePausableUpgradeable } from "../presets/OwnablePausableUpgradeable.sol";
import { AccessControlUpgradeable } from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import { ERC725YCore } from "@erc725/smart-contracts/contracts/ERC725YCore.sol";
import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
// interfaces
import { ILSP7DigitalAsset } from "@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/ILSP7DigitalAsset.sol";
import { IERC165 } from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import { IStakedLyxToken } from "../interfaces/IStakedLyxToken.sol";
import { IRewards } from "../interfaces/IRewards.sol";
import { IPool } from "../interfaces/IPool.sol";
// errors
import {LSP7AmountExceedsAuthorizedAmount,
LSP7CannotSendToSelf,
LSP7InvalidTransferBatch,
LSP7CannotUseAddressZeroAsOperator,
LSP7TokenOwnerCannotBeOperator,
LSP7CannotSendWithAddressZero,
LSP7AmountExceedsBalance
} from "@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/LSP7Errors.sol";
// constants
import { LSP4DigitalAssetMetadataInitAbstract } from "@lukso/lsp-smart-contracts/contracts/LSP4DigitalAssetMetadata/LSP4DigitalAssetMetadataInitAbstract.sol";
import { _INTERFACEID_LSP7,_TYPEID_LSP7_TOKENOPERATOR} from "@lukso/lsp-smart-contracts/contracts/LSP7DigitalAsset/LSP7Constants.sol";
/**
* @title StakedLyxToken
*
* @dev StakedLyxToken contract stores pool staked tokens.
*/
contract StakedLyxToken is OwnablePausableUpgradeable, LSP4DigitalAssetMetadataInitAbstract, IStakedLyxToken, ReentrancyGuardUpgradeable {
using EnumerableSet for EnumerableSet.AddressSet;
// --- Storage
// @dev Validator deposit amount.
uint256 public constant override VALIDATOR_TOTAL_DEPOSIT = 32 ether;
// @dev Total deposits - total amount of tokens deposited (the amount of tokens unstaked is not deducted from this amount).
uint256 internal _totalDeposits;
// @dev Total Unstaked - total amount of tokens that were unstaked from the staking node and submitted for claiming.
uint256 public override totalUnstaked;
// @dev Total Pending Unstake - total amount of tokens pending to be unstaked. When unstaked, the amount unstaked is deducted
uint256 public override totalPendingUnstake;
// @dev Unstake Request Count - Used as an index to keep track of unstake requests.
uint256 public unstakeRequestCount;
// @dev Unstake Request Current Index - Used as an index to keep track of the current unstake request being processed. The previous indexes were all processed.
uint256 public unstakeRequestCurrentIndex;
// @dev Unstake Processing - Boolean used to pause unstake requests. When true, no unstake requests can be made, and no unstake requests can be matched.
// This is used to ensure the amount to unstake doesn t change while the unstake is processing
bool public override unstakeProcessing;
// @dev Unstake Request - Mapping containing all the unstake request requests in chronological order, the uint256 key being used as an index.
mapping(uint256 => UnstakeRequest) internal _unstakeRequests;
// Mapping from `tokenOwner` to an `amount` of tokens
mapping(address => uint256) internal _deposits;
// Mapping a `tokenOwner` to an `operator` to `amount` of tokens.
mapping(address => mapping(address => uint256)) internal _operatorAuthorizedAmount;
// @dev Address of the Pool contract.
IPool internal pool;
// @dev Address of the Oracles contract.
address internal oracles;
// @dev Address of the Rewards contract.
IRewards internal rewards;
// @dev The principal amount of the distributor.
uint256 public override distributorPrincipal;
// @dev Validators Exited Threshold - The number of validators that need to exit before we can set unstake processing to true.
uint256 internal validatorsExitedThreshold;
// Mapping an `address` to its authorized operator addresses.
mapping(address => EnumerableSet.AddressSet) internal _operators;
constructor() {
_disableInitializers();
}
function initialize(
address _admin,
IPool _pool,
address _oracles,
IRewards _rewards
) external initializer {
require(address(_pool) != address(0), "StakedLyxToken: pool address cannot be zero");
require(_oracles != address(0), "StakedLyxToken: oracles address cannot be zero");
require(_admin != address(0), "StakedLyxToken: admin address cannot be zero");
require(address(_rewards) != address(0), "StakedLyxToken: rewards address cannot be zero");
LSP4DigitalAssetMetadataInitAbstract._initialize("StakedLyxToken", "sLYX", _admin, 0);
__OwnablePausableUpgradeable_init_unchained(_admin);
pool = _pool;
oracles = _oracles;
rewards = _rewards;
}
// --- Token queries
function supportsInterface(bytes4 interfaceId) public view virtual override(AccessControlUpgradeable, IERC165, ERC725YCore) returns (bool) {
return
interfaceId == _INTERFACEID_LSP7 ||
super.supportsInterface(interfaceId);
}
function decimals() public pure override returns (uint8) {
return 18;
}
function totalSupply() public view override returns (uint256) {
return _totalDeposits;
}
/**
* @dev See {IStakedLyxToken-totalDeposits}.
*/
function totalDeposits() public view override returns (uint256) {
return _totalDeposits;
}
/**
* @dev See {IStakedLyxToken-totalClaimableAmount}.
*/
function totalClaimableUnstakes() public view override returns (uint256) {
uint256 totalClaimable;
for (uint256 i = 0; i <= unstakeRequestCurrentIndex; i++) {
UnstakeRequest memory _request = _unstakeRequests[i];
if (isUnstakeRequestClaimable(i)) totalClaimable += _request.amount;
}
return totalClaimable;
}
/**
* @dev See {IStakedLyxToken-unstakeRequest}.
*/
function unstakeRequest(uint256 index) public view override returns (UnstakeRequest memory) {
require(index <= unstakeRequestCount, "StakedLyxToken: invalid index");
UnstakeRequest memory _unstakeRequest = _unstakeRequests[index];
if (index < unstakeRequestCurrentIndex) {
_unstakeRequest.amountFilled = _unstakeRequest.amount;
}
return _unstakeRequest;
}
/**
* @dev See {IStakedLyxToken-isUnstakeRequestClaimable}.
*/
function isUnstakeRequestClaimable(uint256 index) public view override returns (bool) {
if (index > unstakeRequestCurrentIndex) return false;
UnstakeRequest memory _request = _unstakeRequests[index];
if (_request.claimed) return false;
if (index == unstakeRequestCurrentIndex && _request.amount != _request.amountFilled) return false;
return true;
}
// --- Token owner queries
function balanceOf(address tokenOwner) public view override returns (uint256) {
return _deposits[tokenOwner];
}
/**
* @dev See {IStakedLyxToken-toggleRewards}.
*/
function toggleRewards(address tokenOwner, bool isDisabled) external override onlyAdmin {
require(tokenOwner != address(0), "StakedLyxToken: invalid tokenOwner");
// toggle rewards
rewards.setRewardsDisabled(tokenOwner, isDisabled);
// update distributor principal
uint256 tokenOwnerBalance = _deposits[tokenOwner];
if (isDisabled) {
distributorPrincipal = distributorPrincipal + tokenOwnerBalance;
} else {
distributorPrincipal = distributorPrincipal - tokenOwnerBalance;
}
}
// --- Operator functionality
function authorizeOperator(
address operator,
uint256 amount,
bytes memory operatorNotificationData
) public virtual override {
_updateOperator(
msg.sender,
operator,
amount,
true,
operatorNotificationData
);
bytes memory lsp1Data = abi.encode(
msg.sender,
amount,
operatorNotificationData
);
_notifyTokenOperator(operator, lsp1Data);
}
function revokeOperator(
address operator,
bool notify,
bytes memory operatorNotificationData
) public virtual override {
_updateOperator(
msg.sender,
operator,
0,
notify,
operatorNotificationData
);
if (notify) {
bytes memory lsp1Data = abi.encode(
msg.sender,
0,
operatorNotificationData
);
_notifyTokenOperator(operator, lsp1Data);
}
}
function authorizedAmountFor(address operator, address tokenOwner) public view override returns (uint256)
{
if (tokenOwner == operator) {
return _deposits[tokenOwner];
} else {
return _operatorAuthorizedAmount[tokenOwner][operator];
}
}
function getOperatorsOf(
address tokenOwner
) public view virtual override returns (address[] memory) {
return _operators[tokenOwner].values();
}
// --- Transfer functionality
function transfer(
address from,
address to,
uint256 amount,
bool force,
bytes memory data
) public virtual override {
if (from == to) revert LSP7CannotSendToSelf();
if (msg.sender != from) {
_spendAllowance({
operator: msg.sender,
tokenOwner: from,
amountToSpend: amount
});
}
_transfer(from, to, amount, force, data);
}
function transferBatch(
address[] memory from,
address[] memory to,
uint256[] memory amount,
bool[] memory force,
bytes[] memory data
) public virtual override {
uint256 fromLength = from.length;
if (
fromLength != to.length ||
fromLength != amount.length ||
fromLength != force.length ||
fromLength != data.length
) {
revert LSP7InvalidTransferBatch();
}
for (uint256 i; i < fromLength; ) {
// using the public transfer function to handle updates to operator authorized amounts
transfer(from[i], to[i], amount[i], force[i], data[i]);
unchecked {
++i;
}
}
}
/**
* @dev Changes token `amount` the `operator` has access to from `tokenOwner` tokens.
* If the amount is zero the operator is removed from the list of operators, otherwise he is added to the list of operators.
* If the amount is zero then the operator is being revoked, otherwise the operator amount is being modified.
*
* @param tokenOwner The address that will give `operator` an allowance for on its balance.
* @param operator The address to grant an allowance to spend.
* @param allowance The maximum amount of token that `operator` can spend from the `tokenOwner`'s balance.
* @param notified Boolean indicating whether the operator has been notified about the change of allowance
* @param operatorNotificationData The data to send to the universalReceiver function of the operator in case of notifying
*
* @custom:events
* - {OperatorRevoked} event when operator's allowance is set to `0`.
* - {OperatorAuthorizationChanged} event when operator's allowance is set to any other amount.
*
* @custom:requirements
* - `operator` cannot be the zero address.
* - `operator` cannot be the same address as `tokenOwner`.
*/
function _updateOperator(
address tokenOwner,
address operator,
uint256 allowance,
bool notified,
bytes memory operatorNotificationData
) internal virtual {
if (operator == address(0)) {
revert LSP7CannotUseAddressZeroAsOperator();
}
if (operator == tokenOwner) {
revert LSP7TokenOwnerCannotBeOperator();
}
_operatorAuthorizedAmount[tokenOwner][operator] = allowance;
if (allowance != 0) {
_operators[tokenOwner].add(operator);
emit OperatorAuthorizationChanged(
operator,
tokenOwner,
allowance,
operatorNotificationData
);
} else {
_operators[tokenOwner].remove(operator);
emit OperatorRevoked(
operator,
tokenOwner,
notified,
operatorNotificationData
);
}
}
/**
* @dev See {IStakedLyxToken-unstake}.
*/
function unstake(
uint256 amount
) external override nonReentrant whenNotPaused {
address account = msg.sender;
require(amount > 0, "StakedLyxToken: amount must be greater than zero");
require(_deposits[account] >= amount, "StakedLyxToken: insufficient balance");
// start calculating account rewards with updated deposit amount
bool rewardsDisabled = rewards.updateRewardCheckpoint(account);
if (rewardsDisabled) {
// update merkle distributor principal if account has disabled rewards
distributorPrincipal = distributorPrincipal - amount;
}
_deposits[account] -= amount;
_totalDeposits -= amount;
totalPendingUnstake = totalPendingUnstake + amount;
unstakeRequestCount += 1;
_unstakeRequests[unstakeRequestCount] = UnstakeRequest({
account: account,
amount: uint128(amount),
amountFilled: 0,
claimed: false
});
emit NewUnstakeRequest(unstakeRequestCount, account, amount, totalPendingUnstake);
}
/**
* @dev See {IStakedLyxToken-matchUnstake}.
*/
function matchUnstake(uint256 amount) external override returns (uint256) {
require(msg.sender == address(pool), "StakedLyxToken: access denied");
require(!unstakeProcessing, "StakedLyxToken: unstaking in progress");
uint256 amountMatched = 0;
if (totalPendingUnstake == 0) {
return amountMatched;
}
else if (amount >= totalPendingUnstake) {
amountMatched = totalPendingUnstake;
totalPendingUnstake = 0;
unstakeRequestCurrentIndex = unstakeRequestCount;
_unstakeRequests[unstakeRequestCount].amountFilled = _unstakeRequests[unstakeRequestCount].amount;
} else {
amountMatched = amount;
totalPendingUnstake -= amountMatched;
uint256 amountToFill = amountMatched;
for (uint256 i = unstakeRequestCurrentIndex; i <= unstakeRequestCount; i++) {
UnstakeRequest storage request = _unstakeRequests[i];
if (amountToFill > (request.amount - request.amountFilled)) {
amountToFill -= (request.amount - request.amountFilled);
continue;
} else {
if (amountToFill == (request.amount - request.amountFilled) && i < unstakeRequestCount) {
unstakeRequestCurrentIndex = i + 1;
} else {
request.amountFilled += uint128(amountToFill);
unstakeRequestCurrentIndex = i;
}
break;
}
}
}
emit UnstakeMatched(amountMatched, totalPendingUnstake);
return amountMatched;
}
/**
* @dev See {IStakedLyxToken-setUnstakeProcessing}.
*/
function setUnstakeProcessing() external override {
require(msg.sender == oracles, "StakedLyxToken: access denied");
require(!unstakeProcessing, "StakedLyxToken: unstaking already in progress");
require(totalPendingUnstake >= VALIDATOR_TOTAL_DEPOSIT, "StakedLyxToken: insufficient pending unstake");
unstakeProcessing = true;
uint256 validatorsToUnstake = (totalPendingUnstake - (totalPendingUnstake % VALIDATOR_TOTAL_DEPOSIT)) / VALIDATOR_TOTAL_DEPOSIT;
validatorsExitedThreshold = pool.exitedValidators() + validatorsToUnstake;
emit UnstakeReady(validatorsToUnstake);
}
/**
* @dev See {IStakedLyxToken-unstakeProcessed}.
*/
function unstakeProcessed(uint256 exitedValidators) external override {
require(msg.sender == oracles, "StakedLyxToken: access denied");
require(unstakeProcessing, "StakedLyxToken: unstaking not in process");
uint256 unstakeAmount = exitedValidators * VALIDATOR_TOTAL_DEPOSIT;
if (unstakeAmount > totalPendingUnstake) {
rewards.sendToPoolWithoutActivation(unstakeAmount - totalPendingUnstake);
unstakeAmount = totalPendingUnstake;
totalPendingUnstake = 0;
unstakeRequestCurrentIndex = unstakeRequestCount;
_unstakeRequests[unstakeRequestCount].amountFilled = _unstakeRequests[unstakeRequestCount].amount;
}
else {
totalPendingUnstake -= unstakeAmount;
uint256 amountToFill = unstakeAmount;
for (uint256 i = unstakeRequestCurrentIndex; i <= unstakeRequestCount; i++) {
UnstakeRequest storage request = _unstakeRequests[i];
if (amountToFill > (request.amount - request.amountFilled)) {
amountToFill -= (request.amount - request.amountFilled);
continue;
} else {
if (amountToFill == (request.amount - request.amountFilled) && i < unstakeRequestCount) {
unstakeRequestCurrentIndex = i + 1;
} else {
request.amountFilled += uint128(amountToFill);
unstakeRequestCurrentIndex = i;
}
break;
}
}
}
totalUnstaked += unstakeAmount;
// If less pending unstake under VALIDATOR_TOTAL_DEPOSIT, it means the unstake is completed
if (pool.exitedValidators() + exitedValidators >= validatorsExitedThreshold) {
unstakeProcessing = false;
}
emit UnstakeProcessed(unstakeAmount, totalPendingUnstake);
}
/**
* @dev See {IStakedLyxToken-claimUnstake}.
*/
function claimUnstake(address account, uint256[] calldata unstakeRequestIndexes) external override returns (uint256) {
require(msg.sender == address(rewards), "StakedLyxToken: access denied");
uint256 totalClaimedAmount = 0;
for (uint256 i = 0; i < unstakeRequestIndexes.length; i++) {
uint256 unstakeRequestIndex = unstakeRequestIndexes[i];
require(isUnstakeRequestClaimable(unstakeRequestIndex), "StakedLyxToken: unstake request not claimable");
UnstakeRequest storage request = _unstakeRequests[unstakeRequestIndex];
require(request.account == account, "StakedLyxToken: unstake request not from this account");
totalClaimedAmount += request.amount;
request.claimed = true;
}
return totalClaimedAmount;
}
/**
* @dev See {IStakedLyxToken-mint}.
*/
function mint(address to,
uint256 amount,
bool force,
bytes memory data) external override {
require(msg.sender == address(pool), "StakedLyxToken: access denied");
// start calculating account rewards with updated deposit amount
bool rewardsDisabled = rewards.updateRewardCheckpoint(to);
if (rewardsDisabled) {
// update merkle distributor principal if account has disabled rewards
distributorPrincipal = distributorPrincipal + amount;
}
_mint(to, amount, force, data);
}
/**
* @dev Mints `amount` tokens and transfers it to `to`.
*
* Requirements:
*
* - `to` cannot be the zero address.
*
* Emits a {Transfer} event.
*/
function _mint(
address to,
uint256 amount,
bool force,
bytes memory data
) internal virtual {
if (to == address(0)) {
revert LSP7CannotSendWithAddressZero();
}
address operator = msg.sender;
// tokens being minted
_totalDeposits += amount;
_deposits[to] += amount;
emit Transfer({
operator: msg.sender,
from: address(0),
to: to,
amount: amount,
force: force,
data: data
});
}
/**
* @dev Transfers `amount` tokens from `from` to `to`.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - `from` cannot be the zero address.
* - `from` must have at least `amount` tokens.
* - If the caller is not `from`, it must be an operator for `from` with access to at least
* `amount` tokens.
*
* Emits a {Transfer} event.
*/
function _transfer(
address from,
address to,
uint256 amount,
bool force,
bytes memory data
) internal virtual whenNotPaused {
require(block.number > rewards.lastUpdateBlockNumber(), "StakedLyxToken: cannot transfer during rewards update");
if (from == address(0) || to == address(0)) {
revert LSP7CannotSendWithAddressZero();
}
uint256 balance = _deposits[from];
if (amount > balance) {
revert LSP7AmountExceedsBalance(balance, from, amount);
}
(bool senderRewardsDisabled, bool recipientRewardsDisabled) = rewards.updateRewardCheckpoints(from, to);
if ((senderRewardsDisabled || recipientRewardsDisabled) && !(senderRewardsDisabled && recipientRewardsDisabled)) {
// update merkle distributor principal if any of the addresses has disabled rewards
uint256 _distributorPrincipal = distributorPrincipal; // gas savings
if (senderRewardsDisabled) {
_distributorPrincipal = _distributorPrincipal - amount;
} else {
_distributorPrincipal = _distributorPrincipal + amount;
}
distributorPrincipal = _distributorPrincipal;
}
address operator = msg.sender;
_deposits[from] -= amount;
_deposits[to] += amount;
emit Transfer(operator, from, to, amount, force, data);
}
/**
* @dev Spend `amountToSpend` from the `operator`'s authorized on behalf of the `tokenOwner`.
*
* @param operator The address of the operator to decrease the allowance of.
* @param tokenOwner The address that granted an allowance on its balance to `operator`.
* @param amountToSpend The amount of tokens to substract in allowance of `operator`.
*
* @custom:events
* - {OperatorRevoked} event when operator's allowance is set to `0`.
* - {OperatorAuthorizationChanged} event when operator's allowance is set to any other amount.
*
* @custom:requirements
* - The `amountToSpend` MUST be at least the allowance granted to `operator` (accessible via {`authorizedAmountFor}`)
* - `operator` cannot be the zero address.
* - `operator` cannot be the same address as `tokenOwner`.
*/
function _spendAllowance(
address operator,
address tokenOwner,
uint256 amountToSpend
) internal virtual {
uint256 authorizedAmount = _operatorAuthorizedAmount[tokenOwner][
operator
];
if (amountToSpend > authorizedAmount) {
revert LSP7AmountExceedsAuthorizedAmount(
tokenOwner,
authorizedAmount,
operator,
amountToSpend
);
}
_updateOperator({
tokenOwner: tokenOwner,
operator: operator,
allowance: authorizedAmount - amountToSpend,
notified: false,
operatorNotificationData: ""
});
}
/**
* @dev Attempt to notify the operator `operator` about the `amount` tokens being authorized with.
* This is done by calling its {universalReceiver} function with the `_TYPEID_LSP7_TOKENOPERATOR` as typeId, if `operator` is a contract that supports the LSP1 interface.
* If `operator` is an EOA or a contract that does not support the LSP1 interface, nothing will happen and no notification will be sent.
* @param operator The address to call the {universalReceiver} function on.
* @param lsp1Data the data to be sent to the `operator` address in the `universalReceiver` call.
*/
function _notifyTokenOperator(
address operator,
bytes memory lsp1Data
) internal virtual {
LSP1Utils.notifyUniversalReceiver(
operator,
_TYPEID_LSP7_TOKENOPERATOR,
lsp1Data
);
}
// --- Methods for ERC20 compatibility ---
function allowance(address tokenOwner, address operator) public view virtual returns (uint256) {
return authorizedAmountFor(operator, tokenOwner);
}
function approve(address operator, uint256 amount) public virtual returns (bool) {
_updateOperator(msg.sender, operator, amount, false, "");
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
transfer(from, to, amount, true, "");
return true;
}
function transfer(address to, uint256 amount) public virtual override returns (bool) {
transfer(msg.sender, to, amount, true, "");
return true;
}
}