-
Notifications
You must be signed in to change notification settings - Fork 0
/
CurveMonitor.mjs
1404 lines (1219 loc) · 47 KB
/
CurveMonitor.mjs
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
import { Server } from "socket.io";
/* eslint-disable no-debugger, quote-props */
/* eslint-disable no-debugger, object-shorthand */
import fs from "fs";
import https from "https";
import Web3 from "web3";
import dotenv from "dotenv";
dotenv.config();
const tempTxHashStorage = [];
let tempMevStorage = [];
import {
getUnixtime,
getABI,
getCurvePools,
isNativeEthAddress,
isNullAddress,
isCurveRegistryExchange,
is3PoolDepositZap,
isZapFor3poolMetapools,
isSwap,
isRemoval,
isDeposit,
isTokenExchangeUnderlying,
formatForPrint,
getTokenAddress,
countUniqueTxHashes,
getTokenName,
buildPoolName,
getCleanedTokenAmount,
wait,
} from "./Utils/GenericUtils.mjs";
import {
setProvider,
getContract,
getTx,
getCurrentBlockNumber,
getBlock,
getBlockUnixtime,
getTokenTransfers,
isCupsErr,
checkForTokenExchange,
checkForTokenExchangeUnderlying,
} from "./Utils/Web3CallUtils.mjs";
import { saveTxEntry, findLastProcessedEvent, getStartBlock, getHolderFee } from "./Utils/StorageUtils.mjs";
import { collectRawLogs, addAndWriteEventToRawLog } from "./Utils/FetchRawLogs.mjs";
import {
getDeltaMevBot,
tokenExchangeCaseMultiple,
tokenExchangeCaseUnderlying,
tokenExchangeCaseSingle,
victimTxCaseExchangeUnderlying,
victimTxCaseTokenExchangeUnderlying,
victimTxCaseDeposit,
victimTxCaseRemoval,
victimTxCaseSwap,
} from "./Utils/TransactionLogicUtils.mjs";
// utils for price-data
import { priceCollectionMain, savePriceEntry, bootPriceJSON, convertToUSD } from "./Utils/PriceUtils.mjs";
// utils for pool-balances
import { fetchBalancesOnce, bootBalancesJSON, balancesCollectionMain } from "./Utils/BalancesUtils.mjs";
// utils to create messages for the socket
import { httpSocketSetup, httpsSocketSetup } from "./Utils/SocketUtils.mjs";
// utils to fetch and store bonding curves
import { updateBondingCurvesForPool } from "./Utils/BondingCurveUtils.mjs";
import ABI_DECODER from "abi-decoder";
ABI_DECODER.addABI(await getABI("ABI_REGISTRY_EXCHANGE"));
ABI_DECODER.addABI(await getABI("ABI_METAPOOL"));
ABI_DECODER.addABI(await getABI("ABI_THREEPOOL_ZAP"));
ABI_DECODER.addABI(await getABI("ABI_3POOL_DEPOSIT_ZAP"));
ABI_DECODER.addABI(await getABI("ABI_ZAP_FOR_3POOL_METAPOOLS"));
import EventEmitter from "events";
const emitter = new EventEmitter();
const options = {
// Enable auto reconnection
reconnect: {
auto: true,
delay: 89, // ms
maxAttempts: 2500,
onTimeout: false,
},
};
console.clear();
let wsKEY1;
let wsKEY2;
let wsKEY3;
let wsKEY4;
let wsKEY5;
function initWeb3Sockets(type) {
if (type === "telegramBot") {
wsKEY1 = setProvider(process.env.wsKEY1);
wsKEY2 = setProvider(process.env.wsKEY2);
wsKEY3 = setProvider(process.env.wsKEY3);
wsKEY4 = setProvider(process.env.wsKEY4);
wsKEY5 = setProvider(process.env.wsKEY5);
}
if (type === "dashboard") {
wsKEY1 = setProvider(process.env.wsKEY6);
wsKEY2 = setProvider(process.env.wsKEY7);
wsKEY3 = setProvider(process.env.wsKEY8);
wsKEY4 = setProvider(process.env.wsKEY9);
wsKEY5 = setProvider(process.env.wsKEY10);
}
}
const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.web3, options));
const ADDRESS_sUSD_V2_SWAP = "0xA5407eAE9Ba41422680e2e00537571bcC53efBfD";
const ADDRESS_ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
// runs through the stored txHashes once a minute, and removes the ones that are older than 15 seconds
// reason for this is to not confuse swap_underlyings with deposits or withdrawals
function cleanTxHashStorage() {
for (let i = 0; i < tempTxHashStorage.length; i++) {
const NOW = getUnixtime();
const STORED_TIME = tempTxHashStorage[i].time;
const SECONDS_PAST = NOW - STORED_TIME;
if (SECONDS_PAST >= 15) {
tempTxHashStorage.splice(i, 1);
}
}
}
setInterval(cleanTxHashStorage, 60000);
// the Buffer consists of tx, which might be part of a sandwich, since they show up one by one, we need to store them for a bit.
async function buildMessageFromBuffer(i) {
const E = mevTxBuffer[i].extraData;
const BLOCK_NUMBER = mevTxBuffer[i].data.blockNumber;
if (isSwap(mevTxBuffer[i].type)) {
return await buildSwapMessage(
BLOCK_NUMBER,
E.soldAddress,
E.soldAmount,
E.boughtAddress,
E.boughtAmount,
E.tokenSoldName,
E.tokenBoughtName,
E.poolAddress,
E.txHash,
E.buyer,
E.to,
E.position,
E.poolName
);
}
if (isRemoval(mevTxBuffer[i].type)) {
return await buildRemovalMessage(BLOCK_NUMBER, E.coinAmount, E.tokenRemovedName, E.poolAddress, E.txHash, E.agentAddress, E.position, E.removedAddress);
}
if (isDeposit(mevTxBuffer[i].type)) {
return await buildDepositMessage(BLOCK_NUMBER, E.coinArray, E.poolAddress, E.txHash, E.agentAddress, E.position);
}
}
// tx that had been stored as pot. sandwich-tx, but were in fact not part of sandwich, are getting processed as normal tx from here on out
async function cleanMevTxBuffer(BRAND_NEW_BLOCK) {
while (true) {
if (mevTxBuffer.length === 0) break;
if (mevTxBuffer[0].blockNumber < BRAND_NEW_BLOCK) {
if (!tempTxHashStorage.find((tx) => tx.txHash === mevTxBuffer[0].txHash)) {
await buildMessageFromBuffer(0);
tempTxHashStorage.push({
txHash: mevTxBuffer[0].txHash,
time: getUnixtime(),
});
}
mevTxBuffer.shift();
} else {
break;
}
}
}
async function subscribeToNewBlocks() {
try {
web3.eth.subscribe("newBlockHeaders", async function (error, result) {
if (error) {
console.error("err in subscribeToNewBlocks", error);
} else {
const BRAND_NEW_BLOCK = result.number;
// cleaning every 2nd block
if (BRAND_NEW_BLOCK % 2 === 0) {
await cleanMevTxBuffer(BRAND_NEW_BLOCK);
}
}
});
} catch (err) {
console.log("err in subscribeToNewBlocks", err.message);
}
}
let mevTxBuffer = [];
async function mevBuffer(blockNumber, position, txHash, type, extraData, isSwapUnderlying, data) {
// first we need to solve multiple blocks getting jammed together:
if (mevTxBuffer.length !== 0) {
for (let i = 0; i < mevTxBuffer.length; i++) {
// going through the elements in mevTxBuffer, to find old entries and clear them
if (mevTxBuffer[i].data.blockNumber !== blockNumber) {
console.log("missmatching block found, cleaning");
await buildMessageFromBuffer(i);
mevTxBuffer.splice(i, 1);
}
}
}
for (const entry of mevTxBuffer) {
if (entry.txHash === txHash) {
// if newPool and oldPool of the same tx are the same: remove doubling
const NEW_POOL = data.address;
const OLD_POOL = entry.data.address;
if (NEW_POOL === OLD_POOL) return; // same txHash and same pool. Remaining edge case: multiple swaps in the same pool in the same transaction
}
}
mevTxBuffer.push({
blockNumber,
position,
txHash,
type,
extraData,
isSwapUnderlying,
data,
});
if (mevTxBuffer.length === 4) {
let poolOccurrences = countPoolOccurrences(mevTxBuffer);
mevTxBuffer = filterTransactionsByTopRankedPool(mevTxBuffer, poolOccurrences);
}
let numUniqueTxHashes = countUniqueTxHashes(mevTxBuffer);
if (numUniqueTxHashes === 3) {
await wait(4000); // giving time to catch tx4, in case there is one
// fully scouted sandwich
mevTxBuffer.sort(function (a, b) {
return a.position - b.position;
});
//debugging
try {
let x = mevTxBuffer[0].extraData.buyer;
if (!x) return;
} catch (error) {
return;
}
try {
let x = mevTxBuffer[1].extraData.buyer;
if (!x) return;
} catch (error) {
return;
}
const BUYER_0 = mevTxBuffer[0].extraData.buyer;
const BUYER_2 = mevTxBuffer[2].extraData.buyer;
if (BUYER_0 !== BUYER_2) return;
const POOL_0 = mevTxBuffer[0].data.address;
const POOL_1 = mevTxBuffer[1].data.address;
const POOL_2 = mevTxBuffer[2].data.address;
if (POOL_0 === POOL_1 && POOL_1 === POOL_2) {
await processFullSandwich(mevTxBuffer);
} else {
mevTxBuffer = [mevTxBuffer[0], mevTxBuffer[2]];
numUniqueTxHashes = 2;
}
}
}
function filterTransactionsByTopRankedPool(mevTxBuffer, poolOccurrences) {
const topRankedPoolName = poolOccurrences[0].poolName;
const filteredTxBuffer = mevTxBuffer.filter((tx) => tx.extraData.poolName === topRankedPoolName);
return filteredTxBuffer;
}
function countPoolOccurrences(mevTxBuffer) {
const poolNameOccurrences = {};
for (let i = 0; i < mevTxBuffer.length; i++) {
const poolName = mevTxBuffer[i].extraData.poolName;
if (!poolNameOccurrences[poolName]) {
poolNameOccurrences[poolName] = 1;
} else {
poolNameOccurrences[poolName]++;
}
}
const poolOccurrencesArray = [];
for (const [poolName, numberOfOccurances] of Object.entries(poolNameOccurrences)) {
poolOccurrencesArray.push({
poolName: poolName,
numberOfOccurances: numberOfOccurances,
});
}
return poolOccurrencesArray;
}
async function processFullSandwich(mevTxBuffer) {
console.log("\nprocessFullSandwich");
if (mevTxBuffer.some((tx) => tempMevStorage.includes(tx.txHash))) return; // exact sandwich had been processed already
for (var i = 0; i < mevTxBuffer.length; i++) {
tempMevStorage.push(mevTxBuffer[i].txHash);
}
let deltaVictim, coinNameBot, peacefulAmountOut, name, messagePosition1, messageVictim;
let victimData = mevTxBuffer[1];
const TOKEN_BOUGHT_NAME = mevTxBuffer[0].extraData.tokenBoughtName;
let messagePosition0 = await buildMessageFromBuffer(0);
if (messagePosition0 === "abort") return;
if (mevTxBuffer.length === 2) {
// filtering a case of un-related swaps. The token that was bought, has to be the same Token that gets sold afterwards.
if (TOKEN_BOUGHT_NAME !== mevTxBuffer[1].extraData.tokenSoldName) return;
messagePosition1 = await buildMessageFromBuffer(1);
if (messagePosition1 === "abort") return;
const BLOCK_DATA = await getBlock(mevTxBuffer[0].blockNumber);
const VICTIM_TX_HASH = BLOCK_DATA.transactions[1 + mevTxBuffer[0].position];
const VICTIM_TX = await getTx(VICTIM_TX_HASH);
messageVictim = "messageVictim to" + VICTIM_TX.to;
} else if (mevTxBuffer.length === 3) {
if (TOKEN_BOUGHT_NAME !== mevTxBuffer[2].extraData.tokenSoldName) return;
messageVictim = await buildMessageFromBuffer(1);
if (messageVictim === "abort") return;
messagePosition1 = await buildMessageFromBuffer(2);
if (messagePosition1 === "abort") return;
} else if (mevTxBuffer.length === 4) {
if (TOKEN_BOUGHT_NAME !== mevTxBuffer[3].extraData.tokenSoldName) return;
const POOL_0 = mevTxBuffer[0].data.address;
const POOL_1 = mevTxBuffer[1].data.address;
if (POOL_0 === POOL_1) {
messageVictim = await buildMessageFromBuffer(1);
victimData = mevTxBuffer[1];
} else {
messageVictim = await buildMessageFromBuffer(2);
victimData = mevTxBuffer[2];
}
messagePosition1 = await buildMessageFromBuffer(3);
}
// updating the txHashStorage, so messages don't get send multiple time
for (var i = 0; i < mevTxBuffer.length; i++) {
tempTxHashStorage.push({
txHash: mevTxBuffer[i].txHash,
time: getUnixtime(),
});
}
if (isCurveRegistryExchange(victimData.data.returnValues["0"])) {
peacefulAmountOut = await victimTxCaseExchangeUnderlying(victimData);
} else if (isTokenExchangeUnderlying(victimData.isSwapUnderlying)) {
peacefulAmountOut = await victimTxCaseTokenExchangeUnderlying(victimData);
} else if (isDeposit(victimData.type)) {
let res = await victimTxCaseDeposit(victimData);
deltaVictim = res[0];
name = res[1];
} else if (isRemoval(victimData.type)) {
let res = await victimTxCaseRemoval(victimData);
deltaVictim = res[0];
name = res[1];
} else {
peacefulAmountOut = await victimTxCaseSwap(victimData);
}
let DELTA_MEV_BOT;
const EXTRA_DATA = victimData.extraData;
if (isSwap(mevTxBuffer[0].type)) {
if (mevTxBuffer.length === 2 && isSwap(mevTxBuffer[1].type)) {
name = mevTxBuffer[0].extraData.tokenSoldName;
DELTA_MEV_BOT = getDeltaMevBot(mevTxBuffer, 1);
} else if (mevTxBuffer.length === 3 && isSwap(mevTxBuffer[2].type)) {
coinNameBot = mevTxBuffer[0].extraData.tokenSoldName;
DELTA_MEV_BOT = getDeltaMevBot(mevTxBuffer, 2);
if (isSwap(victimData.type)) {
deltaVictim = formatForPrint(EXTRA_DATA.boughtAmount - peacefulAmountOut);
name = EXTRA_DATA.tokenBoughtName;
}
} else if (mevTxBuffer.length === 4 && isSwap(mevTxBuffer[3].type)) {
coinNameBot = TOKEN_BOUGHT_NAME;
DELTA_MEV_BOT = getDeltaMevBot(mevTxBuffer, 3);
if (isSwap(victimData.type)) {
deltaVictim = formatForPrint(EXTRA_DATA.boughtAmount - peacefulAmountOut);
name = EXTRA_DATA.tokenBoughtName;
}
}
}
const UNIXTIME = await getBlockUnixtime(messagePosition0[1].blockNumber);
const MEV_ENTRY = {
type: "sandwich",
blockNumber: messagePosition0[1].blockNumber,
unixtime: UNIXTIME,
profit: parseFloat(DELTA_MEV_BOT.replace(/,/g, "")),
profitUnit: coinNameBot,
loss: parseFloat(deltaVictim.replace(/,/g, "")),
lossUnit: name,
tx: [messagePosition0[1], messageVictim[1], messagePosition1[1]],
};
const POOL_ADDRESS = messagePosition0[0];
emitter.emit("Update Table-MEV" + POOL_ADDRESS, MEV_ENTRY);
if (writeToFile) saveTxEntry(POOL_ADDRESS, MEV_ENTRY);
mevTxBuffer.length = 0;
tempMevStorage.length = 0;
}
let prevTxHash;
async function buildSwapMessage(
blockNumber,
soldAddress,
soldAmount,
boughtAddress,
boughtAmount,
tokenSoldName,
tokenBoughtName,
poolAddress,
txHash,
buyer,
to,
position,
poolName
) {
if (txHash === prevTxHash) return;
prevTxHash = txHash;
let dollarAmount = await convertToUSD(tokenSoldName, soldAmount);
if (!dollarAmount) dollarAmount = await convertToUSD(tokenBoughtName, boughtAmount);
if (isNaN(dollarAmount)) {
console.log("undefined dollarAmount when swapping", tokenSoldName, "to", tokenBoughtName);
return "abort";
}
// adding fee
let holderFee = getHolderFee(dollarAmount, poolAddress);
dollarAmount = formatForPrint(dollarAmount);
holderFee = formatForPrint(holderFee);
soldAmount = formatForPrint(soldAmount);
boughtAmount = formatForPrint(boughtAmount);
console.log("sold", soldAmount, tokenSoldName, "bought", boughtAmount, tokenBoughtName, "(" + dollarAmount + "$)");
const UNIXTIME = await getBlockUnixtime(blockNumber);
const ENTRY = {
type: "swap",
txHash: txHash,
blockNumber: blockNumber,
position: position,
trader: buyer,
tradeDetails: {
amountIn: parseFloat(soldAmount.replace(/,/g, "")),
nameIn: tokenSoldName,
amountOut: parseFloat(boughtAmount.replace(/,/g, "")),
nameOut: tokenBoughtName,
feeUSD: parseFloat(holderFee.replace(/,/g, "")),
valueUSD: parseFloat(dollarAmount.replace(/,/g, "")),
},
unixtime: UNIXTIME,
};
if (writeToFile) {
saveTxEntry(poolAddress, ENTRY);
let priceEntry;
if (collectionCompleteForPrices) priceEntry = await savePriceEntry(poolAddress, blockNumber, UNIXTIME);
if (collectionCompleteForPrices && collectionCompleteForBalances) {
const BALANCES_ENTRY = await fetchBalancesOnce(poolAddress, blockNumber);
const VOLUME_ENTRY = { [UNIXTIME]: parseFloat(dollarAmount.replace(/,/g, "")) };
const BALANCES = Object.values(BALANCES_ENTRY)[0];
let tvlEntry = [];
if (BALANCES_ENTRY.length !== 0) {
const TVL = BALANCES.reduce((a, b) => a + b, 0);
tvlEntry = { [UNIXTIME]: TVL };
}
if (!isCollecting) {
await updateBondingCurvesForPool(poolAddress);
const UPDATE = {
all: ENTRY,
unixtime: UNIXTIME,
price: priceEntry,
balances: BALANCES_ENTRY,
volume: VOLUME_ENTRY,
tvl: tvlEntry,
};
emitter.emit("General Pool Update" + poolAddress, UPDATE);
}
}
}
return [poolAddress, ENTRY];
}
async function buildRemovalMessage(blockNumber, coinAmount, tokenRemovedName, poolAddress, txHash, agentAddress, position, removedAddress) {
let dollarAmount = await convertToUSD(tokenRemovedName, coinAmount);
if (isNaN(dollarAmount)) return "abort";
dollarAmount = formatForPrint(dollarAmount);
coinAmount = formatForPrint(coinAmount);
let poolName = await buildPoolName(poolAddress);
console.log("removed", coinAmount, tokenRemovedName, "from", poolName, "(" + dollarAmount + "$)");
const stakedTokenArray = [];
stakedTokenArray.push({
amountOut: parseFloat(coinAmount.replace(/,/g, "")),
nameOut: tokenRemovedName,
valueUSD: parseFloat(dollarAmount.replace(/,/g, "")),
});
const UNIXTIME = await getBlockUnixtime(blockNumber);
const ENTRY = {
type: "remove",
txHash: txHash,
blockNumber: blockNumber,
position: position,
trader: agentAddress,
tradeDetails: stakedTokenArray,
unixtime: UNIXTIME,
};
if (writeToFile) {
saveTxEntry(poolAddress, ENTRY);
let priceEntry;
if (collectionCompleteForPrices) priceEntry = await savePriceEntry(poolAddress, blockNumber, UNIXTIME);
if (collectionCompleteForPrices && collectionCompleteForBalances) {
const BALANCES_ENTRY = await fetchBalancesOnce(poolAddress, blockNumber);
const VOLUME_ENTRY = { [UNIXTIME]: parseFloat(dollarAmount.replace(/,/g, "")) };
const BALANCES = Object.values(BALANCES_ENTRY)[0];
let tvlEntry = [];
if (BALANCES_ENTRY.length !== 0) {
const TVL = BALANCES.reduce((a, b) => a + b, 0);
tvlEntry = { [UNIXTIME]: TVL };
}
if (!isCollecting) {
await updateBondingCurvesForPool(poolAddress);
const UPDATE = {
all: ENTRY,
unixtime: UNIXTIME,
price: priceEntry,
balances: BALANCES_ENTRY,
volume: VOLUME_ENTRY,
tvl: tvlEntry,
};
emitter.emit("General Pool Update" + poolAddress, UPDATE);
}
}
}
return [poolAddress, ENTRY];
}
async function buildDepositMessage(blockNumber, coinArray, poolAddress, txHash, agentAddress, position, to, buyer) {
const DEPOSITED_TOKEN_ARRAY = [];
let dollarAmountTotal = 0;
let tokenDepositedName;
for (const COIN of coinArray) {
tokenDepositedName = COIN.tokenDepositedName;
let coinAmount = COIN.coinAmount;
if (Number(coinAmount) === 0) continue;
let dollarAmount = await convertToUSD(tokenDepositedName, coinAmount);
if (!dollarAmount) {
dollarAmount = coinAmount;
console.log("no dollar value known for", tokenDepositedName, "(undefined)");
return "abort";
}
dollarAmountTotal += Number(dollarAmount);
coinAmount = formatForPrint(coinAmount);
DEPOSITED_TOKEN_ARRAY.push({
amountIn: parseFloat(coinAmount.replace(/,/g, "")),
nameIn: tokenDepositedName,
valueUSD: Number(dollarAmount),
});
}
let dollarAmount = formatForPrint(dollarAmountTotal);
if (isNaN(dollarAmountTotal)) {
console.log("no dollar value known for", tokenDepositedName, "(NaN)", dollarAmountTotal);
return "abort";
}
let poolName = await buildPoolName(poolAddress);
// removed, too much text for 1.5$ fee on a 5M$ deposit
// let holderFee = await getFeesSimple(originalPoolAddress, feeArray)
console.log("deposited", dollarAmount + "$ into", poolName);
const UNIXTIME = await getBlockUnixtime(blockNumber);
const ENTRY = {
type: "deposit",
txHash: txHash,
blockNumber: blockNumber,
position: position,
trader: agentAddress,
tradeDetails: DEPOSITED_TOKEN_ARRAY,
unixtime: UNIXTIME,
};
if (writeToFile) {
saveTxEntry(poolAddress, ENTRY);
let priceEntry;
if (collectionCompleteForPrices) priceEntry = await savePriceEntry(poolAddress, blockNumber, UNIXTIME);
if (collectionCompleteForPrices && collectionCompleteForBalances) {
const BALANCES_ENTRY = await fetchBalancesOnce(poolAddress, blockNumber);
const VOLUME_ENTRY = { [UNIXTIME]: dollarAmountTotal };
const BALANCES = Object.values(BALANCES_ENTRY)[0];
let tvlEntry = [];
if (BALANCES_ENTRY.length !== 0) {
const TVL = BALANCES.reduce((a, b) => a + b, 0);
tvlEntry = { [UNIXTIME]: TVL };
}
if (!isCollecting) {
await updateBondingCurvesForPool(poolAddress);
const UPDATE = {
all: ENTRY,
unixtime: UNIXTIME,
price: priceEntry,
balances: BALANCES_ENTRY,
volume: VOLUME_ENTRY,
tvl: tvlEntry,
};
emitter.emit("General Pool Update" + poolAddress, UPDATE);
}
}
}
return [poolAddress, ENTRY];
}
const ABI_TOKEN_EXCHANGE = await getABI("ABI_TOKEN_EXCHANGE");
const ABI_TOKEN_EXCHANGE_2 = await getABI("ABI_TOKEN_EXCHANGE_2");
const ABI_EXCHANGE_UNDERLYING = await getABI("ABI_EXCHANGE_UNDERLYING");
const ABI_ADD_LIQUIDITY = await getABI("ABI_ADD_LIQUIDITY");
const ABI_REMOVE_LIQUIDITY = await getABI("ABI_REMOVE_LIQUIDITY");
const ABI_REMOVE_LIQUIDITY_ONE = await getABI("ABI_REMOVE_LIQUIDITY_ONE");
const ABI_REMOVE_LIQUIDITY_IMBALANCE = await getABI("ABI_REMOVE_LIQUIDITY_IMBALANCE");
const CURVE_POOLS = getCurvePools();
// first wave of susbscribing to Token Exchanges
async function activateRealTimeMonitoring(singlePoolModus, whiteListedPoolAddress) {
console.log("Real-Time-Monitoring active\n");
for (const POOL_ADDRESS of CURVE_POOLS) {
if (singlePoolModus) {
// for mvp modus, we only listen to events on a single pool (susd -> whiteListedPoolAddress)
if (POOL_ADDRESS.toLocaleLowerCase() !== whiteListedPoolAddress.toLocaleLowerCase()) continue;
}
// RemoveLiquidity
const CONTRACT_REMOVE_LIQUIDITY = new wsKEY1.eth.Contract(ABI_REMOVE_LIQUIDITY, POOL_ADDRESS);
try {
CONTRACT_REMOVE_LIQUIDITY.events
.RemoveLiquidity()
.on("data", async (data) => {
addAndWriteEventToRawLog(POOL_ADDRESS, "RemoveLiquidity", data);
if (collectionCompleteForProcessedLogs) await processRemoveLiquidity(data, POOL_ADDRESS);
})
.on("error", (error) => {
console.error("Error in RemoveLiquidity event: ", error);
});
} catch (err) {
console.log("err in fetching events for RemoveLiquidity", err.message);
}
// RemoveLiquidityOne
const CONTRACT_REMOVE_LIQUIDITY_ONE = new wsKEY2.eth.Contract(ABI_REMOVE_LIQUIDITY_ONE, POOL_ADDRESS);
try {
CONTRACT_REMOVE_LIQUIDITY_ONE.events
.RemoveLiquidityOne()
.on("data", async (data) => {
addAndWriteEventToRawLog(POOL_ADDRESS, "RemoveLiquidityOne", data);
if (collectionCompleteForProcessedLogs) await processRemoveLiquidityOne(data, POOL_ADDRESS);
})
.on("error", (error) => {
console.error("Error in RemoveLiquidityOne event: ", error);
});
} catch (err) {
console.log("err in fetching events for RemoveLiquidityOne", err.message);
}
// RemoveLiquidityImbalance
const CONTRACT_REMOVE_LIQUIDITY_IMBALANCE = new wsKEY3.eth.Contract(ABI_REMOVE_LIQUIDITY_IMBALANCE, POOL_ADDRESS);
try {
CONTRACT_REMOVE_LIQUIDITY_IMBALANCE.events
.RemoveLiquidityImbalance()
.on("data", async (data) => {
addAndWriteEventToRawLog(POOL_ADDRESS, "RemoveLiquidityImbalance", data);
if (collectionCompleteForProcessedLogs) await processRemoveLiquidityImbalance(data, POOL_ADDRESS);
})
.on("error", (error) => {
console.error("Error in RemoveLiquidityImbalance event: ", error);
});
} catch (err) {
console.log("err in fetching events for RemoveLiquidityImbalance", err.message);
}
// AddLiquidity
const CONTRACT_ADD_LIQUIDITY = new wsKEY4.eth.Contract(ABI_ADD_LIQUIDITY, POOL_ADDRESS);
try {
CONTRACT_ADD_LIQUIDITY.events
.AddLiquidity()
.on("data", async (data) => {
await new Promise((resolve) => setTimeout(resolve, 5000));
addAndWriteEventToRawLog(POOL_ADDRESS, "AddLiquidity", data);
if (collectionCompleteForProcessedLogs) await processAddLiquidity(data, POOL_ADDRESS);
})
.on("error", (error) => {
console.error("Error in AddLiquidity event: ", error);
});
} catch (err) {
console.log("err in fetching events for AddLiquidity", err.message);
}
// TokenExchange
const CONTRACT_TOKEN_EXCHANGE = new wsKEY5.eth.Contract(ABI_TOKEN_EXCHANGE, POOL_ADDRESS);
try {
CONTRACT_TOKEN_EXCHANGE.events
.TokenExchange()
.on("data", async (data) => {
addAndWriteEventToRawLog(POOL_ADDRESS, "TokenExchange", data);
if (collectionCompleteForProcessedLogs) await processTokenExchange(data, POOL_ADDRESS);
})
.on("error", (error) => {
console.error("Error in TokenExchange event: ", error);
});
} catch (err) {
console.log("err in fetching events for TokenExchange", err.message);
}
// TokenExchange2
const CONTRACT_TOKEN_EXCHANGE_2 = new wsKEY1.eth.Contract(ABI_TOKEN_EXCHANGE_2, POOL_ADDRESS);
try {
CONTRACT_TOKEN_EXCHANGE_2.events
.TokenExchange()
.on("data", async (data) => {
addAndWriteEventToRawLog(POOL_ADDRESS, "TokenExchange", data);
if (collectionCompleteForProcessedLogs) await processTokenExchange(data, POOL_ADDRESS);
})
.on("error", (error) => {
console.error("Error in TokenExchange2 event: ", error);
});
} catch (err) {
console.log("err in fetching events for TokenExchange2", err.message);
}
// TokenExchangeUnderlying
const CONTRACT_TOKEN_EXCHANGE_UNDERLYING = new wsKEY2.eth.Contract(ABI_EXCHANGE_UNDERLYING, POOL_ADDRESS);
try {
CONTRACT_TOKEN_EXCHANGE_UNDERLYING.events
.TokenExchangeUnderlying()
.on("data", async (data) => {
addAndWriteEventToRawLog(POOL_ADDRESS, "TokenExchangeUnderlying", data);
if (collectionCompleteForProcessedLogs) await processTokenExchange(data, POOL_ADDRESS, "TokenExchangeUnderlying");
})
.on("error", (error) => {
console.error("Error in TokenExchangeUnderlying event: ", error);
});
} catch (err) {
console.log("err in fetching events for TokenExchangeUnderlying", err.message);
}
}
}
// builds a string of fee amounts and the connected token, avoiding dollar for now
async function getFeesSimple(poolAddress, feeArray) {
let feeString = "";
for (let i = 0; i < feeArray.length; i++) {
const TOKEN_ADDRESS = await getTokenAddress(poolAddress, i);
const AMOUNT = await getCleanedTokenAmount(TOKEN_ADDRESS, feeArray[i]);
const NAME = await getTokenName(TOKEN_ADDRESS);
feeString += formatForPrint(AMOUNT) + " " + NAME + " | ";
}
feeString = feeString.substring(0, feeString.length - 3);
return "<i>" + feeString + "</i>";
}
async function getTokenAddressFromStake(poolAddress, blockNumber, coinAmount) {
let id = 0;
let ethSpotter = 0;
let tokenAddress;
while (true) {
tokenAddress = await getTokenAddress(poolAddress, id);
if (!tokenAddress) break;
if (isNullAddress(tokenAddress)) break;
if (isNativeEthAddress(tokenAddress)) ethSpotter = 1;
const TRANSFER_AMOUNTS = await getTokenTransfers(tokenAddress, blockNumber);
for (const TRANSFER_AMOUNT of TRANSFER_AMOUNTS) {
if (TRANSFER_AMOUNT === coinAmount) {
return tokenAddress;
}
}
id += 1;
}
if ((isNullAddress(tokenAddress) || !tokenAddress) && ethSpotter === 1) {
return ADDRESS_ETH;
} else {
return "0 transfers";
}
}
// RemoveLiquidity
async function processRemoveLiquidity(data, poolAddress) {
console.log("\npool", poolAddress, " | block", data.blockNumber.toString(), " | txHash", data.transactionHash, " | RemoveLiquidity");
}
// RemoveLiquidityOne
async function processRemoveLiquidityOne(data, poolAddress) {
const TX_HASH = data.transactionHash;
const BLOCK_NUMBER = data.blockNumber;
const PROVIDER = data.returnValues.provider;
let type;
console.log("\npool", poolAddress, " | block", data.blockNumber.toString(), " | txHash", TX_HASH, " | RemoveLiquidityOne");
if (data.event && data.event === "TokenExchange") return;
if (CURVE_POOLS.includes(PROVIDER)) {
const DATA = await checkForTokenExchangeUnderlying(PROVIDER, BLOCK_NUMBER, TX_HASH);
await processTokenExchange(DATA, PROVIDER, "TokenExchangeUnderlying");
return;
}
let coinAmount = Number(data.returnValues.coin_amount);
const TOKEN_REMOVED_ADDRESS = await getTokenAddressFromStake(poolAddress, BLOCK_NUMBER, coinAmount);
if (TOKEN_REMOVED_ADDRESS === "0 transfers") return;
coinAmount = await getCleanedTokenAmount(TOKEN_REMOVED_ADDRESS, coinAmount);
const TOKEN_REMOVED_NAME = await getTokenName(TOKEN_REMOVED_ADDRESS);
const TX = await getTx(TX_HASH);
// might be exchange_multiple
if (isCurveRegistryExchange(TX.to)) {
const DECODED_TX = ABI_DECODER.decodeMethod(TX.input);
const METHOD_NAME = DECODED_TX.name;
if (METHOD_NAME === "exchange_multiple") {
const ROUTE = DECODED_TX.params[0].value;
for (const poolAddress of ROUTE) {
if (isNullAddress(poolAddress)) continue;
const DATA = await checkForTokenExchange(poolAddress, BLOCK_NUMBER, TX_HASH);
if (DATA === "empty") continue;
await processTokenExchange(DATA, poolAddress);
return;
}
}
}
// or just a simple exchange
if (isZapFor3poolMetapools(TX.to)) {
const DECODED_TX = ABI_DECODER.decodeMethod(TX.input);
if (DECODED_TX.name === "exchange") {
const _POOL_ADDRESS = DECODED_TX.params[0].value;
const DATA_EXCHANGE = await checkForTokenExchange(_POOL_ADDRESS, BLOCK_NUMBER, TX_HASH);
if (data !== "empty") {
const BUYER = TX.from;
const POSITION = TX.transactionIndex;
const TO = TX.to;
const POOL_NAME = await buildPoolName(_POOL_ADDRESS);
const SOLD_ADDRESS = await getTokenAddress(_POOL_ADDRESS, DECODED_TX.params[1].value);
const TOKEN_SOLD_NAME = await getTokenName(SOLD_ADDRESS);
let soldAmount = DATA_EXCHANGE.returnValues.tokens_sold;
soldAmount = await getCleanedTokenAmount(SOLD_ADDRESS, soldAmount);
const BOUGHT_ADDRESS = await getTokenAddress(poolAddress, DECODED_TX.params[2].value - 1);
const TOKEN_BOUGHT_NAME = await getTokenName(BOUGHT_ADDRESS);
let boughtAmount = data.returnValues.coin_amount;
boughtAmount = await getCleanedTokenAmount(BOUGHT_ADDRESS, boughtAmount);
if (POSITION > 7) {
await buildSwapMessage(
BLOCK_NUMBER,
SOLD_ADDRESS,
soldAmount,
BOUGHT_ADDRESS,
boughtAmount,
TOKEN_SOLD_NAME,
TOKEN_BOUGHT_NAME,
_POOL_ADDRESS,
TX_HASH,
BUYER,
TO,
POSITION,
POOL_NAME
);
} else {
console.log("blockPosition", POSITION, TX_HASH);
const extraData = {
soldAmount: soldAmount,
boughtAmount: boughtAmount,
tokenSoldName: TOKEN_SOLD_NAME,
soldAddress: SOLD_ADDRESS,
tokenBoughtName: TOKEN_BOUGHT_NAME,
boughtAddress: BOUGHT_ADDRESS,
poolAddress: _POOL_ADDRESS,
txHash: TX_HASH,
buyer: BUYER,
to: TO,
position: POSITION,
poolName: POOL_NAME,
};
await mevBuffer(BLOCK_NUMBER, POSITION, TX_HASH, "classicCurveMonitor", extraData, type, data);
}
return;
}
}
if (DECODED_TX.name === "remove_liquidity_one_coin") {
poolAddress = DECODED_TX.params[0].value;
}
}
const AGENT_ADDRESS = TX.from;
const TO = TX.to;
const POSITION = TX.transactionIndex;
if (POSITION > 7) {
tempTxHashStorage.push({
TX_HASH,
time: getUnixtime(),
});
await buildRemovalMessage(BLOCK_NUMBER, coinAmount, TOKEN_REMOVED_NAME, poolAddress, TX_HASH, AGENT_ADDRESS, POSITION, TOKEN_REMOVED_ADDRESS);
} else {
console.log("blockPosition", POSITION, TX_HASH);
const extraData = {
coinAmount: coinAmount,
tokenRemovedName: TOKEN_REMOVED_NAME,
removedAddress: TOKEN_REMOVED_ADDRESS,
poolAddress: poolAddress,
txHash: TX_HASH,
agentAddress: AGENT_ADDRESS,
to: TO,
position: POSITION,
};
await mevBuffer(BLOCK_NUMBER, POSITION, TX_HASH, "Removal", extraData, type, data);
}
}
// RemoveLiquidityImbalance
async function processRemoveLiquidityImbalance(data, poolAddress) {
console.log("\npool", poolAddress, " | block", data.blockNumber.toString(), " | txHash", data.transactionHash, " | RemoveLiquidityImbalance");
const CURVE_JSON = JSON.parse(fs.readFileSync("./JSON/CurvePoolData.json"));
let type;
const TX_HASH = data.transactionHash;
const TX = await getTx(TX_HASH);
const POSITION = TX.transactionIndex;