-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathL2BaseTest.sol
336 lines (279 loc) · 13.6 KB
/
L2BaseTest.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
import "forge-std/Test.sol";
import "forge-std/console.sol";
import { GemFactory } from "../../src/L2/GemFactory.sol";
import { GemFactoryForging } from "../../src/L2/GemFactoryForging.sol";
import { GemFactoryMining } from "../../src/L2/GemFactoryMining.sol";
import { GemFactoryProxy } from "../../src/L2/GemFactoryProxy.sol";
import { Treasury } from "../../src/L2/Treasury.sol";
import { TreasuryProxy } from "../../src/L2/TreasuryProxy.sol";
import { MarketPlace } from "../../src/L2/MarketPlace.sol";
import { MarketPlaceProxy } from "../../src/L2/MarketPlaceProxy.sol";
import { RandomPack } from "../../src/L2/RandomPack.sol";
import { RandomPackProxy } from "../../src/L2/RandomPackProxy.sol";
import { L2StandardERC20 } from "../../src/L2/L2StandardERC20.sol";
import { MockTON } from "../../src/L2/Mock/MockTON.sol";
import { GemFactoryStorage } from "../../src/L2/GemFactoryStorage.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { DRBCoordinatorMock } from "../../src/L2/Mock/DRBCoordinatorMock.sol";
import { DRBConsumerBase } from "../../src/L2/Randomness/DRBConsumerBase.sol";
import { Airdrop } from "../../src/L2/Airdrop.sol";
import { AirdropProxy } from "../../src/L2/AirdropProxy.sol";
import {IDRBCoordinator} from "../../src/interfaces/IDRBCoordinator.sol";
contract L2BaseTest is Test {
using SafeERC20 for IERC20;
uint8 public rareminingTry = 2;
uint8 public uniqueminingTry = 1; // for testing purpose
uint8 public epicminingTry = 10;
uint8 public LegendaryminingTry = 15;
uint8 public MythicminingTry = 20;
uint256 public tonFeesRate = 10; // 10%
uint256 public miningFees = 0.01 ether;
uint256 public CommonGemsValue = 10 * 10 ** 27;
uint256 public RareGemsValue = 19 * 10 ** 27;
uint256 public UniqueGemsValue = 53 * 10 ** 27;
uint256 public EpicGemsValue = 204 * 10 ** 27;
uint256 public LegendaryGemsValue = 605 * 10 ** 27;
uint256 public MythicGemsValue = 4000 * 10 ** 27;
uint32 public RareGemsMiningPeriod = 2 weeks;
uint32 public UniqueGemsMiningPeriod = 3 weeks;
uint32 public EpicGemsMiningPeriod = 4 weeks;
uint32 public LegendaryGemsMiningPeriod = 5 weeks;
uint32 public MythicGemsMiningPeriod = 6 weeks;
uint32 public RareGemsCooldownPeriod = 2 weeks;
uint32 public UniqueGemsCooldownPeriod = 3 weeks;
uint32 public EpicGemsCooldownPeriod = 4 weeks;
uint32 public LegendaryGemsCooldownPeriod = 5 weeks;
uint32 public MythicGemsCooldownPeriod = 6 weeks;
address payable owner;
address payable user1;
address payable user2;
address payable user3;
GemFactory gemfactory;
GemFactoryForging gemfactoryforging;
GemFactoryMining gemfactorymining;
GemFactoryProxy gemfactoryProxy;
address gemfactoryProxyAddress;
Treasury treasury;
TreasuryProxy treasuryProxy;
address payable treasuryProxyAddress;
MarketPlace marketplace;
MarketPlaceProxy marketplaceProxy;
address marketplaceProxyAddress;
DRBCoordinatorMock drbCoordinatorMock;
Airdrop airdrop;
AirdropProxy airdropProxy;
address airdropProxyAddress;
RandomPack randomPack;
RandomPackProxy randomPackProxy;
address payable randomPackProxyAddress;
address wston;
address ton;
address l1wston;
address l1ton;
address l2bridge;
//DRB storage
uint256 public avgL2GasUsed = 2100000;
uint256 public premiumPercentage = 0;
uint256 public flatFee = 0.001 ether;
uint256 public calldataSizeBytes = 2071;
//random pack storage
uint256 randomPackFees = 40*10**18;
uint256 randomBeaconFees = 0.005 ether;
event CommonGemMinted();
event DebugSelector(bytes4 forgeTokensSelector);
function setUp() public virtual {
owner = payable(makeAddr("Owner"));
user1 = payable(makeAddr("User1"));
user2 = payable(makeAddr("User2"));
user3 = payable(makeAddr("User3"));
vm.startPrank(owner);
vm.warp(1632934800);
wston = address(new L2StandardERC20(l2bridge, l1wston, "Wrapped Ston", "WSTON")); // 27 decimals
ton = address(new MockTON(l2bridge, l1ton, "Ton", "TON")); // 18 decimals
vm.stopPrank();
// mint some tokens to User1, user2 and user3
vm.startPrank(l2bridge);
L2StandardERC20(wston).mint(owner, 1000000 * 10 ** 27);
L2StandardERC20(wston).mint(user1, 100000 * 10 ** 27);
L2StandardERC20(wston).mint(user2, 100000 * 10 ** 27);
L2StandardERC20(wston).mint(user3, 100000 * 10 ** 27);
MockTON(ton).mint(user1, 1000000 * 10 ** 18);
MockTON(ton).mint(user1, 100000 * 10 ** 18);
MockTON(ton).mint(user2, 100000 * 10 ** 18);
MockTON(ton).mint(user3, 100000 * 10 ** 18);
vm.stopPrank();
vm.startPrank(owner);
// give ETH to User1 and User2 to cover gasFees associated with using VRF functions as well as interacting with thanos
vm.deal(user1, 1000000 ether);
vm.deal(user2, 1000000 ether);
// deploy DRBCoordinatorMock
drbCoordinatorMock = new DRBCoordinatorMock(
avgL2GasUsed,
premiumPercentage,
flatFee,
calldataSizeBytes
);
// --------------------------- GEM FACTORY DEPLOYMENT -------------------------------------------------
// deploy GemFactory
gemfactory = new GemFactory();
gemfactoryforging = new GemFactoryForging();
gemfactorymining = new GemFactoryMining();
gemfactoryProxy = new GemFactoryProxy();
gemfactoryProxy.upgradeTo(address(gemfactory));
// we set the forging implementation under the proxy
gemfactoryProxy.setImplementation2(address(gemfactoryforging), 1, true);
// we set the mining implementation under the proxy
gemfactoryProxy.setImplementation2(address(gemfactorymining), 2, true);
// Compute the function selector for GemFactoryForging
bytes4 forgeTokensSelector = bytes4(keccak256("forgeTokens(uint256[],uint8,uint8[2])"));
// Create a dynamic array for the selector
bytes4[] memory forgingSelectors = new bytes4[](1);
forgingSelectors[0] = forgeTokensSelector;
// Map the forgeTokens function to the GemFactoryForging implementation
gemfactoryProxy.setSelectorImplementations2(forgingSelectors, address(gemfactoryforging));
// Compute the function selector for GemFactoryMining
bytes4 startMiningSelector = bytes4(keccak256("startMiningGEM(uint256)"));
bytes4 cancelMiningSelector = bytes4(keccak256("cancelMining(uint256)"));
bytes4 pickMinedGEMSelector = bytes4(keccak256("pickMinedGEM(uint256)"));
bytes4 drbInitializeSelector = bytes4(keccak256("DRBInitialize(address)"));
bytes4 rawFulfillRandomWordsSelector = bytes4(keccak256("rawFulfillRandomWords(uint256,uint256)"));
// Create a dynamic array for the selector
bytes4[] memory miningSelector = new bytes4[](5);
miningSelector[0] = startMiningSelector;
miningSelector[1] = cancelMiningSelector;
miningSelector[2] = pickMinedGEMSelector;
miningSelector[3] = drbInitializeSelector;
miningSelector[4] = rawFulfillRandomWordsSelector;
// Map the mining functions to the GemFactoryMining implementation
gemfactoryProxy.setSelectorImplementations2(miningSelector, address(gemfactorymining));
// Debugging: Verify the mapping
address forgeTokenslementation = gemfactoryProxy.getSelectorImplementation2(forgeTokensSelector);
require(forgeTokenslementation == address(gemfactoryforging), "Selector not mapped to GemFactoryForging");
address startMiningImpl = gemfactoryProxy.getSelectorImplementation2(startMiningSelector);
require(startMiningImpl == address(gemfactorymining), "Selector not mapped to GemFactoryMining");
gemfactoryProxyAddress = address(gemfactoryProxy);
// ------------------------------- TREASURY DEPLOYMENT -------------------------------------------------
// deploy and initialize treasury
treasury = new Treasury();
treasuryProxy = new TreasuryProxy();
treasuryProxy.upgradeTo(address(treasury));
treasuryProxyAddress = payable(address(treasuryProxy));
Treasury(treasuryProxyAddress).initialize(
wston,
ton,
gemfactoryProxyAddress
);
// deploy and initialize marketplace
marketplace = new MarketPlace();
marketplaceProxy = new MarketPlaceProxy();
marketplaceProxy.upgradeTo(address(marketplace));
marketplaceProxyAddress = address(marketplaceProxy);
MarketPlace(marketplaceProxyAddress).initialize(
treasuryProxyAddress,
gemfactoryProxyAddress,
tonFeesRate,
wston,
ton
);
vm.stopPrank();
// mint some TON & WSTON to treasury
vm.startPrank(l2bridge);
L2StandardERC20(wston).mint(treasuryProxyAddress, 100000 * 10 ** 27);
MockTON(ton).mint(treasuryProxyAddress, 100000 * 10 ** 18);
vm.stopPrank();
vm.startPrank(owner);
// initialize gemfactory with newly created contract addreses
GemFactory(gemfactoryProxyAddress).initialize(
owner,
wston,
ton,
treasuryProxyAddress
);
GemFactoryMining(gemfactoryProxyAddress).DRBInitialize(
address(drbCoordinatorMock)
);
GemFactory(gemfactoryProxyAddress).setGemsValue(
CommonGemsValue,
RareGemsValue,
UniqueGemsValue,
EpicGemsValue,
LegendaryGemsValue,
MythicGemsValue
);
GemFactory(gemfactoryProxyAddress).setGemsMiningPeriods(
RareGemsMiningPeriod,
UniqueGemsMiningPeriod,
EpicGemsMiningPeriod,
LegendaryGemsMiningPeriod,
MythicGemsMiningPeriod
);
GemFactory(gemfactoryProxyAddress).setGemsCooldownPeriods(
RareGemsCooldownPeriod,
UniqueGemsCooldownPeriod,
EpicGemsCooldownPeriod,
LegendaryGemsCooldownPeriod,
MythicGemsCooldownPeriod
);
// Set mining tries
GemFactory(gemfactoryProxyAddress).setMiningTries(
rareminingTry,
uniqueminingTry,
epicminingTry,
LegendaryminingTry,
MythicminingTry
);
// set the MarketPlace contract address into the GemFactory contract
GemFactory(gemfactoryProxyAddress).setMarketPlaceAddress(marketplaceProxyAddress);
Treasury(treasuryProxyAddress).setMarketPlace(marketplaceProxyAddress);
// We deploy the RandomPack contract
randomPack = new RandomPack();
randomPackProxy = new RandomPackProxy();
randomPackProxy.upgradeTo(address(randomPack));
randomPackProxyAddress = payable(address(randomPackProxy));
RandomPack(randomPackProxyAddress).initialize(
address(drbCoordinatorMock),
ton,
gemfactoryProxyAddress,
treasuryProxyAddress,
randomPackFees
);
// Set randomPack address into the treasury contract
Treasury(treasuryProxyAddress).setRandomPack(randomPackProxyAddress);
// we set up the list of colors available for the GEM
GemFactory(gemfactoryProxyAddress).addColor("Ruby",0,0,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Amber/Topaz",1,2,0,148,255,0,148,255);
GemFactory(gemfactoryProxyAddress).addColor("Ruby/Amber",0,1,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Amber",1,1,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Topaz",2,2,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Topaz/Emerald",2,3,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Emerald/Topaz",3,2,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Emerald",3,3,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Emerald/Amber",3,1,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Turquoise",4,4,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Sapphire",5,5,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Amethyst",6,6,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Amethyst/Amber",6,1,255,255,255,255,0,0);
GemFactory(gemfactoryProxyAddress).addColor("Garnet",7,7,255,255,255,255,0,0);
//deploying and initializing the airdrop contract
airdrop = new Airdrop();
airdropProxy = new AirdropProxy();
airdropProxy.upgradeTo(address(airdrop));
airdropProxyAddress = address(airdropProxy);
Airdrop(airdropProxyAddress).initialize(treasuryProxyAddress, gemfactoryProxyAddress);
// set the airdrop address in treasury and gemfactory
Treasury(treasuryProxyAddress).setAirdrop(airdropProxyAddress);
GemFactory(gemfactoryProxyAddress).setAirdrop(airdropProxyAddress);
vm.stopPrank();
}
function testSetup() public view {
address wstonAddress = GemFactory(gemfactoryProxyAddress).getWstonAddress();
assert(wstonAddress == address(wston));
address tonAddress = GemFactory(gemfactoryProxyAddress).getTonAddress();
assert(tonAddress == address(ton));
address treasuryAddress = GemFactory(gemfactoryProxyAddress).getTreasuryAddress();
assert(treasuryAddress == treasuryProxyAddress);
}
}