From 1e1971ae6b4bc5a0185ef33da97332d2a08f3805 Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Thu, 21 Dec 2023 12:46:31 -0300 Subject: [PATCH 1/4] Fix poolsToIgnore filtering by id instead of address --- .../src/modules/sor/pool-data/subgraphPoolDataService.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts b/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts index 676691f9e..742e1f84e 100644 --- a/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts +++ b/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts @@ -76,8 +76,8 @@ export class SubgraphPoolDataService implements PoolDataService { const filteredPools = pools.filter((p) => { if (!this.network.poolsToIgnore) return true; - const index = this.network.poolsToIgnore.findIndex((addr) => - isSameAddress(addr, p.address) + const index = this.network.poolsToIgnore.findIndex( + (id) => id.toLowerCase() === p.id.toLowerCase() ); return index === -1; }); From ceb58aaaa730978b7c3686b6bed8582b22a34972 Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Thu, 21 Dec 2023 12:46:54 -0300 Subject: [PATCH 2/4] Add a swapDebug example to help debug swaps --- balancer-js/examples/swaps/swapDebug.ts | 104 ++++++++++++++++++++++++ balancer-js/src/test/lib/utils.ts | 2 +- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 balancer-js/examples/swaps/swapDebug.ts diff --git a/balancer-js/examples/swaps/swapDebug.ts b/balancer-js/examples/swaps/swapDebug.ts new file mode 100644 index 000000000..12f35164d --- /dev/null +++ b/balancer-js/examples/swaps/swapDebug.ts @@ -0,0 +1,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(); diff --git a/balancer-js/src/test/lib/utils.ts b/balancer-js/src/test/lib/utils.ts index 327e1ec6b..d0755cbda 100644 --- a/balancer-js/src/test/lib/utils.ts +++ b/balancer-js/src/test/lib/utils.ts @@ -68,7 +68,7 @@ export const RPC_URLS: Record = { }; export const FORK_NODES: Record = { - [Network.MAINNET]: `${process.env.ALCHEMY_URL}`, + [Network.MAINNET]: `${process.env.RPC_URL_MAINNET}`, [Network.GOERLI]: `${process.env.ALCHEMY_URL_GOERLI}`, [Network.POLYGON]: `${process.env.ALCHEMY_URL_POLYGON}`, [Network.ARBITRUM]: `${process.env.ALCHEMY_URL_ARBITRUM}`, From 63ed6de009c39c9cc13c94fe30535551448f4972 Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Thu, 21 Dec 2023 13:07:10 -0300 Subject: [PATCH 3/4] Fix lint --- balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts b/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts index 742e1f84e..3cd48f793 100644 --- a/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts +++ b/balancer-js/src/modules/sor/pool-data/subgraphPoolDataService.ts @@ -19,7 +19,6 @@ import { SubgraphArgsFormatter, } from '@/lib/graphql/args-builder'; -import { isSameAddress } from '@/lib/utils'; import { Logger } from '@/lib/utils/logger'; // eslint-disable-next-line @typescript-eslint/no-explicit-any From e63aefad5eda4016a7a7a57cee594ddc4709f494 Mon Sep 17 00:00:00 2001 From: Bruno Eidam Guerios Date: Thu, 21 Dec 2023 13:15:52 -0300 Subject: [PATCH 4/4] revert: FORK_NODES update --- balancer-js/src/test/lib/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/balancer-js/src/test/lib/utils.ts b/balancer-js/src/test/lib/utils.ts index d0755cbda..327e1ec6b 100644 --- a/balancer-js/src/test/lib/utils.ts +++ b/balancer-js/src/test/lib/utils.ts @@ -68,7 +68,7 @@ export const RPC_URLS: Record = { }; export const FORK_NODES: Record = { - [Network.MAINNET]: `${process.env.RPC_URL_MAINNET}`, + [Network.MAINNET]: `${process.env.ALCHEMY_URL}`, [Network.GOERLI]: `${process.env.ALCHEMY_URL_GOERLI}`, [Network.POLYGON]: `${process.env.ALCHEMY_URL_POLYGON}`, [Network.ARBITRUM]: `${process.env.ALCHEMY_URL_ARBITRUM}`,