-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
83 lines (65 loc) · 2.28 KB
/
test.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
79
80
81
82
83
import { BalancerSDK, SwapType, Network, BatchSwapStep, EncodeBatchSwapInput, SwapTypes, Swaps } from '@balancer-labs/sdk';
import { ethers } from 'ethers';
const PRIVATE_KEY = '';
const INFURA_API_KEY = '';
async function executeFlashSwap(path: string[]) {
const balancer = new BalancerSDK({
network: Network.MAINNET,
rpcUrl: `https://mainnet.infura.io/v3/${INFURA_API_KEY}`,
});
const provider = new ethers.providers.InfuraProvider(Network.MAINNET, INFURA_API_KEY);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const swaps: BatchSwapStep[] = [];
const assets: string[] = [];
for (let i = 0; i < path.length - 1; i++) {
const tokenA = path[i];
const tokenB = path[i + 1];
const poolId = await findPoolId(tokenA, tokenB, balancer);
if (!poolId) {
throw new Error(`Pool not found for tokens ${tokenA} and ${tokenB}`);
}
const swapStep: BatchSwapStep = {
poolId,
assetInIndex: i === 0 ? i : i - 1,
assetOutIndex: i === 0 ? i + 1 : i,
amount: '0', // Replace with the appropriate amount for each swap step
userData: '0x',
};
swaps.push(swapStep);
assets.push(tokenA);
}
const funds = {
sender: wallet.address,
fromInternalBalance: false,
recipient: wallet.address,
toInternalBalance: false,
};
const limits = ['0'];
const deadline = '999999999999999999';
const encodedBatchSwapData = Swaps.encodeBatchSwap({
kind: SwapType.SwapExactIn,
swaps,
assets,
funds,
limits,
deadline,
} );
const tx = await wallet.sendTransaction({
data: encodedBatchSwapData,
to: 'balancerVault', // Replace with the appropriate Balancer vault address
value: '0',
});
await tx.wait();
}
async function findPoolId(tokenA: string, tokenB: string, balancer: BalancerSDK): Promise<string | undefined> {
const { pools } = balancer.data;
const result = await pools.all();
const filteredPools = result.filter(pool => parseFloat(pool.totalLiquidity) > 100000);
for (const pool of filteredPools) {
const tokens = pool.tokens.map((token: any) => token.address);
if (tokens.includes(tokenA) && tokens.includes(tokenB)) {
return pool.id;
}
}
return undefined;
}