forked from Militereum/Militereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks.pas
1342 lines (1287 loc) · 47.2 KB
/
checks.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
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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit checks;
interface
uses
// Delphi
System.SysUtils,
// Indy
IdGlobal,
// web3
web3,
// project
base,
server,
transaction;
type
TWarning = (Approve, TransferOut, Other);
type
TPrompted = set of TWarning;
type
TDone = reference to procedure(const prompted: TPrompted);
TNext = reference to procedure(const prompted: TPrompted; const err: IError);
type
TStep = procedure(const prompted: TPrompted; const next: TNext) of object;
TSteps = array of TStep;
type
TComment = class(TCustomAttribute)
strict private
FValue: string;
public
constructor Create(const aValue: string);
property Value: string read FValue;
end;
type
TApproval = record
strict private
FChain : TChain;
FToken : TAddress;
FOwner : TAddress;
FSpender: TAddress;
public
constructor Create(const aChain: TChain; const aToken, aOwner, aSpender: TAddress);
procedure Active(const callback: TProc<Boolean, IError>);
property Chain : TChain read FChain;
property Token : TAddress read FToken;
property Owner : TAddress read FOwner;
property Spender: TAddress read FSpender;
end;
type
TCustomChecks = class(TObject)
private
FApproved: TArray<TApproval>;
public
property Approved: TArray<TApproval> read FApproved;
end;
type
TChecks = class(TCustomChecks)
strict private
server: TEthereumRPCServer;
port : TIdPort;
chain : TChain;
tx : transaction.ITransaction;
block : TDone;
log : TLog;
public
constructor Create(const server: TEthereumRPCServer; const port: TIdPort; const chain: TChain; const tx: transaction.ITransaction; const block: TDone; const log: TLog);
[TComment('approve(address,uint256)')]
procedure Step1(const prompted: TPrompted; const next: TNext);
[TComment('transfer(address,uint256)')]
procedure Step2(const prompted: TPrompted; const next: TNext);
[TComment('setApprovalForAll(address,bool)')]
procedure Step3(const prompted: TPrompted; const next: TNext);
[TComment('are we transacting with (a) smart contract and (b) not verified with etherscan?')]
procedure Step4(const prompted: TPrompted; const next: TNext);
[TComment('are we transferring more than $5k in ERC-20, translated to USD?')]
procedure Step5(const prompted: TPrompted; const next: TNext);
[TComment('are we sending more than $5k in ETH, translated to USD?')]
procedure Step6(const prompted: TPrompted; const next: TNext);
[TComment('are we transacting with a contract that has been identified as a phisher in the MobyMask Phisher Registry?')]
procedure Step7(const prompted: TPrompted; const next: TNext);
[TComment('are we transacting with a spam contract or receiving spam tokens?')]
procedure Step8(const prompted: TPrompted; const next: TNext);
[TComment('have we transacted with this address before?')]
procedure Step9(const prompted: TPrompted; const next: TNext);
[TComment('are we transacting with an unsupported contract or receiving unsupported tokens?')]
procedure Step10(const prompted: TPrompted; const next: TNext);
[TComment('are we transacting with a sanctioned address?')]
procedure Step11(const prompted: TPrompted; const next: TNext);
[TComment('are we receiving (or otherwise transacting with) a low-DEX-score token?')]
procedure Step12(const prompted: TPrompted; const next: TNext);
[TComment('are we receiving (or otherwise transacting with) a token without a DEX pair?')]
procedure Step13(const prompted: TPrompted; const next: TNext);
[TComment('are we receiving (or otherwise transacting with) a censorable token that can blacklist you?')]
procedure Step14(const prompted: TPrompted; const next: TNext);
[TComment('are we buying a honeypot token?')]
procedure Step15(const prompted: TPrompted; const next: TNext);
[TComment('are we receiving (or otherwise transacting with) a dormant token/contract?')]
procedure Step16(const prompted: TPrompted; const next: TNext);
[TComment('are we receiving (or otherwise transacting with) a token with an unlock event coming up?')]
procedure Step17(const prompted: TPrompted; const next: TNext);
[TComment('are we depositing into a vault, but is there another vault with higher APY (while having the same TVL or more)?')]
procedure Step18(const prompted: TPrompted; const next: TNext);
[TComment('simulate transaction, prompt for each and every token (a) getting approved, or (b) leaving your wallet')]
procedure Step19(const prompted: TPrompted; const next: TNext);
[TComment('include this step if you want the transaction to fail')]
procedure Fail(const prompted: TPrompted; const next: TNext);
end;
implementation
uses
// Delphi
System.DateUtils,
System.JSON,
System.Math,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3.defillama,
web3.eth.alchemy.api,
web3.eth.breadcrumbs,
web3.eth.erc20,
web3.eth.etherscan,
web3.eth.simulate,
web3.eth.tokenlists,
web3.eth.types,
web3.eth.utils,
web3.utils,
// project
airdrop,
asset,
cache,
censorable,
common,
dextools,
dormant,
error,
firsttime,
honeypot,
limit,
lowDexScore,
moralis,
noDexPair,
pausable,
phisher,
sanctioned,
setApprovalForAll,
spam,
thread,
unlock,
unsupported,
unverified,
vault,
vaults.fyi;
{------- spender status (EOA, unverified, phisher, sanctioned, or good --------}
procedure getSpenderStatus(const chain: TChain; const address: TAddress; const callback: TProc<TSpenderStatus, IError>);
type
TStep = reference to procedure(const callback: TProc<Boolean, IError>);
TNext = reference to procedure(const steps: TArray<TStep>; const index: Integer);
begin
var next: TNext;
// is the address an EOA? (there is never a good reason to approve an EOA)
const step1: TStep = procedure(const callback: TProc<Boolean, IError>)
begin
address.IsEOA(TWeb3.Create(chain), callback);
end;
// is the contract not verified with etherscan?
const step2: TStep = procedure(const callback: TProc<Boolean, IError>)
begin
common.Etherscan(chain)
.ifErr(procedure(err: IError)
begin
callback(False, err)
end)
.&else(procedure(etherscan: IEtherscan)
begin
etherscan.getContractSourceCode(address, procedure(src: string; err: IError)
begin
callback(src = '', err);
end)
end);
end;
// is the address a known phisher?
const step3: TStep = procedure(const callback: TProc<Boolean, IError>)
begin
phisher.isPhisher(address, callback);
end;
// is the address sanctioned?
const step4: TStep = procedure(const callback: TProc<Boolean, IError>)
begin
web3.eth.breadcrumbs.sanctioned({$I keys/breadcrumbs.api.key}, chain, address, callback);
end;
next := procedure(const steps: TArray<TStep>; const index: Integer)
begin
if index >= Length(steps) then
callback(isGood, nil) // we're good if none of the above steps was a problem
else
steps[index](procedure(bad: Boolean; err: IError)
begin
if bad or Assigned(err) then
callback(TSpenderStatus(index), err)
else
next(steps, index + 1);
end);
end;
next([step1, step2, step3, step4], 0); // MUST have the same order as TSpenderStatus
end;
{--------------------------------- TContract ----------------------------------}
type
TContract = record
private
Action : TTokenAction;
Address: TAddress;
Chain : TChain;
public
constructor Create(const aAction: TTokenAction; const aAddress: TAddress; const aChain: TChain);
procedure IsToken(const callback: TProc<Boolean, IError>);
end;
constructor TContract.Create(const aAction: TTokenAction; const aAddress: TAddress; const aChain: TChain);
begin
Self.Action := aAction;
Self.Address := aAddress;
Self.Chain := aChain;
end;
procedure TContract.IsToken(const callback: TProc<Boolean, IError>);
begin
const address = Self.Address;
if Self.Action = taReceive then
callback(True, nil)
else
cache.getContractABI(Self.Chain, address, procedure(abi: IContractABI; err: IError)
begin
if Assigned(err) then
callback(False, err)
else
callback(abi.IsERC20, nil);
end);
end;
{-------------------------------- getEachToken --------------------------------}
type
TGetEach = reference to procedure(const contracts: TArray<TContract>; const err: IError);
TSubStep = reference to procedure(const index: Integer; const prompted: TPrompted);
procedure getEachToken(const server: TEthereumRPCServer; const port: TIdPort; const chain: TChain; const tx: transaction.ITransaction; const callback: TGetEach);
begin
var contracts: TArray<TContract> := [];
// step #1: tx.To
tx.ToIsEOA(chain, procedure(isEOA: Boolean; err: IError)
begin
if Assigned(err) then
begin
callback(contracts, err);
EXIT;
end;
if not isEOA then
contracts := contracts + [TContract.Create(taTransact, tx.&To, chain)];
// step #2: incoming tokens
server.apiKey(port)
.ifErr(procedure(err: IError)
begin
callback(contracts, err)
end)
.&else(procedure(apiKey: string)
begin
tx.Simulate(apiKey, chain, procedure(changes: IAssetChanges; err: IError)
begin
if Assigned(err) or not Assigned(changes) then
callback(contracts, err)
else
tx.From
.ifErr(procedure(err: IError)
begin
callback(contracts, err)
end)
.&else(procedure(from: TAddress)
begin
const incoming: IAssetChanges = changes.Incoming(from);
try
if Assigned(incoming) then for var I := 0 to Pred(incoming.Count) do
if incoming.Item(I).Asset = native then
// ignore native asset (probably ETH)
else if incoming.Item(I).Contract.SameAs(tx.&To) then
// ignore tx.To
else
contracts := contracts + [TContract.Create(taReceive, incoming.Item(I).Contract, chain)];
finally
callback(contracts, nil);
end;
end);
end);
end);
end);
end;
{---------------------------------- TComment ----------------------------------}
constructor TComment.Create(const aValue: string);
begin
inherited Create;
FValue := aValue;
end;
{--------------------------------- TApproval ----------------------------------}
constructor TApproval.Create(const aChain: TChain; const aToken, aOwner, aSpender: TAddress);
begin
Self.FChain := aChain;
Self.FToken := aToken;
Self.FOwner := aOwner;
Self.FSpender := aSpender;
end;
procedure TApproval.Active(const callback: TProc<Boolean, IError>);
begin
web3.eth.erc20.create(TWeb3.Create(Self.Chain), Self.Token).Allowance(Self.Owner, Self.Spender, procedure(allowance: BigInteger; err: IError)
begin
callback(not allowance.IsZero, err);
end);
end;
{---------------------------------- TChecks -----------------------------------}
constructor TChecks.Create(const server: TEthereumRPCServer; const port: TIdPort; const chain: TChain; const tx: transaction.ITransaction; const block: TDone; const log: TLog);
begin
inherited Create;
Self.server := server;
Self.port := port;
Self.chain := chain;
Self.tx := tx;
Self.block := block;
Self.log := log;
end;
procedure TChecks.Step1(const prompted: TPrompted; const next: TNext);
begin
getTransactionFourBytes(tx.Data)
.ifErr(procedure(_: IError)
begin
next(prompted, nil)
end)
.&else(procedure(func: TFourBytes)
begin
if not SameText(fourBytestoHex(func), '0x095EA7B3') then // approve(address,uint256)
next(prompted, nil)
else
getTransactionArgs(tx.Data)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step1))
end)
.&else(procedure(args: TArray<TArg>)
begin
if Length(args) = 0 then
next(prompted, nil)
else begin
const value = (function(const args: TArray<TArg>): BigInteger
begin
if Length(args) > 1 then
Result := args[1].toUInt256
else
Result := web3.Infinite;
end)(args);
if value = 0 then
next(prompted, nil)
else
web3.eth.tokenlists.token(chain, tx.&To, procedure(token: web3.eth.tokenlists.IToken; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step1))
else if not Assigned(token) then
next(prompted, nil)
else
getSpenderStatus(chain, args[0].ToAddress, procedure(status: TSpenderStatus; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step1))
else
tx.From
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step1))
end)
.&else(procedure(from: TAddress)
begin
thread.synchronize(procedure
begin
asset.approve(chain, tx, token, args[0].ToAddress, status, value, procedure(allow: Boolean)
begin
if allow then
try
FApproved := FApproved + [TApproval.Create(chain, token.Address, from, args[0].ToAddress)];
finally
next(prompted + [TWarning.Approve], nil)
end else
block(prompted);
end, log);
end);
end);
end);
end);
end;
end);
end);
end;
procedure TChecks.Step2(const prompted: TPrompted; const next: TNext);
begin
getTransactionFourBytes(tx.Data)
.ifErr(procedure(_: IError)
begin
next(prompted, nil)
end)
.&else(procedure(func: TFourBytes)
begin
if not SameText(fourBytestoHex(func), '0xA9059CBB') then // transfer(address,uint256)
next(prompted, nil)
else
getTransactionArgs(tx.Data)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step2))
end)
.&else(procedure(args: TArray<TArg>)
begin
if Length(args) = 0 then
next(prompted, nil)
else begin
const quantity = args[1].toUInt256;
if quantity = 0 then
next(prompted, nil)
else
server.apiKey(port)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step2))
end)
.&else(procedure(apiKey: string)
begin
tx.Simulate(apiKey, chain, procedure(changes: IAssetChanges; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step2))
else if not Assigned(changes) then
next(prompted, nil)
else begin
const index = changes.IndexOf(tx.&To);
if (index > -1) and (changes.Item(index).Amount > 0) then
thread.synchronize(procedure
begin
asset.transfer(chain, tx, changes.Item(index), procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.TransferOut], nil)
else
block(prompted);
end, log);
end)
else
thread.synchronize(procedure
begin
honeypot.show(chain, tx, tx.&To, args[0].ToAddress, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end;
end);
end);
end;
end);
end);
end;
procedure TChecks.Step3(const prompted: TPrompted; const next: TNext);
begin
getTransactionFourBytes(tx.Data)
.ifErr(procedure(_: IError)
begin
next(prompted, nil)
end)
.&else(procedure(func: TFourBytes)
begin
if not SameText(fourBytestoHex(func), '0xA22CB465') then // setApprovalForAll(address,bool)
next(prompted, nil)
else
getTransactionArgs(tx.Data)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step3))
end)
.&else(procedure(args: TArray<TArg>)
begin
if Length(args) = 0 then
next(prompted, nil)
else begin
const approved = (function(const args: TArray<TArg>): Boolean
begin
if Length(args) > 1 then
Result := args[1].toBoolean
else
Result := False;
end)(args);
if not approved then
next(prompted, nil)
else
thread.synchronize(procedure
begin
setApprovalForAll.show(chain, tx, tx.&To, args[0].ToAddress, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end;
end);
end);
end;
procedure TChecks.Step4(const prompted: TPrompted; const next: TNext);
begin
tx.ToIsEOA(chain, procedure(isEOA: Boolean; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step4))
else if isEOA then
next(prompted, nil)
else
common.Etherscan(chain)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step4))
end)
.&else(procedure(etherscan: IEtherscan)
begin
etherscan.getContractSourceCode(tx.&To, procedure(src: string; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step4))
else if src <> '' then
next(prompted, nil)
else
thread.synchronize(procedure
begin
unverified.show(chain, tx, tx.&To, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end);
end);
end);
end;
procedure TChecks.Step5(const prompted: TPrompted; const next: TNext);
begin
getTransactionFourBytes(tx.Data)
.ifErr(procedure(_: IError)
begin
next(prompted, nil)
end)
.&else(procedure(func: TFourBytes)
begin
if not SameText(fourBytestoHex(func), '0xA9059CBB') then // transfer(address,uint256)
next(prompted, nil)
else
getTransactionArgs(tx.Data)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step5))
end)
.&else(procedure(args: TArray<TArg>)
begin
if Length(args) = 0 then
next(prompted, nil)
else begin
const quantity = args[1].toUInt256;
if quantity = 0 then
next(prompted, nil)
else
web3.defillama.coin(chain, tx.&To, procedure(coin: ICoin; err: IError)
begin
if Assigned(err) or not Assigned(coin) then
next(prompted, error.wrap(err, Self.Step5))
else begin
const amount = (quantity.AsDouble / Round(Power(10, coin.Decimals))) * coin.Price;
if amount < common.LIMIT then
next(prompted, nil)
else
cache.getSymbol(chain, tx.&To, procedure(symbol: string; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step5))
else
thread.synchronize(procedure
begin
limit.show(chain, tx, symbol, args[0].ToAddress, amount, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end);
end;
end);
end;
end);
end);
end;
procedure TChecks.Step6(const prompted: TPrompted; const next: TNext);
begin
if tx.Value = 0 then
next(prompted, nil)
else begin
const client: IWeb3 = TWeb3.Create(chain);
client.LatestPrice(procedure(price: Double; err: IError)
begin
if (price = 0) or Assigned(err) then
next(prompted, error.wrap(err, Self.Step6))
else begin
const amount = dotToFloat(fromWei(tx.Value, ether)) * price;
if amount < common.LIMIT then
next(prompted, nil)
else
thread.synchronize(procedure
begin
limit.show(chain, tx, string(chain.Symbol), tx.&To, amount, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end;
end);
end;
end;
procedure TChecks.Step7(const prompted: TPrompted; const next: TNext);
begin
phisher.isPhisher(tx.&To, procedure(result: Boolean; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step7))
else if not result then
next(prompted, nil)
else
thread.synchronize(procedure
begin
phisher.show(chain, tx, tx.&To, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end);
end;
procedure TChecks.Step8(const prompted: TPrompted; const next: TNext);
begin
server.apiKey(port)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step8))
end)
.&else(procedure(apiKey: string)
begin
getEachToken(server, port, chain, tx, procedure(const contracts: TArray<TContract>; const err: IError)
begin
if Assigned(err) then
begin
next(prompted, error.wrap(err, Self.Step8));
EXIT;
end;
var step: TSubStep;
step := procedure(const index: Integer; const prompted: TPrompted)
begin
if index >= Length(contracts) then
next(prompted, nil)
else ( // call Alchemy's API or Moralis API
procedure(const token: TAddress; const callback: TProc<TContractType, IError>)
begin
web3.eth.alchemy.api.detect(apiKey, chain, token, [TContractType.Airdrop, TContractType.Spam], procedure(contractType: TContractType; err: IError)
begin
if not Assigned(err) then
callback(contractType, nil)
else
moralis.isPossibleSpam({$I keys/moralis.api.key}, chain, token, procedure(spam: Boolean; err: IError)
begin
if spam then
callback(TContractType.Spam, err)
else
callback(TContractType.Good, err);
end);
end);
end)(contracts[index].Address, procedure(contractType: TContractType; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step8))
else case contractType of
TContractType.Airdrop: // probably an unwarranted airdrop (most of the owners are honeypots)
thread.synchronize(procedure
begin
airdrop.show(contracts[index].Action, chain, tx, contracts[index].Address, procedure(allow: Boolean)
begin
if allow then
step(index + 1, prompted + [TWarning.Other])
else
block(prompted);
end, log);
end);
TContractType.Spam: // probably spam (contains duplicate NFTs, or lies about its own token supply)
thread.synchronize(procedure
begin
spam.show(contracts[index].Action, chain, tx, contracts[index].Address, procedure(allow: Boolean)
begin
if allow then
step(index + 1, prompted + [TWarning.Other])
else
block(prompted);
end, log);
end);
else
step(index + 1, prompted);
end;
end);
end;
step(0, prompted);
end);
end);
end;
procedure TChecks.Step9(const prompted: TPrompted; const next: TNext);
begin
tx.From
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step9))
end)
.&else(procedure(from: TAddress)
begin
common.Etherscan(chain)
.ifErr(procedure(err: IError)
begin
next(prompted, error.wrap(err, Self.Step9))
end)
.&else(procedure(etherscan: IEtherscan)
begin
etherscan.getTransactions(from, procedure(txs: ITransactions; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step9))
else if not Assigned(txs) then
next(prompted, nil)
else begin
txs.FilterBy(tx.&To);
if txs.Count > 0 then
next(prompted, nil)
else
thread.synchronize(procedure
begin
firsttime.show(chain, tx, tx.&To, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end;
end);
end);
end);
end;
procedure TChecks.Step10(const prompted: TPrompted; const next: TNext);
begin
web3.eth.tokenlists.unsupported(chain, procedure(tokens: TTokens; err: IError)
begin
if Assigned(err) then
begin
next(prompted, error.wrap(err, Self.Step10));
EXIT;
end;
getEachToken(server, port, chain, tx, procedure(const contracts: TArray<TContract>; const err: IError)
begin
if Assigned(err) then
begin
next(prompted, error.wrap(err, Self.Step10));
EXIT;
end;
var step: TSubStep;
step := procedure(const index: Integer; const prompted: TPrompted)
begin
if index >= Length(contracts) then
next(prompted, nil)
else
if tokens.IndexOf(contracts[index].Address) = -1 then
step(index + 1, prompted)
else
thread.synchronize(procedure
begin
unsupported.show(contracts[index].Action, chain, tx, contracts[index].Address, procedure(allow: Boolean)
begin
if allow then
step(index + 1, prompted + [TWarning.Other])
else
block(prompted);
end, log);
end);
end;
step(0, prompted);
end);
end);
end;
procedure TChecks.Step11(const prompted: TPrompted; const next: TNext);
begin
web3.eth.breadcrumbs.sanctioned({$I keys/breadcrumbs.api.key}, chain, tx.&To, procedure(value: Boolean; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step11))
else if not value then
next(prompted, nil)
else
thread.synchronize(procedure
begin
sanctioned.show(chain, tx, tx.&To, procedure(allow: Boolean)
begin
if allow then
next(prompted + [TWarning.Other], nil)
else
block(prompted);
end, log);
end);
end);
end;
procedure TChecks.Step12(const prompted: TPrompted; const next: TNext);
begin
getEachToken(server, port, chain, tx, procedure(const contracts: TArray<TContract>; const err: IError)
begin
if Assigned(err) then
begin
next(prompted, error.wrap(err, Self.Step12));
EXIT;
end;
var step: TSubStep;
step := procedure(const index: Integer; const prompted: TPrompted)
begin
if index >= Length(contracts) then
next(prompted, nil)
else ( // call DEXTools API or Moralis API
procedure(const token: TAddress; const callback: TProc<Integer, IError>)
begin
dextools.score({$I keys/dextools.api.key}, chain, token, procedure(score1: Integer; err1: IError)
begin
if (score1 > 0) and not Assigned(err1) then
callback(score1, nil)
else
moralis.securityScore({$I keys/moralis.api.key}, chain, token, procedure(score2: Integer; err2: IError)
begin
if (score2 > 0) and not Assigned(err2) then
callback(score2, nil)
else
callback(0, err2);
end);
end);
end)(contracts[index].Address, procedure(score: Integer; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step12))
else
if (score = 0) or (score >= 50) then
step(index + 1, prompted)
else
thread.synchronize(procedure
begin
lowDexScore.show(contracts[index].Action, chain, tx, contracts[index].Address, procedure(allow: Boolean)
begin
if allow then
step(index + 1, prompted + [TWarning.Other])
else
block(prompted);
end, log);
end);
end);
end;
step(0, prompted);
end);
end;
procedure TChecks.Step13(const prompted: TPrompted; const next: TNext);
begin
getEachToken(server, port, chain, tx, procedure(const contracts: TArray<TContract>; const err: IError)
begin
if Assigned(err) then
begin
next(prompted, error.wrap(err, Self.Step13));
EXIT;
end;
var step: TSubStep;
step := procedure(const index: Integer; const prompted: TPrompted)
begin
if index >= Length(contracts) then
next(prompted, nil)
else
contracts[index].IsToken(procedure(isToken: Boolean; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step13))
else
if not isToken then
step(index + 1, prompted)
else ( // call DEXTools API or Moralis API
procedure(const token: TAddress; const callback: TProc<TJsonArray, IError>)
begin
dextools.pairs({$I keys/dextools.api.key}, chain, token, procedure(arr: TJsonArray; err: IError)
begin
if Assigned(arr) and not Assigned(err) then
callback(arr, nil)
else
moralis.pairs({$I keys/moralis.api.key}, chain, token, callback);
end);
end)(contracts[index].Address, procedure(arr: TJsonArray; err: IError)
begin
if Assigned(err) then
next(prompted, error.wrap(err, Self.Step13))
else
if Assigned(arr) and (arr.Count > 0) then
step(index + 1, prompted)
else
thread.synchronize(procedure
begin
noDexPair.show(contracts[index].Action, chain, tx, contracts[index].Address, procedure(allow: Boolean)
begin
if allow then
step(index + 1, prompted + [TWarning.Other])
else
block(prompted);
end, log);
end);
end);
end);
end;
step(0, prompted);
end);
end;
procedure TChecks.Step14(const prompted: TPrompted; const next: TNext);
begin
getEachToken(server, port, chain, tx, procedure(const contracts: TArray<TContract>; const err: IError)
begin
if Assigned(err) then
begin
next(prompted, error.wrap(err, Self.Step14));
EXIT;
end;
var step: TSubStep;
step := procedure(const index: Integer; const prompted: TPrompted)
begin
if index >= Length(contracts) then
next(prompted, nil)
else
cache.getContractABI(chain, contracts[index].Address, procedure(abi: IContractABI; err: IError)