-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroidMining.sol
924 lines (757 loc) · 34.3 KB
/
AsteroidMining.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;
import {AccessControl} from "openzeppelin-contracts/access/AccessControl.sol";
import {ReentrancyGuard} from "openzeppelin-contracts/security/ReentrancyGuard.sol";
import {SafeCastLib} from "solmate/utils/SafeCastLib.sol";
import "./Math.sol";
import "./Structs.sol";
import {IncentiveId} from "./IncentiveId.sol";
/// @title Bagholder
/// @author zefram.eth (forked and edited by me for SpaceRats project)
/// @notice Incentivize NFT holders to keep holding their bags without letting their
/// precious NFTs leave their wallets.
/// @dev Uses an optimistic staking model where if someone staked and then transferred
/// their NFT elsewhere, someone else can slash them and receive the staker's bond.
contract AsteroidMining is AccessControl, ReentrancyGuard {
/// -----------------------------------------------------------------------
/// Library usage
/// -----------------------------------------------------------------------
using SafeCastLib for uint256;
using IncentiveId for IncentiveKey;
/// -----------------------------------------------------------------------
/// Errors
/// -----------------------------------------------------------------------
/// @notice Thrown when unstaking an NFT that hasn't been staked
error AsteroidMining__NotStaked();
/// @notice Thrown when an unauthorized account tries to perform an action available
/// only to the NFT's owner
error AsteroidMining__NotNftOwner();
/// @notice Thrown when trying to slash someone who shouldn't be slashed
error AsteroidMining__NotPaperHand();
/// @notice Thrown when staking an NFT that's already staked
error AsteroidMining__AlreadyStaked();
/// @notice Thrown when the bond provided by the staker differs from the specified amount
error AsteroidMining__BondIncorrect();
/// @notice Thrown when creating an incentive using invalid parameters (e.g. start time is after end time)
error AsteroidMining__InvalidIncentiveKey();
/// @notice Thrown when staking into an incentive that doesn't exist
error AsteroidMining__IncentiveNonexistent();
/// @notice Thrown when creating an incentive with a zero reward rate
error AsteroidMining__RewardAmountTooSmall();
/// @notice Thrown when creating an incentive that already exists
error AsteroidMining__IncentiveAlreadyExists();
/// @notice Thrown when setting the protocol fee recipient to the zero address
/// while having a non-zero protocol fee
error AsteroidMining_ProtocolFeeRecipientIsZero();
/// @notice Thrown when call failed to send ether
error AsteroidMining__FailedToSendEther();
/// -----------------------------------------------------------------------
/// Events
/// -----------------------------------------------------------------------
event Stake(
address indexed staker,
bytes32 indexed incentiveId,
uint256 indexed nftId
);
event Unstake(
address indexed staker,
bytes32 indexed incentiveId,
uint256 indexed nftId,
address bondRecipient
);
event SlashPaperHand(
address indexed sender,
bytes32 indexed incentiveId,
uint256 indexed nftId,
address bondRecipient
);
event CreateIncentive(
address indexed sender,
bytes32 indexed incentiveId,
IncentiveKey key,
uint256 rewardAmount,
uint256 protocolFeeAmount
);
event ClaimRewards(
address indexed staker,
bytes32 indexed incentiveId,
address recipient
);
event ClaimRefund(
address indexed sender,
bytes32 indexed incentiveId,
uint256 refundAmount
);
event SetProtocolFee(ProtocolFeeInfo protocolFeeInfo_);
/// -----------------------------------------------------------------------
/// Constants
/// -----------------------------------------------------------------------
/// @notice The precision used by rewardPerToken
uint256 internal constant PRECISION = 1e27;
/// -----------------------------------------------------------------------
/// Storage variables
/// -----------------------------------------------------------------------
/// @notice Records the address that staked an NFT into an incentive.
/// Zero address if the NFT hasn't been staked into the incentive.
/// @dev incentive ID => NFT ID => staker address
mapping(bytes32 => mapping(uint256 => address)) public stakers;
/// @notice Records accounting info about each staker.
/// @dev incentive ID => staker address => info
mapping(bytes32 => mapping(address => StakerInfo)) public stakerInfos;
/// @notice Records accounting info about each incentive.
/// @dev incentive ID => info
mapping(bytes32 => IncentiveInfo) public incentiveInfos;
/// @notice Records total minging (staking) time for an account across incentives
mapping(address => uint256) public miningTime;
/// @notice Stores the amount and recipient of the protocol fee
ProtocolFeeInfo public protocolFeeInfo;
/// -----------------------------------------------------------------------
/// Constructor
/// -----------------------------------------------------------------------
constructor(ProtocolFeeInfo memory protocolFeeInfo_) {
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
if (
protocolFeeInfo_.fee != 0 &&
protocolFeeInfo_.recipient == address(0)
) {
revert AsteroidMining_ProtocolFeeRecipientIsZero();
}
protocolFeeInfo = protocolFeeInfo_;
emit SetProtocolFee(protocolFeeInfo_);
}
/// -----------------------------------------------------------------------
/// Public actions
/// -----------------------------------------------------------------------
/// @notice Stakes an NFT into an incentive. The NFT stays in the user's wallet.
/// The caller must provide the ETH bond (specified in the incentive key) as part of
/// the call. Anyone can stake on behalf of anyone else, provided they provide the bond.
/// @param key the incentive's key
/// @param nftId the ID of the NFT
function stake(IncentiveKey calldata key, uint256 nftId) external payable {
/// -----------------------------------------------------------------------
/// Storage loads
/// -----------------------------------------------------------------------
bytes32 incentiveId = key.compute();
address staker = key.nft.ownerOf(nftId);
StakerInfo memory stakerInfo = stakerInfos[incentiveId][staker];
IncentiveInfo memory incentiveInfo = incentiveInfos[incentiveId];
/// -----------------------------------------------------------------------
/// Validation
/// -----------------------------------------------------------------------
// check bond is correct
if (msg.value != key.bondAmount) {
revert AsteroidMining__BondIncorrect();
}
// check the NFT is not currently being staked in this incentive
if (stakers[incentiveId][nftId] != address(0)) {
revert AsteroidMining__AlreadyStaked();
}
// ensure the incentive exists
if (incentiveInfo.lastUpdateTime == 0) {
revert AsteroidMining__IncentiveNonexistent();
}
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// accrue mining time (multiplier is numberOfStakedTokens)
miningTime[staker] +=
stakerInfo.numberOfStakedTokens *
(block.timestamp - stakerInfo.startedStaking);
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
key,
stakerInfo,
incentiveInfo
);
// update stake state
stakers[incentiveId][nftId] = staker;
// update staker state
stakerInfo.numberOfStakedTokens += 1;
stakerInfo.startedStaking = block.timestamp; // UPDATES BLOCK.TIMESTAMP
stakerInfos[incentiveId][staker] = stakerInfo;
// update incentive state
incentiveInfo.numberOfStakedTokens += 1;
incentiveInfos[incentiveId] = incentiveInfo;
emit Stake(staker, incentiveId, nftId);
}
/// @notice Stakes multiple NFTs into incentives. The NFTs stay in the user's wallet.
/// The caller must provide the ETH bond (specified in the incentive keys) as part of
/// the call. Anyone can stake on behalf of anyone else, provided they provide the bond.
/// @param inputs The array of inputs, with each input consisting of an incentive key
/// and an NFT ID.
function stakeMultiple(StakeMultipleInput[] calldata inputs)
external
payable
{
uint256 numInputs = inputs.length;
uint256 totalBondRequired;
for (uint256 i; i < numInputs; ) {
/// -----------------------------------------------------------------------
/// Storage loads
/// -----------------------------------------------------------------------
bytes32 incentiveId = inputs[i].key.compute();
uint256 nftId = inputs[i].nftId;
address staker = inputs[i].key.nft.ownerOf(nftId);
StakerInfo memory stakerInfo = stakerInfos[incentiveId][staker];
IncentiveInfo memory incentiveInfo = incentiveInfos[incentiveId];
/// -----------------------------------------------------------------------
/// Validation
/// -----------------------------------------------------------------------
// check the NFT is not currently being staked in this incentive
if (stakers[incentiveId][nftId] != address(0)) {
revert AsteroidMining__AlreadyStaked();
}
// ensure the incentive exists
if (incentiveInfo.lastUpdateTime == 0) {
revert AsteroidMining__IncentiveNonexistent();
}
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// accrue mining time (multiplier is numberOfStakedTokens)
miningTime[staker] +=
stakerInfo.numberOfStakedTokens *
(block.timestamp - stakerInfo.startedStaking);
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
inputs[i].key,
stakerInfo,
incentiveInfo
);
// update stake state
stakers[incentiveId][nftId] = staker;
// update staker state
stakerInfo.numberOfStakedTokens += 1;
stakerInfo.startedStaking = block.timestamp; // UPDATES BLOCK.TIMESTAMP
stakerInfos[incentiveId][staker] = stakerInfo;
// update incentive state
incentiveInfo.numberOfStakedTokens += 1;
incentiveInfos[incentiveId] = incentiveInfo;
emit Stake(staker, incentiveId, nftId);
totalBondRequired += inputs[i].key.bondAmount;
unchecked {
++i;
}
}
// check bond is correct
if (msg.value != totalBondRequired) {
revert AsteroidMining__BondIncorrect();
}
}
/// @notice Unstakes an NFT from an incentive and returns the ETH bond.
/// The caller must be the owner of the NFT AND the current staker.
/// @param key the incentive's key
/// @param nftId the ID of the NFT
/// @param bondRecipient the recipient of the ETH bond
function unstake(
IncentiveKey calldata key,
uint256 nftId,
address bondRecipient
) external nonReentrant {
/// -----------------------------------------------------------------------
/// Validation
/// -----------------------------------------------------------------------
bytes32 incentiveId = key.compute();
address staker = key.nft.ownerOf(nftId);
// check the NFT is currently being staked in the incentive
if (stakers[incentiveId][nftId] != msg.sender) {
revert AsteroidMining__NotStaked();
}
// check msg.sender owns the NFT
if (key.nft.ownerOf(nftId) != msg.sender) {
revert AsteroidMining__NotNftOwner();
}
/// -----------------------------------------------------------------------
/// Storage loads
/// -----------------------------------------------------------------------
StakerInfo memory stakerInfo = stakerInfos[incentiveId][msg.sender];
IncentiveInfo memory incentiveInfo = incentiveInfos[incentiveId];
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// accrue mining time (multiplier is numberOfStakedTokens)
miningTime[staker] +=
stakerInfo.numberOfStakedTokens *
(block.timestamp - stakerInfo.startedStaking);
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
key,
stakerInfo,
incentiveInfo
);
// update NFT state
delete stakers[incentiveId][nftId];
// update staker state
stakerInfo.numberOfStakedTokens -= 1;
if (stakerInfo.numberOfStakedTokens == 0) {
stakerInfo.startedStaking = 0; // UPDATES BLOCK.TIMESTAMP
}
stakerInfos[incentiveId][msg.sender] = stakerInfo;
// update incentive state
incentiveInfo.numberOfStakedTokens -= 1;
incentiveInfos[incentiveId] = incentiveInfo;
/// -----------------------------------------------------------------------
/// External calls
/// -----------------------------------------------------------------------
// return bond to user
(bool sent, ) = bondRecipient.call{value: key.bondAmount}("");
if (!sent) revert AsteroidMining__FailedToSendEther();
emit Unstake(msg.sender, incentiveId, nftId, bondRecipient);
}
/// @notice Unstaked an NFT from an incentive, then use the bond to stake an NFT into another incentive.
/// Must be called by the owner of the unstaked NFT. The bond amount of the incentive to unstake from must be at least
/// that of the incentive to stake in, with any extra bond sent to the specified recipient.
/// @param unstakeKey the key of the incentive to unstake from
/// @param unstakeNftId the ID of the NFT to unstake
/// @param stakeKey the key of the incentive to stake into
/// @param stakeNftId the ID of the NFT to stake
/// @param bondRecipient the recipient of any extra bond
function restake(
IncentiveKey calldata unstakeKey,
uint256 unstakeNftId,
IncentiveKey calldata stakeKey,
uint256 stakeNftId,
address bondRecipient
) external nonReentrant {
/// -----------------------------------------------------------------------
/// Validation
/// -----------------------------------------------------------------------
bytes32 unstakeIncentiveId = unstakeKey.compute();
bytes32 stakeIncentiveId = stakeKey.compute();
// check the NFT is currently being staked in the unstake incentive
if (stakers[unstakeIncentiveId][unstakeNftId] != msg.sender) {
revert AsteroidMining__NotStaked();
}
// check msg.sender owns the unstaked NFT
if (unstakeKey.nft.ownerOf(unstakeNftId) != msg.sender) {
revert AsteroidMining__NotNftOwner();
}
// check there's enough bond
if (unstakeKey.bondAmount < stakeKey.bondAmount) {
revert AsteroidMining__BondIncorrect();
}
// check the staked NFT is not currently being staked in the stake incentive
if (stakers[stakeIncentiveId][stakeNftId] != address(0)) {
revert AsteroidMining__AlreadyStaked();
}
/// -----------------------------------------------------------------------
/// Storage loads (Unstake)
/// -----------------------------------------------------------------------
StakerInfo memory stakerInfo = stakerInfos[unstakeIncentiveId][
msg.sender
];
IncentiveInfo memory incentiveInfo = incentiveInfos[unstakeIncentiveId];
/// -----------------------------------------------------------------------
/// State updates (Unstake)
/// -----------------------------------------------------------------------
// accrue mining time (multiplier is numberOfStakedTokens)
miningTime[msg.sender] +=
stakerInfo.numberOfStakedTokens *
(block.timestamp - stakerInfo.startedStaking);
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
unstakeKey,
stakerInfo,
incentiveInfo
);
// update NFT state
delete stakers[unstakeIncentiveId][unstakeNftId];
// update staker state
stakerInfo.numberOfStakedTokens -= 1;
if (stakerInfo.numberOfStakedTokens == 0) {
stakerInfo.startedStaking = 0; // UPDATES BLOCK.TIMESTAMP
}
stakerInfos[unstakeIncentiveId][msg.sender] = stakerInfo;
// update incentive state
incentiveInfo.numberOfStakedTokens -= 1;
incentiveInfos[unstakeIncentiveId] = incentiveInfo;
emit Unstake(
msg.sender,
unstakeIncentiveId,
unstakeNftId,
bondRecipient
);
/// -----------------------------------------------------------------------
/// Storage loads (Stake)
/// -----------------------------------------------------------------------
address staker = stakeKey.nft.ownerOf(stakeNftId);
stakerInfo = stakerInfos[stakeIncentiveId][staker];
incentiveInfo = incentiveInfos[stakeIncentiveId];
// ensure the incentive exists
if (incentiveInfo.lastUpdateTime == 0) {
revert AsteroidMining__IncentiveNonexistent();
}
/// -----------------------------------------------------------------------
/// State updates (Stake)
/// -----------------------------------------------------------------------
// accrue mining time (multiplier is numberOfStakedTokens)
miningTime[staker] +=
stakerInfo.numberOfStakedTokens *
(block.timestamp - stakerInfo.startedStaking);
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
stakeKey,
stakerInfo,
incentiveInfo
);
// update stake state
stakers[stakeIncentiveId][stakeNftId] = staker;
// update staker state
stakerInfo.numberOfStakedTokens += 1;
stakerInfo.startedStaking = block.timestamp;
stakerInfos[stakeIncentiveId][staker] = stakerInfo;
// update incentive state
incentiveInfo.numberOfStakedTokens += 1;
incentiveInfos[stakeIncentiveId] = incentiveInfo;
emit Stake(staker, stakeIncentiveId, stakeNftId);
/// -----------------------------------------------------------------------
/// External calls
/// -----------------------------------------------------------------------
if (unstakeKey.bondAmount != stakeKey.bondAmount) {
unchecked {
// return extra bond to user
// already checked unstakeKey.bondAmount > stakeKey.bondAmount
(bool sent, ) = bondRecipient.call{
value: unstakeKey.bondAmount - stakeKey.bondAmount
}("");
if (!sent) revert AsteroidMining__FailedToSendEther();
}
}
}
/// @notice Slashes a staker who has transferred the staked NFT to another address.
/// The bond is given to the slasher as reward.
/// @param key the incentive's key
/// @param nftId the ID of the NFT
/// @param bondRecipient the recipient of the ETH bond
function slashPaperHand(
IncentiveKey calldata key,
uint256 nftId,
address bondRecipient
) external nonReentrant {
/// -----------------------------------------------------------------------
/// Validation
/// -----------------------------------------------------------------------
bytes32 incentiveId = key.compute();
// check the NFT is currently being staked in this incentive by someone other than the NFT owner
address staker = stakers[incentiveId][nftId];
if (staker == address(0) || staker == key.nft.ownerOf(nftId)) {
revert AsteroidMining__NotPaperHand();
}
/// -----------------------------------------------------------------------
/// Storage loads
/// -----------------------------------------------------------------------
StakerInfo memory stakerInfo = stakerInfos[incentiveId][staker];
IncentiveInfo memory incentiveInfo = incentiveInfos[incentiveId];
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// Slash miningTime
miningTime[staker] = 0;
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
key,
stakerInfo,
incentiveInfo
);
// update NFT state
delete stakers[incentiveId][nftId];
// update staker state
stakerInfo.numberOfStakedTokens -= 1;
stakerInfo.startedStaking = 0;
stakerInfos[incentiveId][staker] = stakerInfo;
// update incentive state
incentiveInfo.numberOfStakedTokens -= 1;
incentiveInfos[incentiveId] = incentiveInfo;
/// -----------------------------------------------------------------------
/// External calls
/// -----------------------------------------------------------------------
// send bond to recipient as reward
(bool sent, ) = bondRecipient.call{value: key.bondAmount}("");
if (!sent) revert AsteroidMining__FailedToSendEther();
emit SlashPaperHand(msg.sender, incentiveId, nftId, bondRecipient);
}
/// @notice Creates an incentive and transfers the reward tokens from the caller.
/// @dev Will revert if the incentive key is invalid (e.g. startTime >= endTime)
/// @param key the incentive's key
/// @param rewardAmount the amount of reward tokens to add to the incentive
function createIncentive(
IncentiveKey calldata key,
uint256 rewardAmount,
uint256 geodeMiningTime
) external onlyRole(DEFAULT_ADMIN_ROLE) {
/// -----------------------------------------------------------------------
/// Validation
/// -----------------------------------------------------------------------
bytes32 incentiveId = key.compute();
ProtocolFeeInfo memory protocolFeeInfo_ = protocolFeeInfo;
// ensure incentive doesn't already exist
if (incentiveInfos[incentiveId].lastUpdateTime != 0) {
revert AsteroidMining__IncentiveAlreadyExists();
}
// ensure incentive key is valid
if (
address(key.nft) == address(0) ||
address(key.rewardToken) == address(0) ||
key.startTime >= key.endTime ||
key.endTime < block.timestamp
) {
revert AsteroidMining__InvalidIncentiveKey();
}
// apply protocol fee
uint256 protocolFeeAmount;
if (protocolFeeInfo_.fee != 0) {
protocolFeeAmount = (rewardAmount * protocolFeeInfo_.fee) / 1000;
rewardAmount -= protocolFeeAmount;
}
// ensure incentive amount makes sense
uint128 rewardRatePerSecond = (rewardAmount /
(key.endTime - key.startTime)).safeCastTo128();
if (rewardRatePerSecond == 0) {
revert AsteroidMining__RewardAmountTooSmall();
}
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// create incentive info
incentiveInfos[incentiveId] = IncentiveInfo({
rewardRatePerSecond: rewardRatePerSecond,
rewardPerTokenStored: 0,
numberOfStakedTokens: 0,
lastUpdateTime: block.timestamp.safeCastTo64(),
accruedRefund: 0,
miningTimeForGeodes: geodeMiningTime
});
/// -----------------------------------------------------------------------
/// External calls
/// -----------------------------------------------------------------------
// transfer reward tokens from sender
key.rewardToken.transferFrom(msg.sender, address(this), rewardAmount);
// transfer protocol fee
if (protocolFeeAmount != 0) {
key.rewardToken.transferFrom(
msg.sender,
protocolFeeInfo_.recipient,
protocolFeeAmount
);
}
emit CreateIncentive(
msg.sender,
incentiveId,
key,
rewardAmount,
protocolFeeAmount
);
}
/// @notice Claims the reward tokens the caller has earned from a particular incentive.
/// @param key the incentive's key
/// @param recipient the recipient of the reward tokens
/// @return rewardAmount the amount of reward tokens claimed
function claimRewards(IncentiveKey calldata key, address recipient)
external
returns (uint256 rewardAmount)
{
bytes32 incentiveId = key.compute();
/// -----------------------------------------------------------------------
/// Storage loads
/// -----------------------------------------------------------------------
StakerInfo memory stakerInfo = stakerInfos[incentiveId][msg.sender];
IncentiveInfo memory incentiveInfo = incentiveInfos[incentiveId];
// ensure the incentive exists
if (incentiveInfo.lastUpdateTime == 0) {
revert AsteroidMining__IncentiveNonexistent();
}
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// accrue mining time (multiplier is numberOfStakedTokens)
miningTime[msg.sender] +=
stakerInfo.numberOfStakedTokens *
(block.timestamp - stakerInfo.startedStaking);
// accrue rewards
(stakerInfo, incentiveInfo) = _accrueRewards(
key,
stakerInfo,
incentiveInfo
);
// update staker state
rewardAmount = stakerInfo.totalRewardUnclaimed;
stakerInfo.totalRewardUnclaimed = 0;
stakerInfo.startedStaking = 0;
stakerInfos[incentiveId][msg.sender] = stakerInfo;
// update incentive state
incentiveInfos[incentiveId] = incentiveInfo;
/// -----------------------------------------------------------------------
/// External calls
/// -----------------------------------------------------------------------
// transfer reward to user
key.rewardToken.transfer(recipient, rewardAmount);
emit ClaimRewards(msg.sender, incentiveId, recipient);
// mint geodes
if (miningTime[msg.sender] >= incentiveInfo.miningTimeForGeodes) {
miningTime[msg.sender] = 0;
key.rewardNft.mint(recipient);
}
}
function claimRefund(IncentiveKey calldata key)
external
returns (uint256 refundAmount)
{
bytes32 incentiveId = key.compute();
/// -----------------------------------------------------------------------
/// Storage loads
/// -----------------------------------------------------------------------
IncentiveInfo memory incentiveInfo = incentiveInfos[incentiveId];
refundAmount = incentiveInfo.accruedRefund;
// ensure the incentive exists
if (incentiveInfo.lastUpdateTime == 0) {
revert AsteroidMining__IncentiveNonexistent();
}
/// -----------------------------------------------------------------------
/// State updates
/// -----------------------------------------------------------------------
// accrue rewards
uint256 lastTimeRewardApplicable = min(block.timestamp, key.endTime);
uint256 rewardPerToken_ = _rewardPerToken(
incentiveInfo,
lastTimeRewardApplicable
);
if (incentiveInfo.numberOfStakedTokens == 0) {
// [lastUpdateTime, lastTimeRewardApplicable] was a period without any staked NFTs
// accrue refund
refundAmount +=
incentiveInfo.rewardRatePerSecond *
(lastTimeRewardApplicable - incentiveInfo.lastUpdateTime);
}
incentiveInfo.rewardPerTokenStored = rewardPerToken_;
incentiveInfo.lastUpdateTime = lastTimeRewardApplicable.safeCastTo64();
incentiveInfo.accruedRefund = 0;
// update incentive state
incentiveInfos[incentiveId] = incentiveInfo;
/// -----------------------------------------------------------------------
/// External calls
/// -----------------------------------------------------------------------
// transfer refund to recipient
key.rewardToken.transfer(key.refundRecipient, refundAmount);
emit ClaimRefund(msg.sender, incentiveId, refundAmount);
}
/// -----------------------------------------------------------------------
/// View functions
/// -----------------------------------------------------------------------
/// @notice Computes the current rewardPerToken value of an incentive.
/// @param key the incentive's key
/// @return the rewardPerToken value
function rewardPerToken(IncentiveKey calldata key)
external
view
returns (uint256)
{
uint256 lastTimeRewardApplicable = min(block.timestamp, key.endTime);
return
_rewardPerToken(
incentiveInfos[key.compute()],
lastTimeRewardApplicable
);
}
/// @notice Computes the amount of reward tokens a staker has accrued
/// from an incentive.
/// @param key the incentive's key
/// @param staker the staker's address
/// @return the amount of reward tokens accrued
function earned(IncentiveKey calldata key, address staker)
external
view
returns (uint256)
{
bytes32 incentiveId = key.compute();
uint256 lastTimeRewardApplicable = min(block.timestamp, key.endTime);
StakerInfo memory info = stakerInfos[incentiveId][staker];
return
_earned(
info,
_rewardPerToken(
incentiveInfos[key.compute()],
lastTimeRewardApplicable
)
);
}
function getMiningTime(IncentiveKey calldata key, address staker)
external
view
returns (uint256 time)
{
bytes32 incentiveId = key.compute();
StakerInfo memory info = stakerInfos[incentiveId][staker];
time =
info.numberOfStakedTokens *
(block.timestamp - info.startedStaking);
}
/// -----------------------------------------------------------------------
/// Owner functions
/// -----------------------------------------------------------------------
/// @notice Updates the protocol fee and/or the protocol fee recipient.
/// Only callable by the owner.
/// @param protocolFeeInfo_ The new protocol fee info
function ownerSetProtocolFee(ProtocolFeeInfo calldata protocolFeeInfo_)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
if (
protocolFeeInfo_.fee != 0 &&
protocolFeeInfo_.recipient == address(0)
) {
revert AsteroidMining_ProtocolFeeRecipientIsZero();
}
protocolFeeInfo = protocolFeeInfo_;
emit SetProtocolFee(protocolFeeInfo_);
}
/// -----------------------------------------------------------------------
/// Internal utilities
/// -----------------------------------------------------------------------
function _rewardPerToken(
IncentiveInfo memory info,
uint256 lastTimeRewardApplicable
) internal pure returns (uint256) {
if (info.numberOfStakedTokens == 0) {
return info.rewardPerTokenStored;
}
return
info.rewardPerTokenStored +
(((lastTimeRewardApplicable - info.lastUpdateTime) *
PRECISION *
info.rewardRatePerSecond) / info.numberOfStakedTokens);
}
function _earned(StakerInfo memory info, uint256 rewardPerToken_)
internal
pure
returns (uint256)
{
return
((info.numberOfStakedTokens *
(rewardPerToken_ - info.rewardPerTokenStored)) / PRECISION) +
info.totalRewardUnclaimed;
}
function _accrueRewards(
IncentiveKey calldata key,
StakerInfo memory stakerInfo,
IncentiveInfo memory incentiveInfo
) internal view returns (StakerInfo memory, IncentiveInfo memory) {
uint256 lastTimeRewardApplicable = min(block.timestamp, key.endTime);
uint256 rewardPerToken_ = _rewardPerToken(
incentiveInfo,
lastTimeRewardApplicable
);
if (incentiveInfo.numberOfStakedTokens == 0) {
// [lastUpdateTime, lastTimeRewardApplicable] was a period without any staked NFTs
// accrue refund
incentiveInfo.accruedRefund +=
incentiveInfo.rewardRatePerSecond *
(lastTimeRewardApplicable - incentiveInfo.lastUpdateTime);
}
incentiveInfo.rewardPerTokenStored = rewardPerToken_;
incentiveInfo.lastUpdateTime = lastTimeRewardApplicable.safeCastTo64();
stakerInfo.totalRewardUnclaimed = _earned(stakerInfo, rewardPerToken_)
.safeCastTo192();
stakerInfo.rewardPerTokenStored = rewardPerToken_;
return (stakerInfo, incentiveInfo);
}
}