forked from balancer/balancer-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
joinGeneralised.ts
157 lines (138 loc) · 4.54 KB
/
joinGeneralised.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import dotenv from 'dotenv';
import { JsonRpcProvider } from '@ethersproject/providers';
import { parseFixed } from '@ethersproject/bignumber';
import { BalancerSDK, Network } from '../src/index';
import { forkSetup, getBalances } from '../src/test/lib/utils';
import { ADDRESSES } from '../src/test/lib/constants';
import { Relayer } from '../src/modules/relayer/relayer.module';
import { Contracts } from '../src/modules/contracts/contracts.module';
dotenv.config();
const {
ALCHEMY_URL_GOERLI: jsonRpcUrl,
TENDERLY_ACCESS_KEY,
TENDERLY_PROJECT,
TENDERLY_USER,
} = process.env;
const network = Network.GOERLI;
const blockNumber = 7890980;
const rpcUrl = 'http://127.0.0.1:8000';
const addresses = ADDRESSES[network];
// Setup local fork with correct balances/approval to join pool with DAI/USDC/bbaDAI/bbaUSDC
async function setUp(provider: JsonRpcProvider): Promise<string> {
const signer = provider.getSigner();
const signerAddress = await signer.getAddress();
const mainTokens = [addresses.DAI.address, addresses.USDC.address];
const mainInitialBalances = [
parseFixed('100', addresses.DAI.decimals).toString(),
parseFixed('100', addresses.USDC.decimals).toString(),
];
const mainSlots = [
addresses.DAI.slot as number,
addresses.USDC.slot as number,
];
const linearPoolTokens = [
addresses.bbadai?.address as string,
addresses.bbausdc?.address as string,
];
const linearInitialBalances = [
parseFixed('100', addresses.bbadai?.decimals).toString(),
parseFixed('100', addresses.bbausdc?.decimals).toString(),
];
const linearPoolSlots = [
addresses.bbadai?.slot as number,
addresses.bbausdc?.slot as number,
];
await forkSetup(
signer,
[...mainTokens, ...linearPoolTokens],
[...mainSlots, ...linearPoolSlots],
[...mainInitialBalances, ...linearInitialBalances],
jsonRpcUrl as string,
blockNumber
);
const { contracts, contractAddresses } = new Contracts(
network as number,
provider
);
return await Relayer.signRelayerApproval(
contractAddresses.relayerV4 as string,
signerAddress,
signer,
contracts.vault
);
}
/*
Example showing how to use the SDK generalisedJoin method.
This allows joining of a ComposableStable that has nested pools, e.g.:
CS0
/ \
CS1 CS2
/ \ / \
DAI USDC USDT FRAX
Can join with tokens: DAI, USDC, USDT, FRAX, CS1_BPT, CS2_BPT
*/
async function join() {
const provider = new JsonRpcProvider(rpcUrl, network);
// Local fork setup
const relayerAuth = await setUp(provider);
const signer = provider.getSigner();
const signerAddress = await signer.getAddress();
const wrapLeafTokens = false;
const slippage = '100'; // 100 bps = 1%
const bbausd2 = {
id: addresses.bbausd2?.id as string,
address: addresses.bbausd2?.address as string,
};
// Here we join with USDC and bbadai
const tokensIn = [
addresses.USDC.address,
addresses.bbadai?.address as string,
];
const amountsIn = [
parseFixed('10', 6).toString(),
parseFixed('10', 18).toString(),
];
// Custom Tenderly configuration parameters - remove in order to use default values
const tenderlyConfig = {
accessKey: TENDERLY_ACCESS_KEY as string,
user: TENDERLY_USER as string,
project: TENDERLY_PROJECT as string,
blockNumber,
};
const balancer = new BalancerSDK({
network,
rpcUrl,
customSubgraphUrl:
'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-goerli-v2-beta',
tenderly: tenderlyConfig,
});
// Checking balances to confirm success
const tokenBalancesBefore = (
await getBalances([bbausd2.address, ...tokensIn], signer, signerAddress)
).map((b) => b.toString());
// Use SDK to create join
const query = await balancer.pools.generalisedJoin(
bbausd2.id,
tokensIn,
amountsIn,
signerAddress,
wrapLeafTokens,
slippage,
relayerAuth
);
// Submit join tx
const transactionResponse = await signer.sendTransaction({
to: query.to,
data: query.callData,
});
await transactionResponse.wait();
const tokenBalancesAfter = (
await getBalances([bbausd2.address, ...tokensIn], signer, signerAddress)
).map((b) => b.toString());
console.log('Balances before exit: ', tokenBalancesBefore);
console.log('Balances after exit: ', tokenBalancesAfter);
console.log('Expected BPT after exit: ', [query.expectedOut]);
console.log('Min BPT after exit: ', [query.minOut]);
}
// yarn examples:run ./examples/joinGeneralised.ts
join();