-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceRatsProject.t.sol
249 lines (198 loc) · 7.74 KB
/
SpaceRatsProject.t.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import {SpaceRats} from "../src/SpaceRats.sol";
import {IridiumToken} from "../src/IridiumToken.sol";
import {Geode} from "../src/Geode.sol";
import {AsteroidMining} from "../src/staking/AsteroidMining.sol";
import {ProcessingPlant} from "../src/ProcessingPlant.sol";
import {MockVRFCoordinatorV2} from "chainlink-foundry/test/mocks/MockVRFCoordinatorV2.sol";
import {LinkToken} from "chainlink-foundry/test/mocks/LinkToken.sol";
import {IncentiveId} from "../src/staking/IncentiveId.sol";
import "../src/staking/Structs.sol";
contract SpaceRatsProjectTest is Test {
// Contracts
SpaceRats public spaceRats;
IridiumToken public iridium;
Geode public geode;
AsteroidMining public asteroidMining;
ProcessingPlant public plant;
LinkToken public linkToken;
MockVRFCoordinatorV2 public vrfCoordinator;
// Addresses
address multiSig = makeAddr("Space Rats Multi Sig");
address alice = makeAddr("Alice");
address bob = makeAddr("Bob");
// Space Rats mint details
uint256 internal constant COLLECTION_SIZE = 2000;
uint256 internal constant MAX_BATCH_SIZE = 5;
uint256 internal constant AMOUNT_FOR_WHITELIST = 1000;
uint256 internal constant AMOUNT_FOR_PUBLIC = 1000;
uint32 internal constant PUBLIC_SALE_START_TIME = 15_000_000;
uint64 internal constant WHITELIST_PRICE = 1 ether;
uint64 internal constant PUBLIC_PRICE = 2 ether;
uint32 internal constant PUBLIC_SALE_KEY = 1_000;
// Asteroid Mining variables
uint256 constant INCENTIVE_LENGTH = 30 days;
uint256 constant INCENTIVE_AMOUNT = 1000 ether;
uint8 constant PROTOCOL_FEE = 10; // 1%
uint256 constant GEODE_MINING_TIME = 21 days;
uint256 constant BOND = 0.05 ether;
uint256 constant MAX_ERROR_PERCENT = 1e9; // 10**-9
uint256 constant INCENTIVE_AMOUNT_AFTER_FEE =
(INCENTIVE_AMOUNT * (1000 - PROTOCOL_FEE)) / 1000;
IncentiveKey key;
// ProcessingPlant chainlink VRF V2 variables
uint96 internal constant FUND_AMOUNT = 1_000 ether;
uint256 internal constant IRIDIUM_REWARD_AMOUNT = 10e18;
uint64 subId;
bytes32 keyHash;
function setUp() public {
startHoax(multiSig, multiSig);
// deploy SpaceRats
spaceRats = new SpaceRats(
MAX_BATCH_SIZE,
COLLECTION_SIZE,
COLLECTION_SIZE,
AMOUNT_FOR_WHITELIST
);
// deploy Iridium
iridium = new IridiumToken();
// deploy Geode
geode = new Geode("");
// deploy MockVRFCoordinatorV2: setup subId and fund it
vrfCoordinator = new MockVRFCoordinatorV2();
subId = vrfCoordinator.createSubscription();
vrfCoordinator.fundSubscription(subId, FUND_AMOUNT);
// deploy ProcessingPlant
plant = new ProcessingPlant(
iridium,
geode,
subId,
address(vrfCoordinator),
address(linkToken),
keyHash
);
// deploy AsteroidMining
asteroidMining = new AsteroidMining(
ProtocolFeeInfo({recipient: multiSig, fee: PROTOCOL_FEE})
);
// setup SpaceRats mint
spaceRats.setPublicSaleKey(PUBLIC_SALE_KEY);
spaceRats.setupSaleInfo(
PUBLIC_SALE_START_TIME,
WHITELIST_PRICE,
PUBLIC_PRICE,
PUBLIC_SALE_KEY
);
// 10 whitelist spots to alice and bob
spaceRats.addToWhitelist(alice, 10);
spaceRats.addToWhitelist(bob, 10);
// assign roles
geode.grantRole(geode.MINTER_ROLE(), address(asteroidMining));
geode.grantRole(geode.BURNER_ROLE(), address(plant));
iridium.grantRole(iridium.MINTER_ROLE(), multiSig); // for creating incentives
iridium.grantRole(iridium.MINTER_ROLE(), address(asteroidMining));
iridium.grantRole(iridium.MINTER_ROLE(), address(plant));
// set up AsteroidMining
iridium.mint(multiSig, INCENTIVE_AMOUNT);
iridium.approve(address(asteroidMining), type(uint256).max); // max approval for testing
vm.stopPrank();
// whitelist mint for alice and bob
startHoax(alice, alice);
spaceRats.whitelistMint{value: WHITELIST_PRICE}(); //tokenId 0
spaceRats.whitelistMint{value: WHITELIST_PRICE}(); //tokenId 1
spaceRats.whitelistMint{value: WHITELIST_PRICE}(); //tokenId 2
assertEq(spaceRats.balanceOf(alice), 3);
vm.stopPrank();
startHoax(bob, bob);
spaceRats.whitelistMint{value: WHITELIST_PRICE}(); //tokenId 3
assertEq(spaceRats.balanceOf(bob), 1);
vm.stopPrank();
startHoax(multiSig, multiSig);
// AsteroidMining: create incentive
key = IncentiveKey({
nft: spaceRats,
rewardToken: iridium,
rewardNft: geode,
startTime: block.timestamp,
endTime: block.timestamp + INCENTIVE_LENGTH,
bondAmount: BOND,
refundRecipient: multiSig
});
asteroidMining.createIncentive(
key,
INCENTIVE_AMOUNT,
GEODE_MINING_TIME
);
// set up ProcessingPlant
plant.setIridiumRewardAmount(IRIDIUM_REWARD_AMOUNT);
vrfCoordinator.addConsumer(subId, address(plant));
vm.stopPrank();
}
function test_stakeWaitClaimRewards() public {
startHoax(alice, alice);
uint256 beforeBalance = alice.balance;
asteroidMining.stake{value: BOND}(key, 0);
skip(INCENTIVE_LENGTH + 1 days);
// verify reward amount
assertApproxEqRel(
asteroidMining.earned(key, alice),
INCENTIVE_AMOUNT_AFTER_FEE,
MAX_ERROR_PERCENT,
"alice reward amount incorrect"
);
asteroidMining.claimRewards(key, alice);
assertEq(geode.balanceOf(alice, 0), 1, "Alice geode balance not 1");
assertApproxEqRel(
iridium.balanceOf(alice),
INCENTIVE_AMOUNT_AFTER_FEE,
MAX_ERROR_PERCENT,
"Alice iridium balance incorrect"
);
}
function test_stakeToCrackGeodes() public {
startHoax(alice, alice);
uint256 iridiumBefore = iridium.balanceOf(alice);
asteroidMining.stake{value: BOND}(key, 0);
skip(INCENTIVE_LENGTH + 1 days);
asteroidMining.claimRewards(key, alice);
// set approval of geode for plant
geode.setApprovalForAll(address(plant), true);
uint256 tokenId = 0;
assertEq(
geode.balanceOf(alice, tokenId),
1,
"Alice geode balance not 1"
);
plant.processGeode(tokenId);
vm.stopPrank();
uint256 processRound = plant.processRound();
// skip to the end of the processing round
skip(4 days);
startHoax(multiSig, multiSig);
uint256 requestId = plant.requestRandomness(processRound);
vrfCoordinator.fulfillRandomWords(requestId, address(plant));
plant.crackGeodes(processRound);
// verify iridium rewards from asteroidMining and cracking a geode
assertApproxEqRel(
iridium.balanceOf(alice) - iridiumBefore,
INCENTIVE_AMOUNT_AFTER_FEE + plant.iridiumRewards(),
MAX_ERROR_PERCENT,
"iridium rewards incorrect"
);
// verify whitelist spot
assertEq(
plant.exercisableWhitelistSpots(alice),
0,
"random word not 100: whitelist spot not 0"
);
// verify geode balances
assertEq(geode.balanceOf(alice, 0), 0, "Alice geode balance not 0");
assertEq(
geode.balanceOf(address(plant), 0),
0,
"ProcessingPlant geode balance not 0"
);
}
}