/
queryBatchSwapWithSor.ts
100 lines (93 loc) · 3.33 KB
/
queryBatchSwapWithSor.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
import dotenv from 'dotenv';
import { parseFixed, BigNumber } from '@ethersproject/bignumber';
import {
BalancerSDK,
BalancerSdkConfig,
Network,
SwapType,
} from '../src/index';
import { AAVE_DAI, AAVE_USDC, AAVE_USDT, STABAL3PHANTOM } from './constants';
dotenv.config();
async function runQueryBatchSwapWithSor() {
const config: BalancerSdkConfig = {
network: Network.KOVAN,
rpcUrl: `https://kovan.infura.io/v3/${process.env.INFURA}`,
};
const balancer = new BalancerSDK(config);
const poolsFetched = await balancer.swaps.fetchPools();
if (!poolsFetched) {
console.log(`Error fetching pools data.`);
return;
}
// Example showing how to join bb-a-usd pool by swapping stables > BPT
let queryResult = await balancer.swaps.queryBatchSwapWithSor({
tokensIn: [AAVE_DAI.address, AAVE_USDC.address, AAVE_USDT.address],
tokensOut: [
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
],
swapType: SwapType.SwapExactIn,
amounts: [
parseFixed('100', 18).toString(),
parseFixed('100', 6).toString(),
parseFixed('100', 6).toString(),
],
fetchPools: {
fetchPools: false, // Because pools were previously fetched we can reuse to speed things up
fetchOnChain: false,
},
});
console.log(`\n******* stables > BPT ExactIn`);
console.log(queryResult.swaps);
console.log(queryResult.assets);
console.log(queryResult.deltas.toString());
console.log(queryResult.returnAmounts.toString());
// Example showing how to exit bb-a-usd pool by swapping BPT > stables
queryResult = await balancer.swaps.queryBatchSwapWithSor({
tokensIn: [
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
],
tokensOut: [AAVE_DAI.address, AAVE_USDC.address, AAVE_USDT.address],
swapType: SwapType.SwapExactIn,
amounts: [
parseFixed('1', 18).toString(),
parseFixed('1', 18).toString(),
parseFixed('1', 18).toString(),
],
fetchPools: {
fetchPools: false,
fetchOnChain: false,
},
});
console.log(`\n******* BPT > stables ExactIn`);
console.log(queryResult.swaps);
console.log(queryResult.assets);
console.log(queryResult.deltas.toString());
console.log(queryResult.returnAmounts.toString());
queryResult = await balancer.swaps.queryBatchSwapWithSor({
tokensIn: [
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
STABAL3PHANTOM.address,
],
tokensOut: [AAVE_DAI.address, AAVE_USDC.address, AAVE_USDT.address],
swapType: SwapType.SwapExactOut,
amounts: queryResult.returnAmounts.map((amt) =>
BigNumber.from(amt).abs().toString()
),
fetchPools: {
fetchPools: false,
fetchOnChain: false,
},
});
console.log(`\n******* BPT > stables Exact Out`);
console.log(queryResult.swaps);
console.log(queryResult.assets);
console.log(queryResult.deltas.toString());
console.log(queryResult.returnAmounts.toString());
}
// yarn examples:run ./examples/queryBatchSwapWithSor.ts
runQueryBatchSwapWithSor();