-
Notifications
You must be signed in to change notification settings - Fork 11
/
swap.ts
112 lines (102 loc) · 3.25 KB
/
swap.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
/**
* Example showing how to find swap information for a token pair.
*
* Run with:
* pnpm example ./examples/swaps/swap.ts
*/
import { config } from 'dotenv';
config();
import {
BalancerApi,
API_ENDPOINT,
ChainId,
Slippage,
SwapKind,
Token,
TokenAmount,
Swap,
SwapBuildOutputExactIn,
SwapBuildOutputExactOut,
SwapInput,
} from '../../src';
const swap = async () => {
// User defined
const rpcUrl = process.env.POLYGON_RPC_URL;
const chainId = ChainId.POLYGON;
const swapKind = SwapKind.GivenIn;
const tokenIn = new Token(
chainId,
'0xfa68FB4628DFF1028CFEc22b4162FCcd0d45efb6',
18,
'MaticX',
);
const tokenOut = new Token(
chainId,
'0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
18,
'WMATIC',
);
const wethIsEth = false;
const slippage = Slippage.fromPercentage('0.1');
const swapAmount =
swapKind === SwapKind.GivenIn
? TokenAmount.fromHumanAmount(tokenIn, '1.2345678910')
: TokenAmount.fromHumanAmount(tokenOut, '1.2345678910');
const sender = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const recipient = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const deadline = 999999999999999999n; // Infinity
// API is used to fetch best path from available liquidity
const balancerApi = new BalancerApi(API_ENDPOINT, chainId);
const sorPaths = await balancerApi.sorSwapPaths.fetchSorSwapPaths({
chainId,
tokenIn: tokenIn.address,
tokenOut: tokenOut.address,
swapKind,
swapAmount,
});
const swapInput: SwapInput = {
chainId,
paths: sorPaths,
swapKind,
userData: '0x',
};
// Swap object provides useful helpers for re-querying, building call, etc
const swap = new Swap(swapInput);
console.log(
`Input token: ${swap.inputAmount.token.address}, Amount: ${swap.inputAmount.amount}`,
);
console.log(
`Output token: ${swap.outputAmount.token.address}, Amount: ${swap.outputAmount.amount}`,
);
// Get up to date swap result by querying onchain
const queryOutput = await swap.query(rpcUrl);
// Construct transaction to make swap
if (queryOutput.swapKind === SwapKind.GivenIn) {
console.log(`Updated amount: ${queryOutput.expectedAmountOut.amount}`);
const callData = swap.buildCall({
slippage,
deadline,
queryOutput,
sender,
recipient,
wethIsEth,
}) as SwapBuildOutputExactIn;
console.log(
`Min Amount Out: ${callData.minAmountOut.amount}\n\nTx Data:\nTo: ${callData.to}\nCallData: ${callData.callData}\nValue: ${callData.value}`,
);
} else {
console.log(`Updated amount: ${queryOutput.expectedAmountIn.amount}`);
const callData = swap.buildCall({
slippage,
deadline,
queryOutput,
sender,
recipient,
wethIsEth,
}) as SwapBuildOutputExactOut;
console.log(
`Max Amount In: ${callData.maxAmountIn.amount}\n\nTx Data:\nTo: ${callData.to}\nCallData: ${callData.callData}\nValue: ${callData.value}`,
);
}
};
export default swap;