-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.ts
78 lines (61 loc) · 2.81 KB
/
test1.ts
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
import { BalancerSDK, BalancerSdkConfig, Network,BalancerError, BalancerErrorCode} from '@balancer-labs/sdk';
async function findArbitrageOpportunities() {
const config: BalancerSdkConfig = {
network: Network.MAINNET,
rpcUrl: ``,
};
const balancer = new BalancerSDK(config);
const { pools } = balancer.data;
try {
const allPools = await pools.all();
const filteredPools = allPools.filter(pool => parseFloat(pool.totalLiquidity) > 100000);
// Build a graph of token connections
const graph = await buildTokenGraph(filteredPools, balancer);
console.log(graph);
} catch (error) {
console.error('An error occurred:', error);
}
}
async function buildTokenGraph(poolfilter, balancer) {
const graph = new Map();
for (let i = 0; i < poolfilter.length; i++) {
const pool = poolfilter[i];
const tokens = pool.tokens.map((token) => token.address);
const poolId = pool.id;
const pool1 = await balancer.pools.find(poolId);
if (!pool1) {
throw new BalancerError(BalancerErrorCode.POOL_DOESNT_EXIST);
}
for (let j = 0; j < tokens.length; j++) {
const tokenA = tokens[j];
const priceTokenA = parseFloat(pool1.tokens[j].token?.latestUSDPrice || '0');
for (let k = j + 1; k < tokens.length; k++) {
const tokenB = tokens[k];
const priceTokenB = parseFloat(pool1.tokens[k].token?.latestUSDPrice || '0');
if (isNaN(priceTokenA) || isNaN(priceTokenB)) {
throw new BalancerError(BalancerErrorCode.MISSING_TOKENS);
}
const wrtPriceAB = priceTokenA / priceTokenB;
const wrtPriceBA = priceTokenB / priceTokenA;
const spotPriceAtoB = wrtPriceAB * (1 + parseFloat(pool1.swapFee));
const spotPriceBtoA = wrtPriceBA * (1 + parseFloat(pool1.swapFee));
const tokenPairKey = `${tokenA}_${tokenB}`;
const inverseTokenPairKey = `${tokenB}_${tokenA}`;
graph.set(tokenPairKey, spotPriceAtoB);
graph.set(inverseTokenPairKey, spotPriceBtoA);
// console.log('Assigned spotPriceAtoB:', spotPriceAtoB);
//console.log('Assigned spotPriceBtoA:', spotPriceBtoA);
//console.log('graph.get(tokenPairKey):', graph.get(tokenPairKey));
//console.log('graph.get(inverseTokenPairKey):', graph.get(inverseTokenPairKey));
//console.log(graph.get('0xba100000625a3754423978a60c9317c58a424e3d_0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'));
//console.log(graph.get('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2_0xba100000625a3754423978a60c9317c58a424e3d'));
//console.log(tokenA, tokenB);
//console.log(priceTokenA);
//console.log(priceTokenB);
//console.log(pool1.tokens[k].id )
}
}
}
return graph;
}
findArbitrageOpportunities();