forked from Militereum/Militereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks.tests.pas
484 lines (457 loc) · 14.5 KB
/
checks.tests.pas
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
unit checks.tests;
interface
uses
// Delphi
DUnitX.TestFramework,
System.SysUtils,
// web3
web3;
type
[TestFixture]
TChecks = class
strict protected
procedure Execute(const proc: TProc<TProc, TProc<IError>>);
public
[Test]
procedure Step4;
[Test]
procedure Step5;
[Test]
procedure Step6;
[Test]
procedure Step7;
[Test]
procedure Step8;
[Test]
procedure Step9;
[Test]
procedure Step10;
[Test]
procedure Step11;
[Test]
procedure Step12;
[Test]
procedure Step13;
[Test]
procedure Step14;
[Test]
procedure Step15;
[Test]
procedure Step16;
[Test]
procedure Step17;
[Test]
procedure Step18;
end;
implementation
uses
// Delphi
System.Classes,
System.DateUtils,
System.JSON,
System.Math,
// web3
web3.coincap,
web3.defillama,
web3.eth.abi,
web3.eth.alchemy.api,
web3.eth.breadcrumbs,
web3.eth.chainlink,
web3.eth.etherscan,
web3.eth.simulate,
web3.eth.tokenlists,
web3.eth.types,
web3.json,
// project
cache,
common,
dextools,
moralis,
phisher,
vaults.fyi;
{$I keys/alchemy.api.key}
// executes an async test. the text is expected to call back into the 1st arg on success, otherwise the 2nd ags when an error occurred
procedure TChecks.Execute(const proc: TProc<TProc, TProc<IError>>);
const
TEST_TIMEOUT = 60000; // 60 seconds
TEST_INTERVAL = 100; // 0.1 second
begin
var done: Boolean := False;
var err : IError := nil;
proc(procedure
begin
done := True;
end, procedure(error: IError)
begin
err := error;
end);
var waited: UInt16 := 0;
while (err = nil) and (not done) and (waited < TEST_TIMEOUT) do
begin
TThread.Sleep(TEST_INTERVAL); waited := waited + TEST_INTERVAL;
end;
if Assigned(err) then Assert.Fail(err.Message) else if waited >= TEST_TIMEOUT then Assert.Fail('test timed out');
end;
// are we transacting with (a) smart contract and (b) verified with etherscan?
procedure TChecks.Step4;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
UNISWAP_V2_ROUTER: TAddress = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
begin
UNISWAP_V2_ROUTER.IsEOA(TWeb3.Create(common.Ethereum), procedure(isEOA: Boolean; err1: IError)
begin
if Assigned(err1) then
err(err1)
else if isEOA then
err(TError.Create('%s is an EOA, expected a smart contract', [UNISWAP_V2_ROUTER]))
else
common.Etherscan(common.Ethereum)
.ifErr(procedure(err2: IError)
begin
err(err2)
end)
.&else(procedure(etherscan: IEtherscan)
begin
etherscan.getContractSourceCode(UNISWAP_V2_ROUTER, procedure(src: string; err3: IError)
begin
if Assigned(err3) then
err(err3)
else if src <> '' then
ok
else
err(TError.Create('%s''s source code is null, expected non-empty string', [UNISWAP_V2_ROUTER]));
end);
end);
end);
end);
end;
// test DefiLlama's "current price of token by contract address" API
procedure TChecks.Step5;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
TETHER: TAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
begin
web3.defillama.coin(web3.Ethereum, TETHER, procedure(coin: ICoin; error: IError)
begin
if Assigned(error) then
err(error)
else if Round(coin.Price) = 1 then
ok
else
err(TError.Create('Tether''s price is %f, expected $1.00'))
end);
end);
end;
// test Chainlink and CoinCap price oracles
procedure TChecks.Step6;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
begin
const client: IWeb3 = TWeb3.Create(common.Ethereum);
web3.eth.chainlink.TAggregatorV3.Create(client, client.Chain.Chainlink).Price(procedure(price1: Double; err1: IError)
begin
if Assigned(err1) then
err(err1)
else
web3.coincap.price(string(client.chain.Symbol), procedure(price2: Double; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if (price2 > 0) and (price1 > 0) and (System.Math.Floor(price2 / 100) = System.Math.Floor(price1 / 100)) then
ok
else
err(TError.Create('CoinCap price is $%.2f, expected $%.2f (Chainlink)', [price2, price1]))
end);
end);
end);
end;
// test the MobyMask Phisher Registry
procedure TChecks.Step7;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
HOOLIGAN_BEAR: TAddress = '0x408cfD714C3bca3859650f6D85bAc1500620961e';
begin
phisher.isPhisher(HOOLIGAN_BEAR, procedure(result: Boolean; error: IError)
begin
if Assigned(error) then
err(error)
else if result then
ok
else
err(TError.Create('isPhisher(''eip155:1:%s'') returned false, expected true', [HOOLIGAN_BEAR]));
end);
end);
end;
// are we transacting with a spam contract or receiving spam tokens?
procedure TChecks.Step8;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
BORED_APE_NIKE_CLUB: TAddress = '0x000386E3F7559d9B6a2F5c46B4aD1A9587D59Dc3';
begin
web3.eth.alchemy.api.detect(ALCHEMY_API_KEY_ETHEREUM, web3.Ethereum, BORED_APE_NIKE_CLUB, [TContractType.Spam], procedure(contractType: TContractType; err1: IError)
begin
if (contractType = TContractType.Spam) and not Assigned(err1) then
ok
else
moralis.isPossibleSpam({$I keys/moralis.api.key}, web3.Ethereum, BORED_APE_NIKE_CLUB, procedure(spam: Boolean; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if spam then
ok
else
err(TError.Create('spam is false, expected true'));
end)
end);
end);
end;
// test etherscan's txlist
procedure TChecks.Step9;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
VITALIK_DOT_ETH = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
begin
common.Etherscan(web3.Ethereum)
.ifErr(procedure(err1: IError)
begin
err(err1);
end)
.&else(procedure(etherscan: IEtherscan)
begin
etherscan.getTransactions(VITALIK_DOT_ETH, procedure(txs: ITransactions; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if Assigned(txs) and (txs.Count > 0) then
ok
else
err(TError.Create('etherscan''s txlist returned 0 transactions, expected many more'));
end);
end);
end);
end;
// test Uniswap's unsupported tokens list
procedure TChecks.Step10;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
ZELDA_WHIRLWIND_CASH: TAddress = '0x249A198d59b57FDa5DDa90630FeBC86fd8c7594c';
begin
web3.eth.tokenlists.unsupported(web3.Ethereum, procedure(tokens: TTokens; error: IError)
begin
if Assigned(error) then
err(error)
else if tokens.IndexOf(ZELDA_WHIRLWIND_CASH) > -1 then
ok
else
err(TError.Create('%s is supported, expected unsupported', [ZELDA_WHIRLWIND_CASH]));
end);
end);
end;
// test the Breadcrumbs sanctioned address API
procedure TChecks.Step11;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
TORNADO_CASH: TAddress = '0x8589427373D6D84E98730D7795D8f6f8731FDA16';
begin
web3.eth.breadcrumbs.sanctioned({$I keys/breadcrumbs.api.key}, web3.Ethereum, TORNADO_CASH, procedure(value: Boolean; error: IError)
begin
if Assigned(error) then
err(error)
else if value then
ok
else
err(TError.Create('%s is not sanctioned, expected sanctioned', [TORNADO_CASH]));
end);
end);
end;
// are we receiving (or otherwise transacting with) a low-DEX-score token?
procedure TChecks.Step12;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
RED_EYED_FROG: TAddress = '0x4DB5C8875ef00ce8040A9685581fF75C3c61aDC8';
begin
dextools.score({$I keys/dextools.api.key}, web3.Ethereum, RED_EYED_FROG, procedure(score1: Integer; err1: IError)
begin
if (score1 > 0) and not Assigned(err1) then
ok
else
moralis.securityScore({$I keys/moralis.api.key}, web3.Ethereum, RED_EYED_FROG, procedure(score2: Integer; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if score2 > 0 then
ok
else
err(TError.Create('score is 0, expected value between 1 and 100'));
end);
end);
end);
end;
// are we receiving (or otherwise transacting with) a token without a DEX pair?
procedure TChecks.Step13;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
BETHEREUM: TAddress = '0x14C926F2290044B647e1Bf2072e67B495eff1905';
begin
dextools.pairs({$I keys/dextools.api.key}, web3.Ethereum, BETHEREUM, procedure(arr1: TJsonArray; err1: IError)
begin
if Assigned(arr1) and not Assigned(err1) then
ok
else
moralis.pairs({$I keys/moralis.api.key}, web3.Ethereum, BETHEREUM, procedure(arr2: TJsonArray; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if Assigned(arr2) then
ok
else
err(TError.Create('pairs is nil, expected []'));
end);
end);
end);
end;
// test etherscan's getabi
procedure TChecks.Step14;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
TETHER: TAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
begin
cache.getContractABI(web3.Ethereum, TETHER, procedure(abi: IContractABI; error: IError)
begin
if Assigned(error) then
err(error)
else
if (function(const abi: IContractABI): Boolean // returns True if censorable, otherwise False
begin
if Assigned(abi) then for var I := 0 to Pred(abi.Count) do
if (abi.Item(I).SymbolType = TSymbolType.Function) and (System.Pos('blacklist', abi.Item(I).Name.ToLower) > 0) then
begin
Result := True;
EXIT;
end;
Result := False;
end)(abi) then
ok
else
if (function(const abi: IContractABI): Boolean // returns True if pausable, otherwise False
begin
if Assigned(abi) then for var I := 0 to Pred(abi.Count) do
if (abi.Item(I).SymbolType = TSymbolType.Function) and (System.Pos('pause', abi.Item(I).Name.ToLower) > 0) then
begin
Result := True;
EXIT;
end;
Result := False;
end)(abi) then
ok
else
err(TError.Create('Tether is neither censorable nor pausable, expected to have a blacklist'));
end);
end);
end;
// test for honeypot token on Sepolia
procedure TChecks.Step15;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
{$I keys/tenderly.api.key}
const
OWNER : TAddress = '0x7033A74F69a49652A51ec1c5B6f952e420795C86'; // deployer/owner (is allowed to transfer)
OTHER : TAddress = '0x81B4a9f9Ab3b55Bf224407A3046b82BDFB32Af4d'; // brandly.eth (not allowed to transfer)
HONEYPOT: TAddress = '0xdd8c2c0b62f1644ee1c7e67789dab758ba0e798b';
begin
// step 1: check if anyone (other than the owner) can transfer the token after a mint
web3.eth.simulate.honeypots(ALCHEMY_API_KEY_ETHEREUM, TENDERLY_ACCOUNT_ID, TENDERLY_PROJECT_ID, TENDERLY_ACCESS_KEY, web3.Sepolia, OTHER, HONEYPOT, 0, web3.eth.abi.encode('mint(uint256)', [1000000000000000000]), procedure(honeypots1: IAssetChanges; err1: IError)
begin
if Assigned(err1) then
err(err1)
else if (honeypots1 = nil) or (honeypots1.Count = 0) then
err(TError.Create('%s is a honeypot token and should be detected as such', [HONEYPOT]))
else
// step 2: now that we have confirmed the token to be a honeypot, double-check our logic and verify the owner *is* allowed to transfer
web3.eth.simulate.honeypots(ALCHEMY_API_KEY_ETHEREUM, TENDERLY_ACCOUNT_ID, TENDERLY_PROJECT_ID, TENDERLY_ACCESS_KEY, web3.Sepolia, OWNER, HONEYPOT, 0, web3.eth.abi.encode('mint(uint256)', [1000000000000000000]), procedure(honeypots2: IAssetChanges; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if (honeypots2 = nil) or (honeypots2.Count = 0) then
ok
else
err(TError.Create('owner %s should be allowed to transfer token %s', [OWNER, HONEYPOT]));
end);
end);
end);
end;
// test for a dormant smart contract
procedure TChecks.Step16;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
begin
common.Etherscan(web3.Ethereum)
.ifErr(procedure(err1: IError)
begin
err(err1)
end)
.&else(procedure(etherscan: IEtherscan)
const
POS_DUMMY_STATE_SENDER: TAddress = '0x53e0bca35ec356bd5dddfebbd1fc0fd03fabad39';
begin
etherscan.getLatestTransaction(POS_DUMMY_STATE_SENDER, procedure(latest: ITransaction; err2: IError)
begin
if Assigned(err2) then
err(err2)
else if Assigned(latest) and (DaysBetween(System.SysUtils.Now, UnixToDateTime(latest.timeStamp, False)) < 30) then
err(TError.Create('%s had a transcation less than 30 days ago, expected the smart contract to be dormant', [POS_DUMMY_STATE_SENDER]))
else
ok
end);
end);
end);
end;
// are we receiving (or otherwise transacting with) a token with an unlock event coming up?
procedure TChecks.Step17;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
UNI: TAddress = '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984';
begin
dextools.unlock({$I keys/dextools.api.key}, web3.Ethereum, UNI, procedure(next: TDateTime; error: IError)
begin
if Assigned(error) then
err(error)
else if next > 0 then
ok
else
err(TError.Create('UNI''s next unlock date is zero, expected some future date'));
end);
end);
end;
// test the vaults.fyi API
procedure TChecks.Step18;
begin
Self.Execute(procedure(ok: TProc; err: TProc<IError>)
const
YEARN_V2_USDC: TAddress = '0xa354F35829Ae975e850e23e9615b11Da1B3dC4DE';
begin
vaults.fyi.better(web3.Ethereum, YEARN_V2_USDC, procedure(other: IVault; error: IError)
begin
if Assigned(error) then
err(error)
else if Assigned(other) then
ok
else
err(TError.Create('vault is nil, expected better than Yearn v2 USDC'))
end);
end);
end;
initialization
TDUnitX.RegisterTestFixture(TChecks);
end.