-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding chunkSize param to multicall fetchPools function;
- Loading branch information
Luiz Gustavo Abou Hatem De Liz
committed
Jul 20, 2023
1 parent
7b7aac5
commit 761dd78
Showing
5 changed files
with
316 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,81 @@ | ||
/** | ||
* How to build a swap and send it using ethers.js | ||
* | ||
* | ||
* How to run: | ||
* yarn example examples/swaps/swap.ts | ||
*/ | ||
import { BalancerSDK, Network } from '@balancer-labs/sdk' | ||
import { formatFixed } from '@ethersproject/bignumber' | ||
import { AddressZero } from '@ethersproject/constants' | ||
import { BalancerSDK, Network } from '@balancer-labs/sdk'; | ||
import { formatFixed } from '@ethersproject/bignumber'; | ||
import { AddressZero } from '@ethersproject/constants'; | ||
|
||
const tokenIn = AddressZero // eth | ||
const tokenOut = '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' // wBTC | ||
const amount = String(BigInt(100e18)) // 100 eth | ||
const tokenIn = AddressZero; // eth | ||
const tokenOut = '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599'; // wBTC | ||
const amount = String(BigInt(100e18)); // 100 eth | ||
|
||
const sdk = new BalancerSDK({ | ||
network: Network.MAINNET, | ||
rpcUrl: `http://127.0.0.1:8545`, // Uses a local fork for simulating transaction sending. | ||
}) | ||
}); | ||
|
||
const { swaps } = sdk | ||
const { swaps } = sdk; | ||
|
||
const erc20Out = sdk.contracts.ERC20(tokenOut, sdk.provider) | ||
const erc20Out = sdk.contracts.ERC20(tokenOut, sdk.provider); | ||
|
||
async function swap() { | ||
const signer = sdk.provider.getSigner() | ||
const account = await signer.getAddress() | ||
const signer = sdk.provider.getSigner(); | ||
const account = await signer.getAddress(); | ||
|
||
// 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], | ||
}, | ||
}, | ||
orderBy: 'totalLiquidity', | ||
orderDirection: 'desc', | ||
}) | ||
await swaps.fetchPools(undefined, 30); | ||
|
||
// Set exectution deadline to 60 seconds from now | ||
const deadline = String(Math.ceil(Date.now() / 1000) + 60) | ||
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 | ||
const maxSlippage = 10; | ||
|
||
// Building the route payload | ||
const payload = await swaps.buildRouteExactIn( | ||
account, | ||
account, | ||
tokenIn, // eth | ||
tokenIn, // eth | ||
tokenOut, // wBTC | ||
amount, | ||
{ | ||
maxSlippage, | ||
deadline | ||
deadline, | ||
} | ||
) | ||
); | ||
|
||
// Extract parameters required for sendTransaction | ||
const { to, data, value } = payload | ||
const { to, data, value } = payload; | ||
|
||
// Execution with ethers.js | ||
try { | ||
const balanceBefore = await erc20Out.balanceOf(account) | ||
const balanceBefore = await erc20Out.balanceOf(account); | ||
|
||
await ( | ||
await signer.sendTransaction({ | ||
to, | ||
data, | ||
value, | ||
}) | ||
).wait() | ||
).wait(); | ||
|
||
// check delta | ||
const balanceAfter = await erc20Out.balanceOf(account) | ||
const balanceAfter = await erc20Out.balanceOf(account); | ||
|
||
console.log( | ||
`Amount of BTC received: ${formatFixed( | ||
balanceAfter.sub(balanceBefore), | ||
8 | ||
)}` | ||
) | ||
); | ||
} catch (err) { | ||
console.log(err) | ||
console.log(err); | ||
} | ||
} | ||
|
||
swap() | ||
swap(); |
Oops, something went wrong.