-
Notifications
You must be signed in to change notification settings - Fork 0
/
uOper.pas
429 lines (382 loc) · 13.3 KB
/
uOper.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
unit uOper;
// îïåðàöèÿ è ìàññèâ îïåðàöèé òåêóùåãî ïðîõîäà.
// Êàæäûé õåø â ìàññèâå òîëüêî îäèí ðàç. Ïîâòîðíûå îïåðàöèè ïî õåøó âûñòàëÿþò ïðèçíàê dupl
// ïîèñê çàäàííîãî õåøà â ìàññèâå
interface
uses
AbsMain, SysUtils, StrUtils, DateUtils, dea.tools;
type
TOper = record
private
procedure GetInfoPageAndParse;
procedure GetTransactionPageAndParse;
procedure GetParseTransactionPageAvax;
public
inout: Boolean; // äîáàâëåíèå/óäàëåíèå ëèêâèäíîñòè
adate: TDateTime;
hash: string;
method: string;
// addr1, addr2: string;
currency: string; { íàçâàíèå âàëþòû }
currencyAddress: string; { õåø âàëþòû /token/0x7... }
amount: double; // Ñóììà íà îñíîâíîé ñòðàíèöå ñïèñêà òðàíçàêöèé, ì.á.= 0, çäåñü öåíà íå íóæíà
// íî äëÿ àâàêñà áåðåòñÿ ñóììà èç ñïèñêà îïåðàöèé
currency2: string; { äëÿ àâàêñà äâå âàëþòû }
currencyAddress2: string;
amount2: double;
dupl: Boolean;
error: Boolean;
function LogLine: string;
function IsLiquid: Boolean;
procedure AppendLine(tbDeals: TABSTable);
// function Find(tbDeals: TABSTable): Boolean;
procedure ParseRow(const Cells: TRowsArray); // ïàðñèíã îäíîé ñòðîêè ïåðâîãî àäðåñà
procedure ParseStep2; // äîïàðñèì ñóììó è äðóãèå ïàðàìåòðû èç ñòðàíèöû òðàíçàêöèè (åñëè íàäî)
function GetDexScreenerLink(htmlTags: Boolean = true): string; // ññûëêà íà ñòðàíèöó âàëþòû íà https://dexscreener.com
function GetDexScreenerLink2(htmlTags: Boolean = true): string;
end;
TOpArray = array of TOper; // ìàññèâ îïåðàöèé òåêóùåãî ïðîõîäà
function FindHashAndMarkAsDupl(const Ahash: string; const Opers: TOpArray): Boolean;
procedure LogOpers(const Opers: TOpArray);
implementation
uses
uConst, dea.get, uAppParam, System.Classes;
procedure DeleteByIndex(var A: TRowsArray; const Index: Cardinal);
var
ALength: Cardinal;
i: Cardinal;
begin
ALength := Length(A);
if (index >= ALength) or (ALength = 0) then
exit;
for i := index + 1 to ALength - 1 do
A[i - 1] := A[i];
SetLength(A, ALength - 1);
end;
function TOper.LogLine: string;
begin
Result := hash + #9 +
DateTimeToStr(adate) + #9 +
ifthen(dupl, '*', ' ') + #9 +
ifthen(inout, 'Added liq.', 'Removed lid.') + #9 +
PadR(currency, 12) + #9 +
currencyAddress + #9 +
amount.ToString + #9 +
PadR(currency2, 12) + #9 +
currencyAddress2 + #9 +
amount2.ToString;
end;
const
MAX_RECORDS = 30000;
procedure TOper.AppendLine(tbDeals: TABSTable);
begin // ñåé÷àñ èíòåðåñóåò òîëüêî õåø, îñòàëüíûå ïîëÿ â áàçå íå èñïîëüçóþòñÿ
if tbDeals.RecordCount <= MAX_RECORDS then
tbDeals.Append
else begin
tbDeals.IndexName := 'xDate';
tbDeals.First; // çàòèðàåì ñàìóþ ïåðâóþ
tbDeals.Edit;
end;
tbDeals.FieldByName('created').AsDateTime := NowUTC;
tbDeals.FieldByName('adate').AsDateTime := adate;
tbDeals.FieldByName('hash').AsString := hash;
tbDeals.FieldByName('currency').AsString := currency;
// currencyAddress â áàçå íå õðàíþ (ïîêà íå íóæåí)
tbDeals.FieldByName('inout').AsBoolean := inout;
tbDeals.FieldByName('amount').AsFloat := amount;
tbDeals.Post;
end;
// èùåò òàêóþ æå îïåðàöèþ â áàçå, âîçâðàùàåò èñòèíó, åñëè íàøåë
(*
function TOper.Find(tbDeals: TABSTable): Boolean;
begin
Result := False;
tbDeals.IndexName := 'xHash';
tbDeals.SetRange([hash], [hash]);
tbDeals.First;
while not tbDeals.Eof do begin
if (tbDeals.FieldByName('inout').AsBoolean = inout) and
(tbDeals.FieldByName('amount').AsFloat = amount) and
(tbDeals.FieldByName('adate').AsDateTime = adate) and
(tbDeals.FieldByName('hash').AsString = hash)
then begin // ñòðîêè ïîëíîñòüþ ñîâïàëè
Result := true;
break;
end;
tbDeals.Next;
end;
tbDeals.CancelRange;
end; *)
function TOper.IsLiquid: Boolean;
begin
Result := StartsStr('Add Liquidity', method)
or StartsStr('Remove Liquidity', method)
end;
// íà âõîäå Cells - ÿ÷åéêè îäíîé ñòðîêè
procedure TOper.ParseRow(const Cells: TRowsArray);
var
link, st: string;
begin
// 0 - êíîïêà ñ âîïðîñèêîì
// 1 - hash <a href='/tx/0x5b8ef...' class. Çäåñü æå áûâàåò âîñêë. çíàê ðàçíûõ öâåòîâ : fa-exclamation-circle - status fail or reverted
// 2 - method - <td><span ... title="" data-original-title="Add Liquidity ETH">Add Liquidity ET...</span></td>
// - - - - -èëè <td><span ... title="" data-original-title="Remove Liquidity ETH With Permit">Remove Liquidity...</span></td>
// 3 - block
// 4 - age - <span ... title='6 secs ago'>2022-01-18 22:45:43</span></td>
// 5 - age - title='2022-01-18 22:45:43'>6 secs ago</span></td>
// 6 - from - <a href='/address/0x20f15...'>0x20f15...</a></span>
// 7 - ñòðåëêà - rounded-circle
// 8 - to - <span ...><i class="far ..." data-toggle='tooltip'
// title='Contract'></i> <a class='hash...' href='/address/0x163...' data-toggle='tooltip'
// title='SpiritSwap: Router (0x163...)'>SpiritSwap: Router</a>
// âàðèàíò: <span class="hash..." data-toggle="tooltip" title=""
// data-original-title="0xfc80..."><a href="/address/0xfc80...">0xfc80...</a></span>
// 9 - value - <td>0 FTM</td> èëè <td>99<b>.</b><span class='text-secondary'>99</span> FTM</td>
// âàðèàíò <td>7<b>.</b><span class="text-secondary">241505634981155</span> FTM</td>
// 10 - fee
dupl := False;
error := Pos('fa-exclamation-circle', Cells[1]) > 0;
st := reprep(Cells[4], '<span*>', '</span>'); // UTC date-time
adate := StrYYYY_MM_DDToDateTime(st); // äàòà
st := trim(HTMLStrip(StrAfterPos('>', Cells[9])));
if Pos(' ', st) > 0 then // â êîíöå íàçâàíèå âàëþòû ñàéòà, íàïðèìåð, äëÿ ôàíòîìà FTM
st := StrBeforeLastPos(' ', st);
amount := StrToFloatDef(st, 0);
link := reprep(Cells[1], 'href=''', ''''); // /tx/0x4833063e8170eb19003b30dea52d60df693d59a441f1
hash := StripFirstStr(link, '/tx/0x'); // â íà÷àëå êàæäîãî õåøà 0x - â áàçå íå õðàíþ
method := trim(reprep(Cells[2], 'original-title="', '"'));
if method = '' then
method := trim(reprep(Cells[2], 'title="', '"'));
if method = '' then
log('!!! method - empty'); // todo
inout := StartsStr('Add Liquidity', method);
// addr1 := reprep(Cells[6], 'href=''', ''''); íå íóæíî
// addr2 := reprep(Cells[8], 'href=''', '''');
end;
procedure TOper.ParseStep2; // äîïàðñèì ñóììó è äðóãèå ïàðàìåòðû èç ñòðàíèöû òðàíçàêöèè (åñëè íàäî)
begin
if error or not IsLiquid then
exit;
// if AppParam.Site = S_FANTOM then
// if inout then
// GetInfoPageAndParse
// else
// GetTransactionPageAndParse
// else
// if AppParam.Site = S_AVAX then
GetParseTransactionPageAvax;
end;
// ïàðñèò ñòðàíè÷êó, êîòîðàÿ îòêðûâàåòñÿ ïî êëèêó íà âîïðîñèê ñëåâà îò ñòðîêè ñòðîêè òðàíçàêöèè
// îïðåäåëÿåò currency è currencyAddress
// èç ïåðâîé ñòðîêè èç âñåõ îïåðàöèé
procedure TOper.GetInfoPageAndParse;
var
link, st, s: string;
nPos: Integer;
begin
currency := '';
currencyAddress := '';
if error or (amount = 0) or not IsLiquid then
exit;
link := AppParam.GetShortTokenUrl(hash);
try
AppParam.DoSleep;
st := xget(link);
except
on E: Exception do begin
log('!xget GetCurrencyAndParse ' + hash + ' ' + E.Message);
Sleep(200);
exit;
end;
end;
nPos := Pos('Token Transfer:', st);
s := reprep(st, '<div class=''d-flex''><', '<div class=''d-flex''', nPos); // ïåðâàÿ ñòðîêà èç âñåõ îïåðàöèé
if s <> '' then begin // ...href='/token/0x7a6e4e3cc2ac9924605dca4ba31d1831c84b44ae'>2OMB</a>
currencyAddress := reprep(s, 'href=''', '''');
currency := trim(HTMLStrip(reprep(s, 'href=*>', '</a')));
if EndsStr('...', currency) and (reprep(s, 'data-toggle=''tooltip'' title=''', '''') <> '') then begin
currency := reprep(s, 'data-toggle=''tooltip'' title=''', '''')
end;
end;
if currency = '' then
log('!!! currency - empty ' + hash); // todo
end;
// ïàðñèò ñòðàíèöó òðàíçàêöèè, íóæíî òîëüêî äëÿ óáèðàíèÿ ëèêâèäíîñòè
// îïðåäåëÿåò currency, currencyAddress è
// amount - ñóììó áåðåì èç ïîñëåäíåé ñòðîêè
procedure TOper.GetTransactionPageAndParse;
var
link, st, s, tmp: string;
List: TStringList;
nPos, Len: Integer;
samount: string;
begin
currency := '';
currencyAddress := '';
amount := 0; // ñóììó áåðåì èç ïîñëåäíåé ñòðîêè
if error or not IsLiquid then
exit;
link := AppParam.GetTokenUrl(hash);
try
AppParam.DoSleep;
st := xget(link);
except
on E: Exception do begin
log('!xget GetTransactionPageAndParse ' + hash + ' ' + E.Message);
Sleep(200);
exit;
end;
end;
nPos := Pos('Tokens Transferred:', st);
s := reprep(st, '<ul ', '</ul>', nPos); // ïîñëåäíÿÿ ñòðîêà èç âñåõ îïåðàöèé
List := TStringList.Create;
ReprepList(s, '<li', '</li', List);
Len := List.Count;
if Len > 1 then begin
currencyAddress := reprepLast(List[Len - 2], 'href=''', ''''); // ïðåäïîñëåäíÿÿ ññûëêà èç òðåõ
currency := trim(HTMLStrip(reprepLast(List[Len - 2], 'href=*>', '</a>')));
if (currency = '') or (StartsStr('0x', currency)) then begin
currency := trim(HTMLStrip(reprepLast(List[Len - 2], 'title=''', '''')));
end;
tmp := reprep(currency, '(', ')');
if tmp <> '' then
if not EndsStr('...', tmp) then
currency := tmp
else
currency := trim(StrBeforePos('(', currency));
// data-original-title="Current Price : $2.28 / WFTM">76.928591914670303152 ($175.40)</span>
samount := reprep(List[Len - 1], '<b>For</b>*Current Price :', '</span');
amount := reprepFloat(samount, '>', '(');
if amount = 0 then
amount := reprepFloat(samount, '>', '<');
end;
if currency = '' then
log('!!! currency 2 - empty ' + hash); // todo
List.Free;
end;
// ïàðñèò ñòðàíèöó òðàíçàêöèè äëÿ Avax
// îïðåäåëÿåò äâå âàëþòû currency, currencyAddress è amount
procedure TOper.GetParseTransactionPageAvax;
var
link, st, s, tmp: string;
List: TStringList;
nPos, i, flag: Integer;
samount: string;
function parseCurrency(const s: string; var currency, currencyAddress: string): Boolean;
begin
currencyAddress := reprepLast(List[i], 'href=''', ''''); // ïðåäïîñëåäíÿÿ ññûëêà èç òðåõ
currency := trim(HTMLStrip(reprepLast(List[i], 'href=*>', '</a>')));
if (currency = '') or (StartsStr('0x', currency)) then begin
currency := trim(HTMLStrip(reprepLast(List[i], 'title=''', '''')));
end;
tmp := reprep(currency, '(', ')');
if tmp <> '' then
if not EndsStr('...', tmp) then
currency := tmp
else
currency := trim(StrBeforePos('(', currency));
Result := (currency <> 'JLP') and (currency <> 'PGL') and
(currency <> 'Lydia LPs');
end;
function parseAmount(const s: string; var amount: double): Boolean;
begin
// data-original-title="Current Price : $2.28 / WFTM">76.928591914670303152 ($175.40)</span>
samount := reprep(List[i], '<b>For</b>*Current Price :', '</span');
tmp := ReplaceStr(reprep(samount, '($', ')'), ',', '');
amount := iStrToFloat(tmp);
Result := amount <> 0;
end;
begin
currency := '';
currencyAddress := '';
amount := 0;
currency2 := '';
currencyAddress2 := '';
amount2 := 0;
if error or not IsLiquid then
exit;
link := AppParam.GetTokenUrl(hash);
try
AppParam.DoSleep;
st := xget(link);
except
on E: Exception do begin
log('!xget GetParseTransactionPageAvax ' + hash + ' ' + E.Message);
Sleep(200);
exit;
end;
end;
nPos := Pos('Tokens Transferred:', st);
s := reprep(st, '<ul ', '</ul>', nPos);
List := TStringList.Create;
ReprepList(s, '<li', '</li', List);
flag := 0;
for i := 0 to List.Count - 1 do begin
if flag = 0 then begin
if parseCurrency(List[i], currency, currencyAddress) then begin // ïàðñèì âàëþòó
parseAmount(List[i], amount); // ïàðñèì ñóììó
flag := 1;
end
end
else begin
if parseCurrency(List[i], currency2, currencyAddress2) and (currency2 <> currency)
then begin // ïàðñèì âòîðóþ âàëþòó, äóáëè ïðîïóñêàåì
parseAmount(List[i], amount2); // ïàðñèì ñóììó
break; // äîëæíî áûòü òîëüêî äâå ðàçíûå âàëþòû
end;
end;
end;
if (amount = 0) and (amount2 <> 0) then
amount := amount2
else
if (amount2 = 0) and (amount <> 0) then
amount2 := amount;
if currency = '' then
log('!!! currency 3 - empty ' + hash);
List.Free;
end;
// èùåò õåø â ìàññèâå, âîçâðàùàåò èñòèíó, åñëè íàõîäèò
// âûñòàâëÿåò ïðèçíàê dupl â èñòèíó, åñëè íàõîäèò
// ïðè äîáàâëåíèè õåøà äåëàåòñÿ ïðîâåðêà íà óíèêàëüíîñòü, ïîýòîìó õåø ìîæåò áûòü â ìàññèâå òîëüêî îäèí ðàç
function FindHashAndMarkAsDupl(const Ahash: string; const Opers: TOpArray): Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to high(Opers) do begin
if Opers[i].hash = Ahash then begin
Opers[i].dupl := true;
Result := true;
exit; // íè÷åãî äîáàâëÿòü íå íàäî, ïîâòîðíàÿ îïåðàöèÿ ïî õåøó
end;
end;
end;
procedure LogOpers(const Opers: TOpArray);
var
i: Integer;
begin
for i := 0 to high(Opers) do begin
log(Opers[i].LogLine);
end;
end;
function TOper.GetDexScreenerLink(htmlTags: Boolean = true): string;
begin
if currencyAddress = '' then // /token/0x7...
Result := ''
else begin
Result := AppParam.GetDexUrl + StrAfterPos('/token/', currencyAddress);
if htmlTags then
Result := '<a href="' + Result + '">dexscreener.com</a>';
end;
end;
function TOper.GetDexScreenerLink2(htmlTags: Boolean = true): string;
begin
if currencyAddress2 = '' then // /token/0x7...
Result := ''
else begin
Result := AppParam.GetDexUrl + StrAfterPos('/token/', currencyAddress2);
if htmlTags then
Result := '<a href="' + Result + '">dexscreener.com</a>';
end;
end;
end.