From 49f03d88930b64499aa8bfde8cda64326dfddcd7 Mon Sep 17 00:00:00 2001 From: johngrantuk Date: Mon, 14 Aug 2023 17:09:13 +0100 Subject: [PATCH] Add buildRecoveryExit to pools so it can be refreshed. --- .../examples/pools/exit/recovery-exit.ts | 67 +++++++++++++------ balancer-js/src/modules/pools/index.ts | 24 +++++++ 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/balancer-js/examples/pools/exit/recovery-exit.ts b/balancer-js/examples/pools/exit/recovery-exit.ts index 38a4f61f0..a218fddf5 100644 --- a/balancer-js/examples/pools/exit/recovery-exit.ts +++ b/balancer-js/examples/pools/exit/recovery-exit.ts @@ -6,7 +6,7 @@ */ import { BalancerSDK, - insert, + removeItem, Network, truncateAddresses, } from '@balancer-labs/sdk'; @@ -14,52 +14,75 @@ import { parseEther } from '@ethersproject/units'; import { getTokenBalance, reset, setTokenBalance } from 'examples/helpers'; async function recoveryExit() { + const poolId = + '0x20b156776114e8a801e9767d90c6ccccc8adf398000000000000000000000499'; + const blockNo = 17700000; + const balancer = new BalancerSDK({ network: Network.MAINNET, rpcUrl: 'http://127.0.0.1:8545', // Using local fork for simulation }); + const { poolsOnChain, pools } = balancer.data; // Setup exit parameters const signer = balancer.provider.getSigner(); - const address = await signer.getAddress(); - - const poolId = - // '0x50cf90b954958480b8df7958a9e965752f62712400000000000000000000046f'; // bb-e-usd - // '0xd4e7c1f3da1144c9e2cfd1b015eda7652b4a439900000000000000000000046a'; // bb-e-usdc - // '0xa13a9247ea42d743238089903570127dda72fe4400000000000000000000035d'; // bb-a-usd - '0xa718042e5622099e5f0ace4e7122058ab39e1bbe000200000000000000000475'; // 50temple_50bb-e-usd + const userAddress = await signer.getAddress(); - const bptIn = String(parseEther('1')); + const bptAmount = String(parseEther('1')); const slippage = '200'; // 200 bps = 2% // Use SDK to find pool info - const pool = await balancer.pools.find(poolId); + let pool = await pools.find(poolId); if (!pool) throw 'POOL_DOESNT_EXIST'; // Prepare local fork for simulation - await reset(balancer.provider, 17700000); - await setTokenBalance(balancer.provider, address, pool.address, bptIn, 0); + await reset(balancer.provider, blockNo); + await setTokenBalance( + balancer.provider, + userAddress, + pool.address, + bptAmount, + 0 + ); + + // Refresh pool data from chain before building and sending tx + pool = await poolsOnChain.refresh(pool); // Build transaction const { to, data, expectedAmountsOut, minAmountsOut } = - pool.buildRecoveryExit(address, bptIn, slippage); + balancer.pools.buildRecoveryExit({ + pool, + bptAmount, + userAddress, + slippage, + }); // Send transaction await signer.sendTransaction({ to, data }); + // Refresh pool data from chain before building and sending tx + pool = await poolsOnChain.refresh(pool); + + const bptIndex = pool.tokensList.indexOf(pool.address); + const tokensWithoutBpt = + bptIndex === -1 ? pool.tokensList : removeItem(pool.tokensList, bptIndex); // Check balances after transaction to confirm success - const balances = await Promise.all( - pool.tokensList.map((token) => - getTokenBalance(token, address, balancer.provider) - ) - ); + const balances = await Promise.all([ + ...tokensWithoutBpt.map((token) => + getTokenBalance(token, userAddress, balancer.provider) + ), + getTokenBalance(pool.address, userAddress, balancer.provider), + ]); console.table({ - tokensOut: truncateAddresses(pool.tokensList), - minAmountsOut: insert(minAmountsOut, pool.bptIndex, bptIn), - expectedAmountsOut: insert(expectedAmountsOut, pool.bptIndex, bptIn), - amountsOut: balances.map((b) => b.toString()), + tokensOut: truncateAddresses(tokensWithoutBpt), + minAmountsOut: minAmountsOut, + expectedAmountsOut: expectedAmountsOut, + amountsOut: removeItem(balances, balances.length - 1).map((b) => + b.toString() + ), }); + console.log(`BPT Balance: `, balances[balances.length - 1].toString()); } recoveryExit(); diff --git a/balancer-js/src/modules/pools/index.ts b/balancer-js/src/modules/pools/index.ts index 5767f0042..e0be721e7 100644 --- a/balancer-js/src/modules/pools/index.ts +++ b/balancer-js/src/modules/pools/index.ts @@ -393,6 +393,30 @@ export class Pools implements Findable { }); } + buildRecoveryExit({ + pool, + bptAmount, + userAddress, + slippage, + }: { + pool: Pool; + bptAmount: string; + userAddress: string; + slippage: string; + }): ExitExactBPTInAttributes { + const concerns = PoolTypeConcerns.from(pool.poolType); + if (!concerns || !concerns.exit.buildRecoveryExit) + throw `buildRecoveryExit for poolType ${pool.poolType} not implemented`; + + return concerns.exit.buildRecoveryExit({ + exiter: userAddress, + pool, + bptIn: bptAmount, + slippage, + toInternalBalance: false, + }); + } + /** * Builds generalised join transaction *