|
| 1 | +/** |
| 2 | + * Helper example to facilitate swap debugging within the SDK |
| 3 | + * |
| 4 | + * How to run: |
| 5 | + * yarn example examples/swaps/swapDebug.ts |
| 6 | + */ |
| 7 | +import { ADDRESSES } from '@/test/lib/constants'; |
| 8 | +import { FORK_NODES, RPC_URLS, forkSetup } from '@/test/lib/utils'; |
| 9 | +import { BalancerSDK, Network } from '@balancer-labs/sdk'; |
| 10 | +import { formatFixed } from '@ethersproject/bignumber'; |
| 11 | + |
| 12 | +const network = Network.MAINNET; |
| 13 | +const rpcUrl = RPC_URLS[network]; |
| 14 | +const sdk = new BalancerSDK({ |
| 15 | + network, |
| 16 | + rpcUrl, |
| 17 | +}); |
| 18 | + |
| 19 | +const tokenIn = ADDRESSES[network].BAL8020BPT; |
| 20 | +const tokenOut = ADDRESSES[network].auraBal; |
| 21 | +const amount = String(BigInt(1000e18)); // 1000 eth |
| 22 | + |
| 23 | +const { swaps } = sdk; |
| 24 | +const erc20Out = sdk.contracts.ERC20(tokenOut.address, sdk.provider); |
| 25 | + |
| 26 | +async function swap() { |
| 27 | + const signer = sdk.provider.getSigner(); |
| 28 | + const account = await signer.getAddress(); |
| 29 | + |
| 30 | + await forkSetup( |
| 31 | + signer, |
| 32 | + [tokenIn.address], |
| 33 | + [tokenIn.slot], |
| 34 | + [amount], |
| 35 | + FORK_NODES[network] |
| 36 | + ); |
| 37 | + |
| 38 | + // Finding a trading route rely on on-chain data. |
| 39 | + // fetchPools will fetch the current data from the subgraph. |
| 40 | + // Let's fetch just 5 pools with highest liquidity of tokenOut. |
| 41 | + await swaps.fetchPools({ |
| 42 | + first: 5, |
| 43 | + where: { |
| 44 | + swapEnabled: { |
| 45 | + eq: true, |
| 46 | + }, |
| 47 | + tokensList: { |
| 48 | + contains: [tokenOut.address], |
| 49 | + }, |
| 50 | + }, |
| 51 | + orderBy: 'totalLiquidity', |
| 52 | + orderDirection: 'desc', |
| 53 | + }); |
| 54 | + |
| 55 | + // Set exectution deadline to 60 seconds from now |
| 56 | + const deadline = String(Math.ceil(Date.now() / 1000) + 60); |
| 57 | + |
| 58 | + // Avoid getting rekt by setting low slippage from expected amounts out, 10 bsp = 0.1% |
| 59 | + const maxSlippage = 10; |
| 60 | + |
| 61 | + // Building the route payload |
| 62 | + const payload = await swaps.buildRouteExactIn( |
| 63 | + account, |
| 64 | + account, |
| 65 | + tokenIn.address, |
| 66 | + tokenOut.address, |
| 67 | + amount, |
| 68 | + { |
| 69 | + maxSlippage, |
| 70 | + deadline, |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + // Extract parameters required for sendTransaction |
| 75 | + const { to, data, value } = payload; |
| 76 | + |
| 77 | + // Execution with ethers.js |
| 78 | + try { |
| 79 | + const balanceBefore = await erc20Out.balanceOf(account); |
| 80 | + |
| 81 | + await ( |
| 82 | + await signer.sendTransaction({ |
| 83 | + to, |
| 84 | + data, |
| 85 | + value, |
| 86 | + gasLimit: 8e6, |
| 87 | + }) |
| 88 | + ).wait(); |
| 89 | + |
| 90 | + // check delta |
| 91 | + const balanceAfter = await erc20Out.balanceOf(account); |
| 92 | + |
| 93 | + console.log( |
| 94 | + `Amount of tokenOut received: ${formatFixed( |
| 95 | + balanceAfter.sub(balanceBefore), |
| 96 | + tokenOut.decimals |
| 97 | + )}` |
| 98 | + ); |
| 99 | + } catch (err) { |
| 100 | + console.log(err); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +swap(); |
0 commit comments