forked from balancer/balancer-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exitExactTokensOut.ts
94 lines (78 loc) · 2.84 KB
/
exitExactTokensOut.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
import dotenv from 'dotenv';
import { JsonRpcProvider } from '@ethersproject/providers';
import {
BalancerError,
BalancerErrorCode,
BalancerSDK,
Network,
PoolWithMethods,
} from '../src/index';
import { forkSetup, getBalances } from '../src/test/lib/utils';
import { ADDRESSES } from '../src/test/lib/constants';
import { BigNumber } from '@ethersproject/bignumber';
dotenv.config();
const { ALCHEMY_URL: jsonRpcUrl } = process.env;
// Slots used to set the account balance for each token through hardhat_setStorageAt
// Info fetched using npm package slot20
const BPT_SLOT = 0;
/*
Example showing how to use Pools module to exit pools with exact tokens out method.
*/
async function exitExactTokensOut() {
const network = Network.MAINNET;
const rpcUrl = 'http://127.0.0.1:8545';
const provider = new JsonRpcProvider(rpcUrl, network);
const signer = provider.getSigner();
const signerAddress = await signer.getAddress();
const poolId =
'0xa6f548df93de924d73be7d25dc02554c6bd66db500020000000000000000000e'; // 50/50 WBTC/WETH Pool
const slippage = '200'; // 200 bps = 2%
const sdkConfig = {
network,
rpcUrl,
};
const balancer = new BalancerSDK(sdkConfig);
// Use SDK to find pool info
const pool: PoolWithMethods | undefined = await balancer.pools.find(poolId);
if (!pool) throw new BalancerError(BalancerErrorCode.POOL_DOESNT_EXIST);
const tokensOut = [
ADDRESSES[network].WBTC?.address,
ADDRESSES[network].WETH?.address,
]; // Tokens that will be provided to pool by joiner
const amountsOut = ['10000000', '1000000000000000000'];
const { to, data, expectedBPTIn, maxBPTIn } = pool.buildExitExactTokensOut(
signerAddress,
tokensOut as string[],
amountsOut,
slippage
);
// Sets up local fork granting signer initial balances and token approvals
await forkSetup(
signer,
[pool.address],
[BPT_SLOT],
[maxBPTIn],
jsonRpcUrl as string
);
// Checking balances to confirm success
const tokenBalancesBefore = (
await getBalances([pool.address, ...pool.tokensList], signer, signerAddress)
).map((b) => b.toString());
// Submit exit tx
const transactionResponse = await signer.sendTransaction({
to,
data,
// gasPrice: '6000000000', // gas inputs are optional
// gasLimit: '2000000', // gas inputs are optional
});
await transactionResponse.wait();
const tokenBalancesAfter = (
await getBalances([pool.address, ...pool.tokensList], signer, signerAddress)
).map((b) => b.toString());
console.log('Balances before exit: ', tokenBalancesBefore);
console.log('Balances after exit: ', tokenBalancesAfter);
console.log('Expected BPT input: ', expectedBPTIn);
console.log('Max BPT input (slippage): ', maxBPTIn);
}
// yarn examples:run ./examples/exitExactTokensOut.ts
exitExactTokensOut();