forked from balancer/balancer-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.ts
92 lines (77 loc) · 2.76 KB
/
join.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
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';
dotenv.config();
const { ALCHEMY_URL: jsonRpcUrl } = process.env;
/*
Example showing how to use Pools module to join pools.
*/
async function join() {
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();
// 50/50 WBTC/WETH Pool
const poolId = ADDRESSES[network].WBTCWETH?.id as string;
// Tokens that will be provided to pool by joiner
const tokensIn = [
ADDRESSES[network].WBTC?.address,
ADDRESSES[network].WETH?.address,
] as string[];
// Slots used to set the account balance for each token through hardhat_setStorageAt
// Info fetched using npm package slot20
const slots = [
ADDRESSES[network].WBTC?.slot,
ADDRESSES[network].WETH?.slot,
] as number[];
const amountsIn = ['10000000', '1000000000000000000'];
const slippage = '100'; // 100 bps = 1%
const sdkConfig = {
network,
rpcUrl,
};
const balancer = new BalancerSDK(sdkConfig);
// Sets up local fork granting signer initial balances and token approvals
await forkSetup(signer, tokensIn, slots, amountsIn, jsonRpcUrl as string);
// Use SDK to find pool info
const pool: PoolWithMethods | undefined = await balancer.pools.find(poolId);
if (!pool) throw new BalancerError(BalancerErrorCode.POOL_DOESNT_EXIST);
// Checking balances to confirm success
const tokenBalancesBefore = (
await getBalances([pool.address, ...pool.tokensList], signer, signerAddress)
).map((b) => b.toString());
// Use SDK to create join
const { to, data, minBPTOut } = pool.buildJoin(
signerAddress,
tokensIn,
amountsIn,
slippage
);
// Calculate price impact
const priceImpact = await pool.calcPriceImpact(amountsIn, minBPTOut, true);
// Submit join 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('Min BPT expected after exit: ', [minBPTOut.toString()]);
}
// yarn examples:run ./examples/join.ts
join();