forked from calvinheath/wheat-v1-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpbot.js
1473 lines (1353 loc) · 54.7 KB
/
gulpbot.js
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
const fs = require('fs')
require('dotenv').config();
const axios = require('axios')
const Web3 = require('web3');
const HDWalletProvider = require('@truffle/hdwallet-provider');
// process
function idle() {
return new Promise((resolve, reject) => { });
}
function sleep(delay) {
return new Promise((resolve, reject) => setTimeout(resolve, delay));
}
function abort(e) {
console.error(e || new Error('Program aborted'));
process.exit(1);
}
function exit() {
process.exit(0);
}
function interrupt(f) {
process.on('SIGINT', f);
process.on('SIGTERM', f);
process.on('SIGUSR1', f);
process.on('SIGUSR2', f);
process.on('uncaughtException', f);
process.on('unhandledRejection', f);
}
function entrypoint(main) {
const args = process.argv;
(async () => { try { await main(args); } catch (e) { abort(e); } exit(); })();
}
function randomInt(limit) {
return Math.floor(Math.random() * limit)
}
// conversion
function valid(amount, decimals) {
const regex = new RegExp(`^\\d+${decimals > 0 ? `(\\.\\d{1,${decimals}})?` : ''}$`);
return regex.test(amount);
}
function coins(units, decimals) {
if (!valid(units, 0)) throw new Error('Invalid amount');
if (decimals == 0) return units;
const s = units.padStart(1 + decimals, '0');
return s.slice(0, -decimals) + '.' + s.slice(-decimals);
}
function units(coins, decimals) {
if (!valid(coins, decimals)) throw new Error('Invalid amount');
let i = coins.indexOf('.');
if (i < 0) i = coins.length;
const s = coins.slice(i + 1);
return coins.slice(0, i) + s + '0'.repeat(decimals - s.length);
}
// web3
const privateKey = process.env['PRIVATE_KEY'] || '';
const ankrProjectId = process.env['ANKR_PROJECT_ID'] || '';
const ankrApikeyBscmain = process.env['ANKR_APIKEY_BSCMAIN'] || '';
const ankrApikeyBsctest = process.env['ANKR_APIKEY_BSCTEST'] || '';
const CHAIN_ID = {
'bscmain': 56,
'bsctest': 97,
'avaxmain': 43114,
'avaxtest': 43113,
};
const NETWORK_NAME = {
56: 'bscmain',
97: 'bsctest',
43114: 'avaxmain',
43113: 'avaxtest',
};
const ADDRESS_URL_PREFIX = {
'bscmain': 'https://bscscan.com/address/',
'bsctest': 'https://testnet.bscscan.com/address/',
'avaxmain': 'https://snowtrace.io/address/',
'avaxtest': 'https://testnet.snowtrace.io/address/',
};
const TX_URL_PREFIX = {
'bscmain': 'https://bscscan.com/tx/',
'bsctest': 'https://testnet.bscscan.com/tx/',
'avaxmain': 'https://snowtrace.io/tx/',
'avaxtest': 'https://testnet.snowtrace.io/tx/',
};
const NATIVE_SYMBOL = {
'bscmain': 'BNB',
'bsctest': 'BNB',
'avaxmain': 'AVAX',
'avaxtest': 'AVAX',
};
const LIMIT_GASPRICE = {
'bscmain': '5000000000',
'bsctest': '10000000000',
'avaxmain': '100000000000',
'avaxtest': '100000000000',
};
const HTTP_PROVIDER_URLS = {
'bscmain': [
'https://bsc-dataseed.binance.org/',
'https://bsc-dataseed1.defibit.io/',
'https://bsc-dataseed1.ninicoin.io/',
'https://bsc-dataseed2.defibit.io/',
'https://bsc-dataseed3.defibit.io/',
'https://bsc-dataseed4.defibit.io/',
'https://bsc-dataseed2.ninicoin.io/',
'https://bsc-dataseed3.ninicoin.io/',
'https://bsc-dataseed4.ninicoin.io/',
'https://bsc-dataseed1.binance.org/',
'https://bsc-dataseed2.binance.org/',
'https://bsc-dataseed3.binance.org/',
'https://bsc-dataseed4.binance.org/',
// 'https://apis.ankr.com/' + ankrApikeyBscmain + '/' + ankrProjectId + '/binance/full/main',
],
'bsctest': [
'https://data-seed-prebsc-1-s1.binance.org:8545/',
'https://data-seed-prebsc-2-s1.binance.org:8545/',
'https://data-seed-prebsc-1-s2.binance.org:8545/',
'https://data-seed-prebsc-2-s2.binance.org:8545/',
'https://data-seed-prebsc-1-s3.binance.org:8545/',
'https://data-seed-prebsc-2-s3.binance.org:8545/',
// 'https://apis.ankr.com/' + ankrApikeyBsctest + '/' + ankrProjectId + '/binance/full/test',
],
'avaxmain': [
'https://api.avax.network/ext/bc/C/rpc',
],
'avaxtest': [
'https://api.avax-test.network/ext/bc/C/rpc',
],
};
const web3Cache = {};
function getWeb3(privateKey, network) {
let web3 = web3Cache[network];
if (!web3) {
const index = randomInt(HTTP_PROVIDER_URLS[network].length);
const url = HTTP_PROVIDER_URLS[network][index];
const options = { transactionConfirmationBlocks: 0 };
web3 = new Web3(new HDWalletProvider(privateKey, url), null, options);
web3Cache[network] = web3;
}
return web3;
}
// telegram
function escapeHTML(message) {
return message
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>');
}
const telegramBotApiKey = process.env['TELEGRAM_BOT_API_KEY'] || '';
const telegramBotChatId = process.env['TELEGRAM_BOT_CHAT_ID'] || '';
let lastTelegramMessage;
async function sendTelegramMessage(message, key = '') {
const filename = '/tmp/bridgebot-telegram-messages.json';
if (lastTelegramMessage === undefined) {
lastTelegramMessage = {};
try { lastTelegramMessage = JSON.parse(fs.readFileSync(filename)); } catch { }
}
if (message !== lastTelegramMessage[key]) {
console.log(new Date().toISOString());
console.log(message);
try {
const url = 'https://api.telegram.org/bot'+ telegramBotApiKey +'/sendMessage';
await axios.post(url, { chat_id: telegramBotChatId, text: message, parse_mode: 'HTML', disable_web_page_preview: true });
lastTelegramMessage[key] = message;
} catch (e) {
console.error('FAILURE', e.message);
}
}
try { fs.writeFileSync(filename, JSON.stringify(lastTelegramMessage, undefined, 2)); } catch { }
}
// lib
const IERC20_ABI = require('../build/contracts/IERC20.json').abi;
const MASTERCHEF_ABI = require('../build/contracts/CustomMasterChef.json').abi;
const MASTERCHEFJOE_ABI = require('../build/contracts/MasterChefJoe.json').abi;
const STRATEGY_ABI = require('../build/contracts/RewardCompoundingStrategyToken.json').abi;
const COLLECTOR_ADAPTER_ABI = require('../build/contracts/AutoFarmFeeCollectorAdapter.json').abi;
const COLLECTOR_ABI = require('../build/contracts/FeeCollector.json').abi;
const BUYBACK_ABI = require('../build/contracts/Buyback.json').abi;
const UNIVERSAL_BUYBACK_ABI = require('../build/contracts/UniversalBuyback.json').abi;
const MASTERCHEF_ADDRESS = {
'bscmain': '0x95fABAe2E9Fb0A269cE307550cAC3093A3cdB448',
'bsctest': '0xF4748df5D63F6AB01e276065E6bD098Ce8dEA98a',
'avaxmain': '0x5818388B1d756090FF1545D943fF9D1F6Ea13ce3',
'avaxtest': '0x0000000000000000000000000000000000000000',
};
function getDefaultAccount(privateKey, network) {
const web3 = getWeb3(privateKey, network);
const [account] = web3.currentProvider.getAddresses();
return account;
}
async function getNonce(privateKey, network, account = null) {
const web3 = getWeb3(privateKey, network);
if (account === null) [account] = web3.currentProvider.getAddresses();
try {
const nonce = await web3.eth.getTransactionCount(account);
return nonce;
} catch (e) {
throw new Error(e.message);
}
}
async function getNativeBalance(privateKey, network, account = null) {
const web3 = getWeb3(privateKey, network);
if (account === null) [account] = web3.currentProvider.getAddresses();
try {
const amount = await web3.eth.getBalance(account);
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function getTokenBalance(privateKey, network, address, account = null) {
const web3 = getWeb3(privateKey, network);
const abi = IERC20_ABI;
const contract = new web3.eth.Contract(abi, address);
if (account === null) [account] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.balanceOf(account).call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function getTokenSymbol(privateKey, network, address) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
try {
const symbol = await contract.methods.symbol().call();
return symbol;
} catch (e) {
throw new Error(e.message);
}
}
async function getPendingBalance(privateKey, network, address, pid, account = null) {
const web3 = getWeb3(privateKey, network);
const abi = MASTERCHEF_ABI;
const contract = new web3.eth.Contract(abi, address);
if (account === null) [account] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingCake(pid, account).call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function getPendingBalanceJoe(privateKey, network, address, pid, account = null) {
const web3 = getWeb3(privateKey, network);
const abi = MASTERCHEFJOE_ABI;
const contract = new web3.eth.Contract(abi, address);
if (account === null) [account] = web3.currentProvider.getAddresses();
try {
const { _pendingJoe: amount } = await contract.methods.pendingTokens(pid, account).call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingReward(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingReward().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function performanceFee(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.performanceFee().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingPerformanceFee(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingPerformanceFee().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function getCollector(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const collector = await contract.methods.collector().call();
return collector;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingDeposit(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = COLLECTOR_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingDeposit().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function getBuyback(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = COLLECTOR_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const buyback = await contract.methods.buyback().call();
return buyback;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingBuyback(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = BUYBACK_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingBuyback().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingSource(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = COLLECTOR_ADAPTER_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingSource().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingTarget(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = COLLECTOR_ADAPTER_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const amount = await contract.methods.pendingTarget().call();
return amount;
} catch (e) {
throw new Error(e.message);
}
}
async function pendingBurning(privateKey, network, address, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = UNIVERSAL_BUYBACK_ABI;
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const { _burning1, _burning2 } = await contract.methods.pendingBurning().call();
return [_burning1, _burning2];
} catch (e) {
throw new Error(e.message);
}
}
async function gulp0(privateKey, network, address, nonce) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
const [from] = web3.currentProvider.getAddresses();
let txId = null;
try {
const estimatedGas = await contract.methods.gulp().estimateGas({ from, nonce });
const gas = 2 * estimatedGas;
const gasPrice = await web3.eth.getGasPrice();
if (BigInt(gasPrice) > BigInt(LIMIT_GASPRICE[network])) {
throw new Error('Gas price beyond the set limit');
}
await contract.methods.gulp().send({ from, nonce, gas })
.on('transactionHash', (hash) => {
txId = hash;
});
} catch (e) {
throw new Error(e.message);
}
if (txId === null) throw new Error('Failure reading txId');
return txId;
}
async function gulp1(privateKey, network, address, amount, nonce) {
const web3 = getWeb3(privateKey, network);
const abi = COLLECTOR_ADAPTER_ABI;
const contract = new web3.eth.Contract(abi, address);
const [from] = web3.currentProvider.getAddresses();
let txId = null;
try {
const estimatedGas = await contract.methods.gulp(amount).estimateGas({ from, nonce });
const gas = 2 * estimatedGas;
const gasPrice = await web3.eth.getGasPrice();
if (BigInt(gasPrice) > BigInt(LIMIT_GASPRICE[network])) {
throw new Error('Gas price beyond the set limit');
}
await contract.methods.gulp(amount).send({ from, nonce, gas })
.on('transactionHash', (hash) => {
txId = hash;
});
} catch (e) {
throw new Error(e.message);
}
if (txId === null) throw new Error('Failure reading txId');
return txId;
}
async function gulp2(privateKey, network, address, amount1, amount2, nonce) {
const web3 = getWeb3(privateKey, network);
const abi = UNIVERSAL_BUYBACK_ABI;
const contract = new web3.eth.Contract(abi, address);
const [from] = web3.currentProvider.getAddresses();
let txId = null;
try {
const estimatedGas = await contract.methods.gulp(amount1, amount2).estimateGas({ from, nonce });
const gas = 2 * estimatedGas;
const gasPrice = await web3.eth.getGasPrice();
if (BigInt(gasPrice) > BigInt(LIMIT_GASPRICE[network])) {
throw new Error('Gas price beyond the set limit');
}
await contract.methods.gulp(amount1, amount2).send({ from, nonce, gas })
.on('transactionHash', (hash) => {
txId = hash;
});
} catch (e) {
throw new Error(e.message);
}
if (txId === null) throw new Error('Failure reading txId');
return txId;
}
async function poolLength(privateKey, network, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = MASTERCHEF_ABI;
const address = MASTERCHEF_ADDRESS[network];
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const length = await contract.methods.poolLength().call();
return length;
} catch (e) {
throw new Error(e.message);
}
}
async function poolInfo(privateKey, network, pid, agent = null) {
const web3 = getWeb3(privateKey, network);
const abi = MASTERCHEF_ABI;
const address = MASTERCHEF_ADDRESS[network];
const contract = new web3.eth.Contract(abi, address);
if (agent === null) [agent] = web3.currentProvider.getAddresses();
try {
const { lpToken } = await contract.methods.poolInfo(pid).call();
return { lpToken };
} catch (e) {
throw new Error(e.message);
}
}
// app
const ACTIVE_PIDS = [
5,
// 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51
];
const MONITORING_INTERVAL = 15; // 15 seconds
/*
const DEFAULT_GULP_INTERVAL = 12 * 60 * 60; // 12 hours
const GULP_INTERVAL = {
// 5 - stkCAKE
'0x84BA65DB2da175051E25F86e2f459C863CBb3E0C': 24 * 60 * 60, // 24 hours
// 18 - stkBNB/CAKE
// '0x4291474e88E2fEE6eC5B8c28F4Ed2075cEf5B803': 12 * 60 * 60, // 12 hours
// 19 - stkBNB/BUSD
// '0xdC4D358B34619e4fE7feb28bE301B2FBe4F3aFf9': 24 * 60 * 60, // 24 hours
// 20 - stkBNB/BTCB
// '0xA561fa603bf0B43Cb0d0911EeccC8B6777d3401B': 24 * 60 * 60, // 24 hours
// 21 - stkBNB/ETH
// '0x28e6aa3DD98372Da0959Abe9d0efeB4455d4dFe1': 24 * 60 * 60, // 24 hours
// 22 - stkBNB/LINK
// '0x3B88a64D0B9fA485B71c98B00D799aa8D1aEe9E3': 24 * 60 * 60, // 24 hours
// 23 - stkBNB/UNI
// '0x515785CE5D5e94f93fe41Ed3fd83779Fb3Aff8A4': 24 * 60 * 60, // 24 hours
// 24 - stkBNB/DOT
// '0x53073f685474341cdc765F97E7CFB2F427BD9db9': 24 * 60 * 60, // 24 hours
// 25 - stkBNB/ADA
// '0xf5aFfe3459813AB193329E53f17098806709046A': 24 * 60 * 60, // 24 hours
// 26 - stkBUSD/UST
// '0x5141da4ab5b3e13ceE7B10980aE6bB848FdB59Cd': 24 * 60 * 60, // 24 hours
// 27 - stkBUSD/DAI
// '0x691e486b5F7E39e90d37485164fAbDDd93aE43cD': 24 * 60 * 60, // 24 hours
// 28 - stkBUSD/USDC
// '0xae35A19F1DAc62AD3794773D5f0983f05073D0f2': 24 * 60 * 60, // 24 hours
// 33 - stkBNB/CAKEv2
'0x86c15Efe94320Cd139eA4875b7ceF336e1F91f16': 36 * 60 * 60, // 36 hours
// 34 - stkBNB/BUSDv2
'0xd5ffd8318b1c82FDE321f7BC1a553462A13A2E14': 36 * 60 * 60, // 36 hours
// 35 - stkBNB/USDTv2
'0x7259CeBc6D8f84afdce4B81a3a33D53A526521F8': 72 * 60 * 60, // 72 hours
// 36 - stkBNB/BTCBv2
'0x074fD0f3289cF3F5E0E80c969F62B21cB38Ad3b5': 72 * 60 * 60, // 72 hours
// 37 - stkBNB/ETHv2
'0x15B310c8D9d0Ac9aefB94BF492e7eAbC43B4f93e': 72 * 60 * 60, // 72 hours
// 38 - stkBUSD/USDTv2
'0x6f1c4303bC40AEee0aa60dD90e4eeC353487b66f': 72 * 60 * 60, // 72 hours
// 39 - stkBUSD/VAIv2
'0xC8daDd57BD9342b7ba9449B952DBE11B4f3D1648': 72 * 60 * 60, // 72 hours
// 40 - stkBNB/DOTv2
'0x5C96941B28B824c3E9d01E5cb2D77B3f7801560e': 72 * 60 * 60, // 72 hours
// 41 - stkBNB/LINKv2
'0x501382584a3DBF1471918Cd4ee0fd3bE23FfDF29': 72 * 60 * 60, // 72 hours
// 42 - stkBNB/UNIv2
'0x0900a05910E7d4811f9FC17843120D6412df2968': 72 * 60 * 60, // 72 hours
// 43 - stkBNB/DODOv2
'0x67A4c8d130ED95fFaB9F2CDf001811Ada1077875': 72 * 60 * 60, // 72 hours
// 44 - stkBNB/ALPHAv2
'0x6C6d105066462EE9b5Cfc7628e2edB1000e887F1': 72 * 60 * 60, // 72 hours
// 45 - stkBNB/ADAv2
'0x73099318dfBB1C59e473322F29C215132A14Ab86': 72 * 60 * 60, // 72 hours
// 46 - stkBUSD/USTv2
'0xB2b5dba919Da2E06d6cDd15dF17bA4b99D3eB1bD': 72 * 60 * 60, // 72 hours
// 47 - stkBUSD/BTCBv2
'0xf30D01da4257c696e537E2fdF0a2Ce6C9D627352': 72 * 60 * 60, // 72 hours
// 48 - stkbeltBNBv2
'0xeC97D2e53e34Aa8E5C6a843D9cd74641E645681A': 48 * 60 * 60, // 48 hours
// 49 - stkbeltBTCv2
'0x04abDB55DCd0167BFcE8FA0fA125F102c4734C62': 48 * 60 * 60, // 48 hours
// 50 - stkbeltETHv2
'0xE70aA236f2c2dABC346e193F606986Bb843bA3d9': 48 * 60 * 60, // 48 hours
// 51 - stk4BELTv2
'0xeB8e1c316694742E7042882be1ac55ebbD2bCEbB': 48 * 60 * 60, // 48 hours
// - stkBNB/BUSDv2
'0x4046492479a5bA18c2a947A1db75f4f1ef227BF1': 72 * 60 * 60, // 72 hours
// - stkBNB/BTCBv2
'0xc1d3F1dB60DE17afD7770464BAb05c58129d7Ee0': 72 * 60 * 60, // 72 hours
// - stkBNB/ETHv2
'0x9C009595F330CA8070e78b889183e7b8a96cB962': 72 * 60 * 60, // 72 hours
// - stkBNB/CAKEv2
'0x1f48dCbCE7fC91180492a7b083472924b4e8a44b': 72 * 60 * 60, // 72 hours
// - stkBUSD/USDCv2
'0xd802621F65Bd96D76e84E49EecdED49C5acb105d': 72 * 60 * 60, // 72 hours
// - stkBNB/USDTv2
'0xE0327dA3f94Efe600569Ca68Aa02e6921FD89Bfa': 72 * 60 * 60, // 27 hours
// - stkBNB/PANTHERv2
'0x358582CEeeB0F008495C06206973F5F6e495accd': 24 * 60 * 60, // 24 hours
// - stkBUSD/PANTHERv2
'0x1A51686Fb42861AA7E38c1CF8868877F43F82aA4': 24 * 60 * 60, // 24 hours
// CAKE collector
'0x14bAc5f216337F8da5f41Bb920514Af98ef62c36': 24 * 60 * 60, // 24 hours
// AUTO/CAKE collector adapter
'0x626E98ef225A6f79523C9004E8731B793dfd0F68': 48 * 60 * 60, // 48 hours
// CAKE buyback
'0xC351706C3212D45fc24F6B89e686f07fAb048b16': 24 * 60 * 60, // 24 hours
// PANTHER buyback adapter
'0x495089390569d47807F1Db83F14e053002DB25b4': 48 * 60 * 60, // 48 hours
// Universal buyback
'0x01d1c4eC99D0A7D8f4141D42D1624fffa054D7Ae': 48 * 60 * 60, // 48 hours
};
*/
const strategyCache = {};
async function readStrategy(privateKey, network, pid) {
if (strategyCache[pid]) return strategyCache[pid];
const { lpToken: strategy } = await poolInfo(privateKey, network, pid);
strategyCache[pid] = strategy;
return strategy;
}
const collectorCache = {};
async function readCollector(privateKey, network, strategy) {
if (collectorCache[strategy]) return collectorCache[strategy];
const collector = await getCollector(privateKey, network, strategy);
collectorCache[strategy] = collector;
return collector;
}
const buybackCache = {};
async function readBuyback(privateKey, network, collector) {
if (collectorCache[collector]) return collectorCache[collector];
const buyback = await getBuyback(privateKey, network, collector);
collectorCache[collector] = buyback;
return buyback;
}
let lastGulp = {};
function readLastGulp(network) {
try { lastGulp = JSON.parse(fs.readFileSync('gulpbot-' + network + '.json')); } catch (e) { }
}
function writeLastGulp(network) {
try { fs.writeFileSync('gulpbot-' + network + '.json', JSON.stringify(lastGulp, undefined, 2)); } catch (e) { }
}
async function safeGulp(privateKey, network, address) {
const now = Date.now();
/*
const timestamp = lastGulp[address] || 0;
const ellapsed = (now - timestamp) / 1000;
const interval = GULP_INTERVAL[address] || DEFAULT_GULP_INTERVAL;
if (ellapsed < interval) return null;
*/
const nonce = await getNonce(privateKey, network);
try {
let messages = [address];
try { const txId = await gulp0(privateKey, network, address, nonce); return txId; } catch (e) { messages.push(e.message); }
try { const txId = await gulp1(privateKey, network, address, 0, nonce); return txId; } catch (e) { messages.push(e.message); }
try { const txId = await gulp2(privateKey, network, address, 0, 0, nonce); return txId; } catch (e) { messages.push(e.message); }
throw new Error(messages.join('\n'));
} finally {
lastGulp[address] = now;
writeLastGulp(network);
}
}
async function listContracts(privateKey, network) {
const length = await poolLength(privateKey, network);
console.log('pid', 'strategy', 'collector', 'buyback');
for (let pid = 0; pid < length; pid++) {
if (!ACTIVE_PIDS.includes(pid)) continue;
const strategy = await readStrategy(privateKey, network, pid);
const collector = await readCollector(privateKey, network, strategy);
const buyback = await readBuyback(privateKey, network, collector);
console.log(pid, strategy, collector, buyback);
}
}
async function fixTwap(privateKey, network, address, exchange, rewardToken, routingToken, reserveToken) {
const web3 = getWeb3(privateKey, network);
const abi = STRATEGY_ABI;
const contract = new web3.eth.Contract(abi, address);
const [from] = web3.currentProvider.getAddresses();
try {
await contract.methods.gulp().estimateGas({ from });
} catch {
const abi = [
{
name: "oracleAveragePriceFactorFromInput",
type: 'function',
inputs: [
{ name: '_from', type: 'address' },
{ name: '_to', type: 'address' },
{ name: '_inputAmount', type: 'uint256' },
],
outputs: [
{ name: '_factor', type: 'uint256' },
],
},
{
name: 'oraclePoolAveragePriceFactorFromInput',
type: 'function',
inputs: [
{ name: '_pool', type: 'address' },
{ name: '_token', type: 'address' },
{ name: '_inputAmount', type: 'uint256' },
],
outputs: [
{ name: '_factor', type: 'uint256' },
],
},
];
const gas = 300000;
const contract = new web3.eth.Contract(abi, exchange, { from, gas });
if (rewardToken !== routingToken) {
try {
const gasPrice = await web3.eth.getGasPrice();
if (BigInt(gasPrice) > BigInt(LIMIT_GASPRICE[network])) {
throw new Error('Gas price beyond the set limit');
}
await contract.methods.oracleAveragePriceFactorFromInput(rewardToken, routingToken, '10000000000').estimateGas({ from });
let txId = null;
await contract.methods.oracleAveragePriceFactorFromInput(rewardToken, routingToken, '10000000000').send()
.on('receipt', ({ transactionHash }) => { txId = transactionHash; });
if (txId === null) throw new Error('Invalid txId');
} catch (e) {
throw Error('TWAP pool update ' + address + ': ' + exchange + ': ' + rewardToken + ': ' + routingToken + ': ' + e.message);
}
}
if (reserveToken !== routingToken) {
try {
const gasPrice = await web3.eth.getGasPrice();
if (BigInt(gasPrice) > BigInt(LIMIT_GASPRICE[network])) {
throw new Error('Gas price beyond the set limit');
}
await contract.methods.oraclePoolAveragePriceFactorFromInput(reserveToken, routingToken, '10000000000').estimateGas({ from });
let txId = null;
await contract.methods.oraclePoolAveragePriceFactorFromInput(reserveToken, routingToken, '10000000000').send()
.on('receipt', ({ transactionHash }) => { txId = transactionHash; });
if (txId === null) throw new Error('Invalid txId');
} catch (e) {
throw Error('TWAP pool update ' + address + ': ' + exchange + ': ' + reserveToken + ': ' + routingToken + ': ' + e.message);
}
}
}
}
async function gulpAll(privateKey, network) {
if (network === 'avaxmain') {
const WHEAT = '0x1A51686Fb42861AA7E38c1CF8868877F43F82aA4';
const WHEAT_MASTERCHEF = '0x5818388B1d756090FF1545D943fF9D1F6Ea13ce3';
const JOE = '0x6e84a6216eA6dACC71eE8E6b0a5B7322EEbC0fDd';
const JOE_MASTERCHEF_V2 = '0xd6a4F121CA35509aF06A0Be99093d08462f53052';
const JOE_MASTERCHEF_V3 = '0x188bED1968b795d5c9022F6a0bb5931Ac4c18F00';
const WAVAX = '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7';
const WETH = '0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB';
const WBTC = '0x50b7545627a5162F82A992c33b87aDc75187B218';
const USDC = '0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664';
const XJOE = '0x57319d41F71E81F3c65F2a47CA4e001EbAFd4F33';
const CRV = '0x47536F17F4fF30e64A96a7555826b8f9e66ec468';
const JOE_EXCHANGE = '0xB85B290B99B10dCd78189317b5bF965854bdc952';
const TDJ_AVAX_JOE = '0x454E67025631C065d3cFAD6d71E6892f74487a15';
const TDJ_AVAX_WETH = '0xFE15c2695F1F920da45C30AAE47d11dE51007AF9';
const TDJ_AVAX_WBTC = '0xd5a37dC5C9A396A03dd1136Fc76A1a02B1c88Ffa';
const TDJ_AVAX_DAI = '0x87Dee1cC9FFd464B79e058ba20387c1984aed86a';
const TDJ_AVAX_USDC = '0xA389f9430876455C36478DeEa9769B7Ca4E3DDB1';
const TDJ_AVAX_USDT = '0xeD8CBD9F0cE3C6986b22002F03c6475CEb7a6256';
const TDJ_AVAX_LINK = '0x6F3a0C89f611Ef5dC9d96650324ac633D02265D3';
const TDJ_AVAX_MIM = '0x781655d802670bbA3c89aeBaaEa59D3182fD755D';
const TDJ_USDC_JOE = '0x67926d973cD8eE876aD210fAaf7DFfA99E414aCf';
const TDJ_USDT_JOE = '0x1643de2efB8e35374D796297a9f95f64C082a8ce';
const stkUSDCv3 = '0xdC4D358B34619e4fE7feb28bE301B2FBe4F3aFf9';
{
// JOE strategies
const addresses = [
// 24 - xJOE
[24, '0x2E53F47a807F0c45c61C899e62504336B0b5dBDf', JOE, XJOE, JOE_MASTERCHEF_V2],
// 0 - stkAVAX/JOEv3
[0, '0x04abDB55DCd0167BFcE8FA0fA125F102c4734C62', JOE, TDJ_AVAX_JOE, JOE_MASTERCHEF_V3],
// 26 - stkAVAX/WETHv3
[26, '0xE70aA236f2c2dABC346e193F606986Bb843bA3d9', WAVAX, TDJ_AVAX_WETH, JOE_MASTERCHEF_V2],
// 27 - stkAVAX/WBTCv3
[27, '0xeB8e1c316694742E7042882be1ac55ebbD2bCEbB', WAVAX, TDJ_AVAX_WBTC, JOE_MASTERCHEF_V2],
// 37 - stkAVAX/DAIv3
[37, '0xe46a3E475dd4D18A7D2A009b7FA1B113f2bd2903', WAVAX, TDJ_AVAX_DAI, JOE_MASTERCHEF_V2],
// 39 - stkAVAX/USDCv3
[39, '0xed98897f99F0Bb8732afF727f3DBD01bA95D25b4', WAVAX, TDJ_AVAX_USDC, JOE_MASTERCHEF_V2],
// 28 - stkAVAX/USDTv3
[28, '0x38fBF8e6ADbfa44072688BeBd921Fdc6b4b6f328', WAVAX, TDJ_AVAX_USDT, JOE_MASTERCHEF_V2],
// 29 - stkAVAX/LINKv3
[29, '0x235B1E29dc44bA33d0A6518a62A998bCFd8D62e9', WAVAX, TDJ_AVAX_LINK, JOE_MASTERCHEF_V2],
// 43 - stkAVAX/MIMv3
[43, '0xAFe0d419658dEbEF3f8eDc23b011877770594360', WAVAX, TDJ_AVAX_MIM, JOE_MASTERCHEF_V2],
// 58 - stkUSDC/JOEv3
[58, '0x0A0E72c79b5559B9266deD694bccf91d7C60c300', JOE, TDJ_USDC_JOE, JOE_MASTERCHEF_V2],
// 30 - stkUSDT/JOEv3
[30, '0x731010369d41147523e6ae65aD3dB4Dea1Eb13ff', JOE, TDJ_USDT_JOE, JOE_MASTERCHEF_V2],
];
for (const [pid, address, routingToken, reserveToken, masterChef] of addresses) {
await fixTwap(privateKey, network, address, JOE_EXCHANGE, JOE, routingToken, reserveToken);
const amount1 = await getTokenBalance(privateKey, network, JOE, address);
const amount2 = await getPendingBalanceJoe(privateKey, network, masterChef, pid, address);
const MINIMUM_AMOUNT = 250000000000000000000n; // 250 JOE
if (BigInt(amount1) + BigInt(amount2) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
const name = await getTokenSymbol(privateKey, network, address);
return { name, type: 'TraderJoeStrategy', address, tx };
}
}
}
}
{
// WHEAT strategies
const addresses = [
// 0 - stkWHEATv3
[0, '0x01d1c4eC99D0A7D8f4141D42D1624fffa054D7Ae', WHEAT, WHEAT],
];
for (const [pid, address, routingToken, reserveToken] of addresses) {
const amount1 = await getTokenBalance(privateKey, network, WHEAT, address);
const amount2 = await getPendingBalance(privateKey, network, WHEAT_MASTERCHEF, pid, address);
const MINIMUM_AMOUNT = 1000000000000000000000n; // 1000 WHEAT
if (BigInt(amount1) + BigInt(amount2) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
const name = await getTokenSymbol(privateKey, network, address);
return { name, type: 'WheatStrategy', address, tx };
}
}
}
}
{
// JOE SPLITTING ADAPTER
const address = '0x67E5830CCef2Dc38aebEf4eC7D06baD5b5957D44';
const amount = await getTokenBalance(privateKey, network, JOE, address);
const MINIMUM_AMOUNT = 1000000000000000000000n; // 1000 JOE
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'JOE', type: 'SplittingAdapter', address, tx };
}
}
}
{
// PSM INJECTOR stkUSDCv3
const address = '0x5622C4A8F6B245aFdddA6c32748055837A2616Cc';
const amount = await getTokenBalance(privateKey, network, stkUSDCv3, address);
const MINIMUM_AMOUNT = 5000000000n; // 5000 stkUSDCv3
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'stkUSDCv3', type: 'PsmInjector', address, tx };
}
}
}
{
// PSM INJECTOR stkUSDLPv3 using USDC
const address = '0x4EcD4082C1E809D89901cD3A802409c8d2Ae6EC4';
const amount = await getTokenBalance(privateKey, network, USDC, address);
const MINIMUM_AMOUNT = 5000000000n; // 5000 USDC
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'stkUSDLPv3', type: 'PsmInjector', address, tx };
}
}
}
{
// stkUSDLPv3 CRV/WAVAX adapter
const address = '0x744ed0c7453b4332885e650cE66611cEd9f6e50b';
const amount1 = await getTokenBalance(privateKey, network, CRV, address);
const amount2 = await getTokenBalance(privateKey, network, WAVAX, address);
const MINIMUM_AMOUNT1 = 500000000000000000000n; // 500 CRV
const MINIMUM_AMOUNT2 = 25000000000000000000n; // 25 WAVAX
if (BigInt(amount1) >= MINIMUM_AMOUNT1 || BigInt(amount2) >= MINIMUM_AMOUNT2) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'CRV+WAVAX', type: 'TraderJoeCurveAdapter', address, tx };
}
}
}
{
// AVAX/JOE collector
const address = '0xa6E48716e4426ba51C2459c890aDd6604f96404F';
const amount = await getTokenBalance(privateKey, network, TDJ_AVAX_JOE, address);
const MINIMUM_AMOUNT = 100000000000000000000n; // 100 AVAX/JOE
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'AVAX/JOE', type: 'TraderJoeCollector', address, tx };
}
}
}
{
// WAVAX collector
const address = '0xa8FDF216A180A798ED783e14EE71557E613C52bf';
const amount = await getTokenBalance(privateKey, network, WAVAX, address);
const MINIMUM_AMOUNT = 25000000000000000000n; // 25 WAVAX
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'WAVAX', type: 'TraderJoeCollector', address, tx };
}
}
}
{
// WETH collector
const address = '0x42C03f6d6c00f0D5603080a3475903195e9F6CEc';
const amount = await getTokenBalance(privateKey, network, WETH, address);
const MINIMUM_AMOUNT = 500000000000000000n; // 0.5 WETH
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'WETH', type: 'TraderJoeCollector', address, tx };
}
}
}
{
// WBTC collector
const address = '0xCfc7E18096C96Bf33203CCc2781E1D1f8fe77Ceb';
const amount = await getTokenBalance(privateKey, network, WBTC, address);
const MINIMUM_AMOUNT = 50000000000000000n; // 0.05 WBTC
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'WBTC', type: 'TraderJoeCollector', address, tx };
}
}
}
{
// JOE buyback
const address = '0xae35A19F1DAc62AD3794773D5f0983f05073D0f2';
const amount = await getTokenBalance(privateKey, network, JOE, address);
const MINIMUM_AMOUNT = 250000000000000000000n; // 250 JOE
if (BigInt(amount) >= MINIMUM_AMOUNT) {
const tx = await safeGulp(privateKey, network, address);
if (tx !== null) {
return { name: 'JOE', type: 'TraderJoeBuyback', address, tx };
}
}
}
return false;
}
if (network === 'bscmain') {
// NEW SYSTEM v3
const CAKE = '0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82';
const CAKE_MASTERCHEF = '0x73feaa1eE314F8c655E354234017bE2193C9E24E';
const CAKE_EXCHANGE = '0xf31366e709C8d75BCC6e3Bc172E46831D6A4C2B3';
const BANANA = '0x603c7f932ED1fc6575303D8Fb018fDCBb0f39a95';
const BANANA_MASTERCHEF = '0x5c8D727b265DBAfaba67E050f2f739cAeEB4A6F9';
const BANANA_EXCHANGE = '0x39c54945C9B880d4B5F15b0BA3c8c17226C37d68';
const WHEAT = '0x3ab63309F85df5D4c3351ff8EACb87980E05Da4E';
const WHEAT_MASTERCHEF = '0x95fABAe2E9Fb0A269cE307550cAC3093A3cdB448';
const BNB = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';
const ETH = '0x2170Ed0880ac9A755fd29B2688956BD959F933F8';
const BUSD = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56';
const USDT = '0x55d398326f99059fF775485246999027B3197955';
const PCS_BNB_CAKE = '0x0eD7e52944161450477ee417DE9Cd3a859b14fD0';
const PCS_BNB_BUSD = '0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16';
const PCS_BUSD_USDT = '0x7EFaEf62fDdCCa950418312c6C91Aef321375A00';