forked from balancer/balancer-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relayerSwapUnwrap.ts
172 lines (148 loc) · 6.03 KB
/
relayerSwapUnwrap.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import dotenv from 'dotenv';
import { defaultAbiCoder } from '@ethersproject/abi';
import { parseFixed } from '@ethersproject/bignumber';
import { Wallet } from '@ethersproject/wallet';
import { JsonRpcProvider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import linearPoolAbi from '../src/lib/abi/LinearPool.json';
import { BalancerSDK, Network, BalancerSdkConfig } from '../src/index';
import { FundManagement } from '../src/modules/swaps/types';
import balancerRelayerAbi from '../src/lib/abi/BalancerRelayer.json';
dotenv.config();
/*
Example showing how to exit bb-a-USDC to stables via Relayer.
ExactIn - Exact amount of tokenIn to use in swap.
User must approve relayer
Vault must have approvals for tokens
*/
async function runRelayerSwapUnwrapExactIn() {
const config: BalancerSdkConfig = {
network: Network.MAINNET,
rpcUrl: `https://mainnet.infura.io/v3/${process.env.INFURA}`,
};
const provider = new JsonRpcProvider(config.rpcUrl);
const key: any = process.env.TRADER_KEY;
const relayerAddress = '0xAc9f49eF3ab0BbC929f7b1bb0A17E1Fca5786251';
const wallet = new Wallet(key, provider);
const balancer = new BalancerSDK(config);
// Creates fund management info for swap part of call
const funds: FundManagement = {
sender: wallet.address,
recipient: relayerAddress, // Note relayer is recipient of swaps
fromInternalBalance: false,
toInternalBalance: false,
};
const bbausd = '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb2';
const bbadai = '0x804CdB9116a10bB78768D3252355a1b18067bF8f';
const bbausdc = '0x9210F1204b5a24742Eba12f710636D76240dF3d0';
const bbausdt = '0x2BBf681cC4eb09218BEe85EA2a5d3D13Fa40fC0C';
const daiLinearPool = new Contract(bbadai, linearPoolAbi, provider);
const usdcLinearPool = new Contract(bbausdc, linearPoolAbi, provider);
const usdtLinearPool = new Contract(bbausdt, linearPoolAbi, provider);
// This is gets the up to date rates for the Aave tokens
const daiRate = await daiLinearPool.getWrappedTokenRate();
const usdcRate = await usdcLinearPool.getWrappedTokenRate();
const usdtRate = await usdtLinearPool.getWrappedTokenRate();
console.log(`DAI Rate: ${daiRate.toString()}`);
console.log(`USDC Rate: ${usdcRate.toString()}`);
console.log(`USDT Rate: ${usdtRate.toString()}`);
const txInfo = await balancer.relayer.swapUnwrapAaveStaticExactIn(
[bbausd, bbausd, bbausd],
[
'0x02d60b84491589974263d922d9cc7a3152618ef6', // waDAI
'0xd093fa4fb80d09bb30817fdcd442d4d02ed3e5de', // waUSDC
'0xf8fd466f12e236f4c96f7cce6c79eadb819abf58', // waUSDT
],
[
parseFixed('30000000', 18).toString(),
parseFixed('30000000', 18).toString(),
parseFixed('30000000', 18).toString(),
],
[daiRate, usdcRate, usdtRate],
funds,
'50000000000000000' // Slippage 5%
);
const relayerContract = new Contract(
relayerAddress,
balancerRelayerAbi,
provider
);
console.log(`Unwrapped Amounts Out:`);
console.log(txInfo.outputs?.amountsOut?.toString());
const tx = await relayerContract
.connect(wallet)
.callStatic[txInfo.function](txInfo.params, {
value: '0',
// gasLimit: '2000000',
});
console.log(`Swap Deltas:`);
console.log(defaultAbiCoder.decode(['int256[]'], tx[0]).toString());
}
/*
Example showing how to exit bb-a-USDC to stables via Relayer.
ExactOut - Exact amount of tokens out are used for swaps.
User must approve relayer
Vault must have approvals for tokens
*/
async function runRelayerSwapUnwrapExactOut() {
const config: BalancerSdkConfig = {
network: Network.MAINNET,
rpcUrl: `https://mainnet.infura.io/v3/${process.env.INFURA}`,
};
const provider = new JsonRpcProvider(config.rpcUrl);
const key: any = process.env.TRADER_KEY;
const relayerAddress = '0xAc9f49eF3ab0BbC929f7b1bb0A17E1Fca5786251';
const wallet = new Wallet(key, provider);
const balancer = new BalancerSDK(config);
// Creates fund management info for swap part of call
const funds: FundManagement = {
sender: wallet.address,
recipient: relayerAddress, // Note relayer is recipient of swaps
fromInternalBalance: false,
toInternalBalance: false,
};
const bbausd = '0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb2';
const bbadai = '0x804CdB9116a10bB78768D3252355a1b18067bF8f';
const bbausdc = '0x9210F1204b5a24742Eba12f710636D76240dF3d0';
const bbausdt = '0x2BBf681cC4eb09218BEe85EA2a5d3D13Fa40fC0C';
const daiLinearPool = new Contract(bbadai, linearPoolAbi, provider);
const usdcLinearPool = new Contract(bbausdc, linearPoolAbi, provider);
const usdtLinearPool = new Contract(bbausdt, linearPoolAbi, provider);
// This is gets the up to date rates for the Aave tokens
const daiRate = await daiLinearPool.getWrappedTokenRate();
const usdcRate = await usdcLinearPool.getWrappedTokenRate();
const usdtRate = await usdtLinearPool.getWrappedTokenRate();
console.log(`DAI Rate: ${daiRate.toString()}`);
console.log(`USDC Rate: ${usdcRate.toString()}`);
console.log(`USDT Rate: ${usdtRate.toString()}`);
const txInfo = await balancer.relayer.swapUnwrapAaveStaticExactOut(
[bbausd, bbausd, bbausd],
[
'0x02d60b84491589974263d922d9cc7a3152618ef6', // waDAI
'0xd093fa4fb80d09bb30817fdcd442d4d02ed3e5de', // waUSDC
'0xf8fd466f12e236f4c96f7cce6c79eadb819abf58', // waUSDT
],
[parseFixed('1', 16).toString(), '1000', '1000'], // Amount of unwrapped Aave token we want to receive
[daiRate, usdcRate, usdtRate],
funds,
'50000000000000000' // Slippage 5%
);
console.log(`Amounts In:`);
console.log(txInfo.outputs?.amountsIn?.toString());
const relayerContract = new Contract(
relayerAddress,
balancerRelayerAbi,
provider
);
const tx = await relayerContract
.connect(wallet)
.callStatic[txInfo.function](txInfo.params, {
value: '0',
// gasLimit: '2000000',
});
console.log(`Swap Deltas:`);
console.log(defaultAbiCoder.decode(['int256[]'], tx[0]).toString());
}
// yarn examples:run ./examples/relayerSwapUnwrap.ts
runRelayerSwapUnwrapExactIn();
// runRelayerSwapUnwrapExactOut();