forked from balancer/b-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
removeLiquidity.ts
141 lines (129 loc) · 3.98 KB
/
removeLiquidity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Example showing how to remove liquidity from a pool.
* (Runs against a local Anvil fork)
*
* Run with:
* pnpm example ./examples/removeLiquidity/removeLiquidity.ts
*/
import { Address, parseEther } from 'viem';
import {
BalancerApi,
API_ENDPOINT,
ChainId,
InputAmount,
PoolState,
PriceImpact,
RemoveLiquidity,
RemoveLiquidityInput,
RemoveLiquidityKind,
Slippage,
} from '../../src';
import { ANVIL_NETWORKS, startFork } from '../../test/anvil/anvil-global-setup';
import { makeForkTx } from '../lib/makeForkTx';
async function runAgainstFork() {
// User defined inputs
const { rpcUrl } = await startFork(ANVIL_NETWORKS.MAINNET);
const chainId = ChainId.MAINNET;
const userAccount = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
// 80BAL-20WETH
const pool = {
id: '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014',
address: '0x5c6Ee304399DBdB9C8Ef030aB642B10820DB8F56' as Address,
};
const bptIn: InputAmount = {
rawAmount: parseEther('1'),
decimals: 18,
address: pool.address,
};
const tokenOut = '0xba100000625a3754423978a60c9317c58a424e3D'; // BAL
const slippage = Slippage.fromPercentage('1'); // 1%
const call = await removeLiquidity({
rpcUrl,
chainId,
userAccount,
bptIn,
tokenOut,
poolId: pool.id,
slippage,
});
// Make the tx against the local fork and print the result
await makeForkTx(
call,
{
rpcUrl,
chainId,
impersonateAccount: userAccount,
forkTokens: [
{
address: pool.address,
slot: 0,
rawBalance: parseEther('1'),
},
],
},
[
'0xba100000625a3754423978a60c9317c58a424e3D' as Address,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2' as Address,
pool.address,
],
call.protocolVersion,
);
}
const removeLiquidity = async ({
rpcUrl,
chainId,
userAccount,
poolId,
bptIn,
tokenOut,
slippage,
}) => {
// API is used to fetch relevant pool data
const balancerApi = new BalancerApi(API_ENDPOINT, chainId);
const poolState: PoolState = await balancerApi.pools.fetchPoolState(poolId);
// Construct the RemoveLiquidityInput, in this case a RemoveLiquiditySingleTokenExactIn
const removeLiquidityInput: RemoveLiquidityInput = {
chainId,
rpcUrl,
bptIn,
tokenOut,
kind: RemoveLiquidityKind.SingleTokenExactIn,
};
// Calculate price impact to ensure it's acceptable
const priceImpact = await PriceImpact.removeLiquidity(
removeLiquidityInput,
poolState,
);
console.log(`\nPrice Impact: ${priceImpact.percentage.toFixed(2)}%`);
// Simulate removing liquidity to get the tokens out
const removeLiquidity = new RemoveLiquidity();
const queryOutput = await removeLiquidity.query(
removeLiquidityInput,
poolState,
);
console.log('\nRemove Liquidity Query Output:');
console.log(`BPT In: ${queryOutput.bptIn.amount.toString()}`);
console.table({
tokensOut: queryOutput.amountsOut.map((a) => a.token.address),
amountsOut: queryOutput.amountsOut.map((a) => a.amount),
});
// Apply slippage to the tokens out received from the query and construct the call
const call = removeLiquidity.buildCall({
...queryOutput,
slippage,
sender: userAccount,
recipient: userAccount,
chainId,
});
console.log('\nWith slippage applied:');
console.log(`Max BPT In: ${call.maxBptIn.amount}`);
console.table({
tokensOut: call.minAmountsOut.map((a) => a.token.address),
minAmountsOut: call.minAmountsOut.map((a) => a.amount),
});
return {
...call,
protocolVersion: queryOutput.protocolVersion,
};
};
export default runAgainstFork;