-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.sol
422 lines (330 loc) · 11 KB
/
evaluator.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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
pragma experimental ABIEncoderV2;
import "./ERC20TD.sol";
import "./IExerciceSolution.sol";
import "./IAllInOneSolution.sol";
contract Evaluator
{
mapping(address => bool) public teachers;
ERC20TD TDERC20;
uint256[20] private randomSupplies;
string[20] private randomTickers;
uint public nextValueStoreRank;
mapping(address => string) public assignedTicker;
mapping(address => uint256) public assignedSupply;
mapping(address => mapping(uint256 => bool)) public exerciceProgression;
mapping(address => IExerciceSolution) public studentErc20;
mapping(address => uint256) public ex8Tier1AmountBought;
mapping(address => bool) public hasBeenPaired;
event newRandomTickerAndSupply(string ticker, uint256 supply);
event constructedCorrectly(address erc20Address);
constructor(ERC20TD _TDERC20)
public
{
TDERC20 = _TDERC20;
emit constructedCorrectly(address(TDERC20));
}
fallback () external payable
{}
receive () external payable
{}
function ex1_getTickerAndSupply()
public
{
assignedSupply[msg.sender] = randomSupplies[nextValueStoreRank]*1000000000000000000;
// assignedTicker[msg.sender] = bytes32ToString(randomTickers[nextValueStoreRank]);
assignedTicker[msg.sender] = randomTickers[nextValueStoreRank];
nextValueStoreRank += 1;
if (nextValueStoreRank >= 20)
{
nextValueStoreRank = 0;
}
// Crediting points
if (!exerciceProgression[msg.sender][1])
{
exerciceProgression[msg.sender][1] = true;
TDERC20.distributeTokens(msg.sender, 1);
}
}
function ex2_testErc20TickerAndSupply()
public
{
// Checking ticker and supply were received
require(exerciceProgression[msg.sender][1]);
// Checking exercice was submitted
require(exerciceProgression[msg.sender][0]);
// Checking ticker was set properly
require(_compareStrings(assignedTicker[msg.sender], studentErc20[msg.sender].symbol()), "Incorrect ticker");
// Checking supply was set properly
require(assignedSupply[msg.sender] == studentErc20[msg.sender].totalSupply(), "Incorrect supply");
// Checking some ERC20 functions were created
require(studentErc20[msg.sender].allowance(address(this), msg.sender) == 0, "Allowance not implemented or incorrectly set");
require(studentErc20[msg.sender].balanceOf(address(this)) == 0, "BalanceOf not implemented or incorrectly set");
require(studentErc20[msg.sender].approve(msg.sender, 10), "Approve not implemented");
// Crediting points
if (!exerciceProgression[msg.sender][2])
{
exerciceProgression[msg.sender][2] = true;
// Creating ERC20
TDERC20.distributeTokens(msg.sender, 2);
}
}
function ex3_testGetToken()
public
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
// Retrieving initial balance
uint256 initialBalance = studentErc20[msg.sender].balanceOf(address(this));
// Call getToken
studentErc20[msg.sender].getToken();
// Retrieving final balance
uint256 finalBalance = studentErc20[msg.sender].balanceOf(address(this));
require(initialBalance < finalBalance, "Token balance did not increase");
if (!exerciceProgression[msg.sender][3])
{
exerciceProgression[msg.sender][3] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 2);
}
}
function ex4_testBuyToken()
public
{
_testBuyToken();
if (!exerciceProgression[msg.sender][4])
{
exerciceProgression[msg.sender][4] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 2);
}
}
function ex5_testDenyListing()
public
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
require(!studentErc20[msg.sender].isCustomerWhiteListed(address(this)));
bool wasBuyAccepted = true;
try studentErc20[msg.sender].getToken() returns (bool v)
{
wasBuyAccepted = v;
}
catch
{
// This is executed in case revert() was used.
wasBuyAccepted = false;
}
require(!wasBuyAccepted);
if (!exerciceProgression[msg.sender][5])
{
exerciceProgression[msg.sender][5] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 1);
}
}
function ex6_testAllowListing()
public
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
// Checking ex5 was done
require(exerciceProgression[msg.sender][5]);
// Check if the current contract is whitelisted
require(studentErc20[msg.sender].isCustomerWhiteListed(address(this)));
// Trying to buy
ex3_testGetToken();
if (!exerciceProgression[msg.sender][6])
{
exerciceProgression[msg.sender][6] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 2);
}
}
function ex7_testDenyListing()
public
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
require(!studentErc20[msg.sender].isCustomerWhiteListed(address(this)));
require(studentErc20[msg.sender].customerTierLevel(address(this)) == 0);
bool wasBuyAccepted = true;
try studentErc20[msg.sender].buyToken({value: 0.0001 ether}) {
emit BuyTokenFailed(msg.sender);
}
catch (bool v)
{
wasBuyAccepted = v;
}
catch
{
// This is executed in case revert() was used.
wasBuyAccepted = false;
}
require(!wasBuyAccepted);
if (!exerciceProgression[msg.sender][7])
{
exerciceProgression[msg.sender][7] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 1);
}
}
function ex8_testTier1Listing()
public
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
// Checking ex7 was done
require(exerciceProgression[msg.sender][7]);
// Check if the current contract is whitelisted
require(studentErc20[msg.sender].isCustomerWhiteListed(address(this)));
// Check if the current contract has the correct tier level
require(studentErc20[msg.sender].customerTierLevel(address(this)) == 1);
// Trying to buy
ex8Tier1AmountBought[msg.sender] = _testBuyToken();
if (!exerciceProgression[msg.sender][8])
{
exerciceProgression[msg.sender][8] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 2);
}
}
function ex9_testTier2Listing()
public
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
// Checking ex7 was done
require(exerciceProgression[msg.sender][7]);
// Check if the current contract is whitelisted
require(studentErc20[msg.sender].isCustomerWhiteListed(address(this)));
// Check if the current contract has the correct tier level
require(studentErc20[msg.sender].customerTierLevel(address(this)) == 2);
// Trying to buy
uint256 tier2AmountBought = _testBuyToken();
// Checking that bought amount is twice what was bought before, for the same price
require(tier2AmountBought == 2 * ex8Tier1AmountBought[msg.sender]);
if (!exerciceProgression[msg.sender][9])
{
exerciceProgression[msg.sender][9] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 2);
}
}
function ex10_allInOne()
public
{
// Checking that solution has no token yet
uint256 initialBalance = TDERC20.balanceOf(msg.sender);
require(initialBalance == 0, "Solution should start with 0 points");
// Calling the solution so that it solves the workshop
IAllInOneSolution callerSolution = IAllInOneSolution(msg.sender);
callerSolution.completeWorkshop();
// Checking that at least 10 exercices where validated
uint256 finalBalance = TDERC20.balanceOf(msg.sender);
uint256 decimals = TDERC20.decimals();
require(finalBalance >= 10**decimals *18, "Solution should end with at least than 2 points");
if (!exerciceProgression[msg.sender][10])
{
exerciceProgression[msg.sender][10] = true;
// Distribute points
TDERC20.distributeTokens(msg.sender, 2);
}
}
/* Internal functions and modifiers */
modifier onlyTeachers()
{
require(TDERC20.teachers(msg.sender));
_;
}
function submitExercice(IExerciceSolution studentExercice)
public
{
// Checking this contract was not used by another group before
require(!hasBeenPaired[address(studentExercice)]);
// Assigning passed ERC20 as student ERC20
studentErc20[msg.sender] = studentExercice;
hasBeenPaired[address(studentExercice)] = true;
if (!exerciceProgression[msg.sender][0])
{
exerciceProgression[msg.sender][0] = true;
// Setup points
TDERC20.distributeTokens(msg.sender, 2);
// Creating contract points
TDERC20.distributeTokens(msg.sender, 2);
// Deploying contract points
TDERC20.distributeTokens(msg.sender, 1);
}
}
function _compareStrings(string memory a, string memory b)
internal
pure
returns (bool)
{
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}
function bytes32ToString(bytes32 _bytes32)
public
pure returns (string memory)
{
uint8 i = 0;
while(i < 32 && _bytes32[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}
function _testBuyToken()
internal
returns(uint256 firstBuyAmount)
{
// Checking ERC20 was created
require(address(studentErc20[msg.sender]) != address(0), "Student ERC20 not registered");
// Retrieving initial balance
uint256 initialBalance = studentErc20[msg.sender].balanceOf(address(this));
// Call buyToken
studentErc20[msg.sender].buyToken({value: 0.0001 ether});
// Retrieving intermediate balance
uint256 intermediateBalance = studentErc20[msg.sender].balanceOf(address(this));
require(initialBalance < intermediateBalance, "Token balance did not increase");
firstBuyAmount = intermediateBalance - initialBalance;
// Call buyToken again
studentErc20[msg.sender].buyToken({value: 0.0003 ether});
// Retrieving final balance
uint256 finalBalance = studentErc20[msg.sender].balanceOf(address(this));
require(intermediateBalance < finalBalance, "Token balance did not increase");
uint256 secondBuyAmount = finalBalance - intermediateBalance;
// Check that second buy amount was a different amount that first buy amount
require(secondBuyAmount > firstBuyAmount, "Second buy amount lower than first");
}
function readTicker(address studentAddres)
public
view
returns(string memory)
{
return assignedTicker[studentAddres];
}
function readSupply(address studentAddres)
public
view
returns(uint256)
{
return assignedSupply[studentAddres];
}
function setRandomTickersAndSupply(uint256[20] memory _randomSupplies, string[20] memory _randomTickers)
public
onlyTeachers
{
randomSupplies = _randomSupplies;
randomTickers = _randomTickers;
nextValueStoreRank = 0;
for (uint i = 0; i < 20; i++)
{
emit newRandomTickerAndSupply(randomTickers[i], randomSupplies[i]);
}
}
}