/
index.ts
60 lines (57 loc) · 1.73 KB
/
index.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
import { Xfai } from '../../xfai';
import { BigNumber, PopulatedTransaction } from 'ethers';
import { Token, isNativeToken, isSharedToken } from '../../xfai-token';
import { TradeType } from '../../pool';
import type { TransactionOptions } from '../core';
import { swapTokensExactInput } from './exact-input';
import { swapTokensExactOutput } from './exact-output';
import { swapEthToWeth, swapWethToEth } from './native-wrapped';
export async function swapTokens<T = TradeType.EXACT_INPUT>(
xfai: Xfai,
tradeType: T,
tokenIn: Token,
tokenOut: Token,
amountIn: BigNumber,
desiredAmountOut: BigNumber,
options: TransactionOptions,
): Promise<PopulatedTransaction>;
export async function swapTokens<T = TradeType.EXACT_OUTPUT>(
xfai: Xfai,
tradeType: T,
tokenIn: Token,
tokenOut: Token,
desiredAmountIn: BigNumber,
amountOut: BigNumber,
options: TransactionOptions,
): Promise<PopulatedTransaction>;
/**
* Swap tokens.
* @param xfai
* @param tradeType
* @param tokenIn
* @param tokenOut
* @param amountIn
* @param amountOut
* @param options
* @returns
*/
export async function swapTokens<T extends TradeType>(
xfai: Xfai,
tradeType: T,
tokenIn: Token,
tokenOut: Token,
amountIn: BigNumber,
amountOut: BigNumber,
options: TransactionOptions,
): Promise<PopulatedTransaction> {
if (isSharedToken(xfai, tokenIn) && isSharedToken(xfai, tokenOut)) {
if (isNativeToken(tokenIn)) {
return swapEthToWeth(xfai, amountIn, options);
}
return swapWethToEth(xfai, amountIn, options);
}
if (tradeType === TradeType.EXACT_INPUT) {
return swapTokensExactInput(xfai, tokenIn, tokenOut, amountIn, amountOut, options);
}
return swapTokensExactOutput(xfai, tokenIn, tokenOut, amountIn, amountOut, options);
}