Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buildRecoveryExit to pools so it can be refreshed. #519

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading