-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathswapDebug.ts
104 lines (89 loc) · 2.39 KB
/
swapDebug.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Helper example to facilitate swap debugging within the SDK
*
* How to run:
* yarn example examples/swaps/swapDebug.ts
*/
import { ADDRESSES } from '@/test/lib/constants';
import { FORK_NODES, RPC_URLS, forkSetup } from '@/test/lib/utils';
import { BalancerSDK, Network } from '@balancer-labs/sdk';
import { formatFixed } from '@ethersproject/bignumber';
const network = Network.MAINNET;
const rpcUrl = RPC_URLS[network];
const sdk = new BalancerSDK({
network,
rpcUrl,
});
const tokenIn = ADDRESSES[network].BAL8020BPT;
const tokenOut = ADDRESSES[network].auraBal;
const amount = String(BigInt(1000e18)); // 1000 eth
const { swaps } = sdk;
const erc20Out = sdk.contracts.ERC20(tokenOut.address, sdk.provider);
async function swap() {
const signer = sdk.provider.getSigner();
const account = await signer.getAddress();
await forkSetup(
signer,
[tokenIn.address],
[tokenIn.slot],
[amount],
FORK_NODES[network]
);
// Finding a trading route rely on on-chain data.
// fetchPools will fetch the current data from the subgraph.
// Let's fetch just 5 pools with highest liquidity of tokenOut.
await swaps.fetchPools({
first: 5,
where: {
swapEnabled: {
eq: true,
},
tokensList: {
contains: [tokenOut.address],
},
},
orderBy: 'totalLiquidity',
orderDirection: 'desc',
});
// Set exectution deadline to 60 seconds from now
const deadline = String(Math.ceil(Date.now() / 1000) + 60);
// Avoid getting rekt by setting low slippage from expected amounts out, 10 bsp = 0.1%
const maxSlippage = 10;
// Building the route payload
const payload = await swaps.buildRouteExactIn(
account,
account,
tokenIn.address,
tokenOut.address,
amount,
{
maxSlippage,
deadline,
}
);
// Extract parameters required for sendTransaction
const { to, data, value } = payload;
// Execution with ethers.js
try {
const balanceBefore = await erc20Out.balanceOf(account);
await (
await signer.sendTransaction({
to,
data,
value,
gasLimit: 8e6,
})
).wait();
// check delta
const balanceAfter = await erc20Out.balanceOf(account);
console.log(
`Amount of tokenOut received: ${formatFixed(
balanceAfter.sub(balanceBefore),
tokenOut.decimals
)}`
);
} catch (err) {
console.log(err);
}
}
swap();