-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkLiquidityPath.js
111 lines (92 loc) · 4.16 KB
/
checkLiquidityPath.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
const { getLiquidityV2, getLiquidityAverageV2, getRollingVolatility } = require('../src/data.interface/data.interface');
const { watchedPairs, specificPivotsOverride } = require('../src/global.config');
const { BLOCK_PER_DAY } = require('../src/utils/constants');
const { roundTo } = require('../src/utils/utils');
const { getPriceAtBlock } = require('../src/data.interface/internal/data.interface.price');
const { ethers } = require('ethers');
const fs = require('fs');
function getPermutations(array) {
if (array.length === 0) return [[]];
const firstElement = array[0];
const rest = array.slice(1);
const permutationsWithoutFirst = getPermutations(rest);
const allPermutations = [];
permutationsWithoutFirst.forEach(permutation => {
for (let i = 0; i <= permutation.length; i++) {
const permutationWithFirst = [...permutation.slice(0, i), firstElement, ...permutation.slice(i)];
allPermutations.push(permutationWithFirst);
}
});
return allPermutations;
}
async function checkLiquidity() {
const web3Provider = new ethers.providers.StaticJsonRpcProvider('https://nameless-tame-panorama.bsc.quiknode.pro/b87b894fe24ed3fbe12b2b14e1fd6931677cab09/');
const baseTokens = ['USDT', 'WBNB', 'ETH'];
const allPermutations = getPermutations(baseTokens);
const block = await web3Provider.getBlockNumber();
const platform = 'all';
let newSpecificPivotsOverride = {};
if (fs.existsSync('bestPerms.json')) {
newSpecificPivotsOverride = JSON.parse(fs.readFileSync('bestPerms.json'));
}
const pairsToFetch = [];
for (const base of Object.keys(watchedPairs)) {
for (const quoteCfg of watchedPairs[base]) {
if (!quoteCfg.exportToInternalDashboard) continue;
const quote = quoteCfg.quote;
pairsToFetch.push({
base,
quote
});
pairsToFetch.push({
base: quote,
quote: base
});
}
}
for (const pairToFetch of pairsToFetch) {
const base = pairToFetch.base;
const quote = pairToFetch.quote;
if (newSpecificPivotsOverride[`${base}/${quote}`] &&
newSpecificPivotsOverride[`${base}/${quote}`].length > 0) {
console.log(`${base}/${quote} already done`);
continue;
}
let bestPermutation = [];
let bestValue = 0;
for (const pivotPermutation of allPermutations) {
specificPivotsOverride[`${base}/${quote}`] = pivotPermutation;
const liquidity = await getLiquidityV2(platform, base, quote, block);
let valueFor5Pct = 0;
if (liquidity) {
valueFor5Pct = liquidity.slippageMap[500];
}
if (valueFor5Pct > bestValue) {
bestValue = valueFor5Pct;
console.log(`new best permutation ${base} ${quote} for permutations ${pivotPermutation} : ${valueFor5Pct}`);
bestPermutation = structuredClone(pivotPermutation);
}
console.log(`platform ${platform} ${base} ${quote} for permutations ${pivotPermutation} : ${valueFor5Pct}`);
}
console.log(`best permutation for ${base}/${quote}: ${bestPermutation} for value ${bestValue}`);
newSpecificPivotsOverride[`${base}/${quote}`] = structuredClone(bestPermutation);
fs.writeFileSync('bestPerms.json', JSON.stringify(newSpecificPivotsOverride, null, 2));
}
for (const [pair, pivots] of Object.entries(newSpecificPivotsOverride)) {
let shouldDelete = true;
for (let i = 0; i < pivots.length; i++) {
if (pivots[i] != baseTokens[i]) {
shouldDelete = false;
break;
}
}
if (shouldDelete) {
console.log(`for pair ${pair}, pivots: ${pivots} are the same as ${baseTokens}`);
delete newSpecificPivotsOverride[pair];
} else {
console.log(`for pair ${pair}, pivots: ${pivots} are the different than ${baseTokens}`);
}
}
fs.writeFileSync('bestPerms-updated.json', JSON.stringify(newSpecificPivotsOverride, null, 2));
}
checkLiquidity();