Skip to content

Commit

Permalink
Add buildRecoveryExit to pools so it can be refreshed.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngrantuk committed Aug 14, 2023
1 parent 3798833 commit 49f03d8
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
67 changes: 45 additions & 22 deletions balancer-js/examples/pools/exit/recovery-exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,83 @@
*/
import {
BalancerSDK,
insert,
removeItem,
Network,
truncateAddresses,
} from '@balancer-labs/sdk';
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();
24 changes: 24 additions & 0 deletions balancer-js/src/modules/pools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,30 @@ export class Pools implements Findable<PoolWithMethods> {
});
}

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
*
Expand Down

0 comments on commit 49f03d8

Please sign in to comment.