-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.ts
66 lines (56 loc) · 2.28 KB
/
agent.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
import { ethers, providers, Wallet, Contract } from 'ethers';
import { BalancerSubgraphClient, BalancerSubgraphPoolsBatch, BalancerSubgraphPool } from '@balancer-labs/sdk';
import { SwapTypes, encodeSwap, batchSwap } from '@balancer-labs/balancer-js';
import { Balancer, SwapTypes } from "@balancer-labs/sor";
async function executeMultihopBatchSwap(tokenPath, senderAddress, recipientAddress, amountIn, privateKey, balancerVaultAddress) {
try {
// Set up provider and signer
const provider = new ethers.providers.InfuraProvider('homestead', 'INFURA_API_KEY');
const signer = new ethers.Wallet(privateKey, provider);
// Set up Balancer SDK
const balancer = await Balancer.create(provider, signer);
// Construct swaps array
const swaps = [];
for (let i = 0; i < tokenPath.length - 1; i++) {
const fromToken = tokenPath[i];
const toToken = tokenPath[i + 1];
const poolId = await balancer.getPoolIdFromTokens(fromToken, toToken);
const assetIndexes = await balancer.getPoolAssets(poolId);
const swap = {
poolId,
assetInIndex: assetIndexes[0],
assetOutIndex: assetIndexes[1],
amount: amountIn,
userData: ethers.utils.hexlify(0),
};
swaps.push(swap);
}
// Set up transaction parameters
const txParams = {
swaps: swaps,
sender: senderAddress,
recipient: recipientAddress,
deadline: '999999999999999999',
limits: ['0'],
vault: balancerVaultAddress,
permittee: ethers.constants.AddressZero,
permitData: {
v: 0,
r: ethers.constants.HashZero,
s: ethers.constants.HashZero,
},
};
// Execute the batch swap
const tx = await balancer.batchSwap(txParams);
console.log(tx.hash);
} catch (error) {
console.log('Error executing batch swap:', error);
}
}
const tokenPath = ['ETH', 'USDC', 'WBTC'];
const senderAddress = 'SENDER_ADDRESS';
const recipientAddress = 'RECIPIENT_ADDRESS';
const amountIn = '1000000000000000000'; // 1 ETH in wei
const privateKey = 'YOUR_PRIVATE_KEY';
const balancerVaultAddress = 'BALANCER_VAULT_ADDRESS';
executeMultihopBatchSwap(tokenPath, senderAddress, recipientAddress, amountIn, privateKey, balancerVaultAddress);