@rabby-wallet/rabby-swap 0.0.19 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/consts.ts DELETED
@@ -1,132 +0,0 @@
1
- import { CHAINS_ENUM } from "@debank/common";
2
- import { SUPPORT_CHAINS as wrapTokenSupportChains } from "./dexs/wrapToken";
3
- import { SUPPORT_CHAINS as zeroXSupportChains } from "./dexs/0xapi";
4
- import { SUPPORT_CHAINS as oneInchSupportChains } from "./dexs/1inch";
5
- import { SUPPORT_CHAINS as paraswapSupportChains } from "./dexs/paraswap";
6
- import { SUPPORT_CHAINS as openOceanSupportChains } from "./dexs/openocean";
7
-
8
- export enum DEX_ENUM {
9
- // ONEINCH = "1inch",
10
- PARASWAP = "ParaSwap",
11
- ZEROXAPI = "0xAPI",
12
- WRAPTOKEN = "WrapToken",
13
- OPENOCEAN = "OpenOcean",
14
- }
15
-
16
- export const WrapTokenAddressMap = {
17
- [CHAINS_ENUM.ETH]: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
18
- [CHAINS_ENUM.BSC]: "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
19
- [CHAINS_ENUM.POLYGON]: "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270",
20
- [CHAINS_ENUM.OP]: "0x4200000000000000000000000000000000000006",
21
- [CHAINS_ENUM.FTM]: "0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83",
22
- [CHAINS_ENUM.AVAX]: "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7",
23
- [CHAINS_ENUM.ARBITRUM]: "0x82af49447d8a07e3bd95bd0d56f35241523fbab1",
24
- [CHAINS_ENUM.KLAY]: "0xe4f05a66ec68b54a58b17c22107b02e0232cc817",
25
- [CHAINS_ENUM.GNOSIS]: "0xe91d153e0b41518a2ce8dd3d7944fa863463a97d",
26
- };
27
-
28
- export const DEX_SUPPORT_CHAINS = {
29
- [DEX_ENUM.WRAPTOKEN]: wrapTokenSupportChains,
30
- // [DEX_ENUM.ONEINCH]: oneInchSupportChains,
31
- [DEX_ENUM.PARASWAP]: paraswapSupportChains,
32
- [DEX_ENUM.ZEROXAPI]: zeroXSupportChains,
33
- [DEX_ENUM.OPENOCEAN]: openOceanSupportChains,
34
- };
35
-
36
- export const DEX_ROUTER_WHITELIST = {
37
- // [DEX_ENUM.ONEINCH]: {
38
- // [CHAINS_ENUM.ETH]: "0x1111111254eeb25477b68fb85ed929f73a960582",
39
- // [CHAINS_ENUM.POLYGON]: "0x1111111254eeb25477b68fb85ed929f73a960582",
40
- // [CHAINS_ENUM.BSC]: "0x1111111254eeb25477b68fb85ed929f73a960582",
41
- // [CHAINS_ENUM.OP]: "0x1111111254eeb25477b68fb85ed929f73a960582",
42
- // [CHAINS_ENUM.FTM]: "0x1111111254eeb25477b68fb85ed929f73a960582",
43
- // [CHAINS_ENUM.AVAX]: "0x1111111254eeb25477b68fb85ed929f73a960582",
44
- // [CHAINS_ENUM.ARBITRUM]: "0x1111111254eeb25477b68fb85ed929f73a960582",
45
- // [CHAINS_ENUM.KLAY]: "0x1111111254eeb25477b68fb85ed929f73a960582",
46
- // [CHAINS_ENUM.GNOSIS]: "0x1111111254eeb25477b68fb85ed929f73a960582",
47
- // [CHAINS_ENUM.CELO]: "0x1111111254eeb25477b68fb85ed929f73a960582",
48
- // },
49
- [DEX_ENUM.PARASWAP]: {
50
- [CHAINS_ENUM.ETH]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
51
- [CHAINS_ENUM.POLYGON]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
52
- [CHAINS_ENUM.BSC]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
53
- [CHAINS_ENUM.AVAX]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
54
- [CHAINS_ENUM.FTM]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
55
- [CHAINS_ENUM.ARBITRUM]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
56
- [CHAINS_ENUM.OP]: "0xdef171fe48cf0115b1d80b88dc8eab59176fee57",
57
- },
58
- [DEX_ENUM.ZEROXAPI]: {
59
- [CHAINS_ENUM.ETH]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
60
- [CHAINS_ENUM.POLYGON]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
61
- [CHAINS_ENUM.BSC]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
62
- [CHAINS_ENUM.OP]: "0xdef1abe32c034e558cdd535791643c58a13acc10",
63
- [CHAINS_ENUM.FTM]: "0xdef189deaef76e379df891899eb5a00a94cbc250",
64
- [CHAINS_ENUM.CELO]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
65
- [CHAINS_ENUM.AVAX]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
66
- [CHAINS_ENUM.ARBITRUM]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
67
- },
68
- [DEX_ENUM.OPENOCEAN]: {
69
- [CHAINS_ENUM.ETH]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
70
- [CHAINS_ENUM.POLYGON]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
71
- [CHAINS_ENUM.BSC]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
72
- [CHAINS_ENUM.OP]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
73
- [CHAINS_ENUM.FTM]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
74
- [CHAINS_ENUM.AVAX]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
75
- [CHAINS_ENUM.ARBITRUM]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
76
- [CHAINS_ENUM.CRO]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
77
- [CHAINS_ENUM.GNOSIS]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
78
- [CHAINS_ENUM.AURORA]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
79
- [CHAINS_ENUM.BOBA]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
80
- [CHAINS_ENUM.KAVA]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
81
- [CHAINS_ENUM.METIS]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
82
- },
83
- };
84
-
85
- export const DEX_SPENDER_WHITELIST = {
86
- // [DEX_ENUM.ONEINCH]: {
87
- // [CHAINS_ENUM.ETH]: "0x1111111254eeb25477b68fb85ed929f73a960582",
88
- // [CHAINS_ENUM.POLYGON]: "0x1111111254eeb25477b68fb85ed929f73a960582",
89
- // [CHAINS_ENUM.BSC]: "0x1111111254eeb25477b68fb85ed929f73a960582",
90
- // [CHAINS_ENUM.OP]: "0x1111111254eeb25477b68fb85ed929f73a960582",
91
- // [CHAINS_ENUM.FTM]: "0x1111111254eeb25477b68fb85ed929f73a960582",
92
- // [CHAINS_ENUM.AVAX]: "0x1111111254eeb25477b68fb85ed929f73a960582",
93
- // [CHAINS_ENUM.ARBITRUM]: "0x1111111254eeb25477b68fb85ed929f73a960582",
94
- // [CHAINS_ENUM.KLAY]: "0x1111111254eeb25477b68fb85ed929f73a960582",
95
- // [CHAINS_ENUM.GNOSIS]: "0x1111111254eeb25477b68fb85ed929f73a960582",
96
- // [CHAINS_ENUM.CELO]: "0x1111111254eeb25477b68fb85ed929f73a960582",
97
- // },
98
- [DEX_ENUM.PARASWAP]: {
99
- [CHAINS_ENUM.ETH]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
100
- [CHAINS_ENUM.POLYGON]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
101
- [CHAINS_ENUM.BSC]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
102
- [CHAINS_ENUM.AVAX]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
103
- [CHAINS_ENUM.FTM]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
104
- [CHAINS_ENUM.OP]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
105
- [CHAINS_ENUM.ARBITRUM]: "0x216b4b4ba9f3e719726886d34a177484278bfcae",
106
- },
107
- [DEX_ENUM.ZEROXAPI]: {
108
- [CHAINS_ENUM.ETH]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
109
- [CHAINS_ENUM.POLYGON]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
110
- [CHAINS_ENUM.BSC]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
111
- [CHAINS_ENUM.OP]: "0xdef1abe32c034e558cdd535791643c58a13acc10",
112
- [CHAINS_ENUM.FTM]: "0xdef189deaef76e379df891899eb5a00a94cbc250",
113
- [CHAINS_ENUM.CELO]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
114
- [CHAINS_ENUM.AVAX]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
115
- [CHAINS_ENUM.ARBITRUM]: "0xdef1c0ded9bec7f1a1670819833240f027b25eff",
116
- },
117
- [DEX_ENUM.OPENOCEAN]: {
118
- [CHAINS_ENUM.ETH]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
119
- [CHAINS_ENUM.POLYGON]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
120
- [CHAINS_ENUM.BSC]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
121
- [CHAINS_ENUM.OP]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
122
- [CHAINS_ENUM.FTM]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
123
- [CHAINS_ENUM.AVAX]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
124
- [CHAINS_ENUM.ARBITRUM]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
125
- [CHAINS_ENUM.CRO]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
126
- [CHAINS_ENUM.GNOSIS]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
127
- [CHAINS_ENUM.AURORA]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
128
- [CHAINS_ENUM.BOBA]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
129
- [CHAINS_ENUM.KAVA]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
130
- [CHAINS_ENUM.METIS]: "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64",
131
- },
132
- };
package/src/dexs/0xapi.ts DELETED
@@ -1,158 +0,0 @@
1
- import axios from "axios";
2
- import { Interface } from "@ethersproject/abi";
3
- import { CHAINS_ENUM, CHAINS } from "@debank/common";
4
- import {
5
- QuoteParams,
6
- Tx,
7
- QuoteResult,
8
- TxWithChainId,
9
- DecodeCalldataResult,
10
- } from "../quote";
11
- import { isSameAddress } from "../utils";
12
- import { ZeroXABI } from "../abi";
13
-
14
- const NATIVE_TOKEN = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
15
-
16
- const API_DOMAINS = {
17
- [CHAINS_ENUM.ETH]: "https://api.0x.org",
18
- [CHAINS_ENUM.POLYGON]: "https://polygon.api.0x.org",
19
- [CHAINS_ENUM.BSC]: "https://bsc.api.0x.org",
20
- [CHAINS_ENUM.OP]: "https://optimism.api.0x.org",
21
- [CHAINS_ENUM.FTM]: "https://fantom.api.0x.org",
22
- [CHAINS_ENUM.CELO]: "https://celo.api.0x.org",
23
- [CHAINS_ENUM.AVAX]: "https://avalanche.api.0x.org",
24
- [CHAINS_ENUM.ARBITRUM]: "https://arbitrum.api.0x.org",
25
- };
26
-
27
- export const SUPPORT_CHAINS = [
28
- CHAINS_ENUM.ETH,
29
- CHAINS_ENUM.POLYGON,
30
- CHAINS_ENUM.BSC,
31
- CHAINS_ENUM.OP,
32
- CHAINS_ENUM.FTM,
33
- CHAINS_ENUM.AVAX,
34
- CHAINS_ENUM.ARBITRUM,
35
- CHAINS_ENUM.CELO,
36
- ];
37
-
38
- interface SwapParams {
39
- sellToken: string;
40
- buyToken: string;
41
- sellAmount: string;
42
- takerAddress: string;
43
- slippagePercentage: number;
44
- skipValidation: boolean;
45
- buyTokenPercentageFee?: number;
46
- feeRecipient?: string;
47
- affiliateAddress?: string;
48
- }
49
-
50
- interface SwapResponse extends Tx {
51
- allowanceTarget: string;
52
- buyAmount: string;
53
- sellAmount: string;
54
- buyTokenAddress: string;
55
- sellTokenAddress: string;
56
- }
57
-
58
- export const getQuote = async (options: QuoteParams): Promise<QuoteResult> => {
59
- if (!SUPPORT_CHAINS.includes(options.chain)) {
60
- throw new Error(`${CHAINS[options.chain]} is not support on 1inch`);
61
- }
62
- const baseURL = API_DOMAINS[options.chain as keyof typeof API_DOMAINS];
63
- const request = axios.create({
64
- baseURL,
65
- });
66
-
67
- request.interceptors.response.use((response) => {
68
- const code = response.data?.code;
69
- const msg = response.data?.reason;
70
-
71
- if (code && code !== 200) {
72
- if (msg) {
73
- let err;
74
- try {
75
- err = new Error(JSON.parse(msg));
76
- } catch (e) {
77
- err = new Error(msg);
78
- }
79
- throw err;
80
- }
81
- throw new Error(response.data);
82
- }
83
- return response;
84
- });
85
-
86
- const params: SwapParams = {
87
- sellToken:
88
- options.fromToken === CHAINS[options.chain].nativeTokenAddress
89
- ? NATIVE_TOKEN
90
- : options.fromToken,
91
- buyToken:
92
- options.toToken === CHAINS[options.chain].nativeTokenAddress
93
- ? NATIVE_TOKEN
94
- : options.toToken,
95
- sellAmount: options.amount,
96
- takerAddress: options.userAddress,
97
- slippagePercentage: options.slippage / 100,
98
- skipValidation: true,
99
- };
100
-
101
- if (options.feeRate !== null && options.feeRate !== undefined) {
102
- params.buyTokenPercentageFee = options.feeRate / 100;
103
- params.feeRecipient = options.feeAddress;
104
- params.affiliateAddress = options.feeAddress;
105
- }
106
-
107
- const { data } = await request.get<SwapResponse>("/swap/v1/quote", {
108
- params,
109
- });
110
-
111
- return {
112
- tx: {
113
- data: data.data,
114
- to: data.to,
115
- value: data.value,
116
- from: data.from,
117
- },
118
- fromToken: isSameAddress(data.sellTokenAddress, NATIVE_TOKEN)
119
- ? CHAINS[options.chain].nativeTokenAddress
120
- : data.sellTokenAddress,
121
- spender: data.allowanceTarget,
122
- fromTokenAmount: data.sellAmount,
123
- toToken: isSameAddress(data.buyTokenAddress, NATIVE_TOKEN)
124
- ? CHAINS[options.chain].nativeTokenAddress
125
- : data.buyTokenAddress,
126
- toTokenAmount: data.buyAmount,
127
- };
128
- };
129
-
130
- export const decodeCalldata = (
131
- tx: TxWithChainId
132
- ): DecodeCalldataResult | null => {
133
- const chain = Object.values(CHAINS).find((item) => item.id === tx.chainId);
134
- if (!chain) return null;
135
- const contractInterface = new Interface(ZeroXABI);
136
- const result = contractInterface.parseTransaction({ data: tx.data });
137
- if (result.name !== "transformERC20") {
138
- return null;
139
- }
140
-
141
- const [inputToken, outputToken, inputTokenAmount, minOutputTokenAmount] =
142
- result.args;
143
-
144
- if (!inputToken || !outputToken || !inputTokenAmount || !minOutputTokenAmount)
145
- return null;
146
-
147
- return {
148
- fromToken: isSameAddress(inputToken, NATIVE_TOKEN)
149
- ? chain.nativeTokenAddress
150
- : inputToken,
151
- fromTokenAmount: inputTokenAmount.toString(),
152
- toToken: isSameAddress(outputToken, NATIVE_TOKEN)
153
- ? chain.nativeTokenAddress
154
- : outputToken,
155
- minReceiveToTokenAmount: minOutputTokenAmount.toString(),
156
- toTokenReceiver: tx.from,
157
- };
158
- };
package/src/dexs/1inch.ts DELETED
@@ -1,165 +0,0 @@
1
- import axios from "axios";
2
- import { CHAINS_ENUM, CHAINS } from "@debank/common";
3
- import { Interface } from "@ethersproject/abi";
4
- import {
5
- QuoteParams,
6
- Tx,
7
- QuoteResult,
8
- TxWithChainId,
9
- DecodeCalldataResult,
10
- } from "../quote";
11
- import { isSameAddress } from "../utils";
12
- import { OneInchABI } from "../abi";
13
-
14
- const NATIVE_TOKEN = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
15
-
16
- const request = axios.create({
17
- baseURL: "https://api.1inch.io/v5.0",
18
- });
19
-
20
- request.interceptors.response.use((response) => {
21
- const code = response.data?.statusCode;
22
- const msg = response.data?.description || response.data?.error;
23
-
24
- if (code && code !== 200) {
25
- if (msg) {
26
- let err;
27
- try {
28
- err = new Error(JSON.parse(msg));
29
- } catch (e) {
30
- err = new Error(msg);
31
- }
32
- throw err;
33
- }
34
- throw new Error(response.data);
35
- }
36
- return response;
37
- });
38
-
39
- export const SUPPORT_CHAINS = [
40
- CHAINS_ENUM.ETH,
41
- CHAINS_ENUM.POLYGON,
42
- CHAINS_ENUM.BSC,
43
- CHAINS_ENUM.OP,
44
- CHAINS_ENUM.FTM,
45
- CHAINS_ENUM.AVAX,
46
- CHAINS_ENUM.ARBITRUM,
47
- CHAINS_ENUM.KLAY,
48
- CHAINS_ENUM.GNOSIS,
49
- ];
50
-
51
- interface SwapParams {
52
- fromTokenAddress: string;
53
- toTokenAddress: string;
54
- amount: string;
55
- fromAddress: string;
56
- slippage: number;
57
- disableEstimate: boolean;
58
- fee?: number;
59
- referrerAddress?: string;
60
- }
61
-
62
- type IToken = {
63
- symbol: string;
64
- name: string;
65
- decimals: number;
66
- address: string;
67
- logoURI: string;
68
- tags: string[];
69
- eip2612?: boolean;
70
- };
71
-
72
- interface SwapResponse {
73
- fromToken: IToken;
74
- toToken: IToken;
75
- toTokenAmount: string;
76
- fromTokenAmount: string;
77
- tx: Tx;
78
- }
79
-
80
- export const getQuote = async (options: QuoteParams): Promise<QuoteResult> => {
81
- const chainId = CHAINS[options.chain].id;
82
-
83
- if (!SUPPORT_CHAINS.includes(options.chain)) {
84
- throw new Error(`${CHAINS[options.chain]} is not support on 1inch`);
85
- }
86
-
87
- const params: SwapParams = {
88
- fromTokenAddress:
89
- options.fromToken === CHAINS[options.chain].nativeTokenAddress
90
- ? NATIVE_TOKEN
91
- : options.fromToken,
92
- toTokenAddress:
93
- options.toToken === CHAINS[options.chain].nativeTokenAddress
94
- ? NATIVE_TOKEN
95
- : options.toToken,
96
- amount: options.amount,
97
- fromAddress: options.userAddress,
98
- slippage: options.slippage,
99
- disableEstimate: true,
100
- };
101
-
102
- if (options.feeRate !== null && options.feeRate !== undefined) {
103
- params.fee = options.feeRate;
104
- params.referrerAddress = options.feeAddress;
105
- }
106
-
107
- const { data } = await request.get<SwapResponse>(`/${chainId}/swap`, {
108
- params,
109
- });
110
-
111
- return {
112
- tx: data.tx,
113
- fromToken:
114
- data.fromToken.address === NATIVE_TOKEN
115
- ? CHAINS[options.chain].nativeTokenAddress
116
- : data.fromToken.address,
117
- fromTokenAmount: data.fromTokenAmount,
118
- fromTokenDecimals: data.fromToken.decimals,
119
- toToken:
120
- data.toToken.address === NATIVE_TOKEN
121
- ? CHAINS[options.chain].nativeTokenAddress
122
- : data.toToken.address,
123
- toTokenAmount: data.toTokenAmount,
124
- toTokenDecimals: data.toToken.decimals,
125
- spender: data.tx.to,
126
- };
127
- };
128
-
129
- export const decodeCalldata = (
130
- tx: TxWithChainId
131
- ): DecodeCalldataResult | null => {
132
- const chain = Object.values(CHAINS).find((item) => item.id === tx.chainId);
133
- if (!chain) return null;
134
- const contractInterface = new Interface(OneInchABI);
135
- const result = contractInterface.parseTransaction({ data: tx.data });
136
-
137
- if (result.name !== "swap") {
138
- return null;
139
- }
140
-
141
- const [executor, desc] = result.args;
142
- const [
143
- srcToken,
144
- dstToken,
145
- srcReceiver,
146
- dstReceiver,
147
- amount,
148
- minReturnAmount,
149
- ] = desc;
150
-
151
- if (!srcToken || !dstToken || !dstReceiver || !amount || !minReturnAmount)
152
- return null;
153
-
154
- return {
155
- fromToken: isSameAddress(srcToken, NATIVE_TOKEN)
156
- ? chain.nativeTokenAddress
157
- : srcToken,
158
- fromTokenAmount: amount.toString(),
159
- toToken: isSameAddress(dstToken, NATIVE_TOKEN)
160
- ? chain.nativeTokenAddress
161
- : dstToken,
162
- minReceiveToTokenAmount: minReturnAmount.toString(),
163
- toTokenReceiver: dstReceiver,
164
- };
165
- };
@@ -1,217 +0,0 @@
1
- import axios from "axios";
2
- import { Interface } from "@ethersproject/abi";
3
- import { CHAINS_ENUM, CHAINS } from "@debank/common";
4
- import BigNumber from "bignumber.js";
5
- import {
6
- QuoteParams,
7
- Tx,
8
- QuoteResult,
9
- TxWithChainId,
10
- DecodeCalldataResult,
11
- } from "../quote";
12
- import { isSameAddress } from "../utils";
13
- import { OpenOceanABI } from "../abi";
14
-
15
- const NATIVE_TOKENS = {
16
- [CHAINS_ENUM.ETH]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
17
- [CHAINS_ENUM.POLYGON]: "0x0000000000000000000000000000000000001010",
18
- [CHAINS_ENUM.BSC]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
19
- [CHAINS_ENUM.OP]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
20
- [CHAINS_ENUM.FTM]: "0x0000000000000000000000000000000000000000",
21
- [CHAINS_ENUM.AVAX]: "0x0000000000000000000000000000000000000000",
22
- [CHAINS_ENUM.ARBITRUM]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
23
- [CHAINS_ENUM.CRO]: "0x0000000000000000000000000000000000000000",
24
- [CHAINS_ENUM.GNOSIS]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
25
- [CHAINS_ENUM.AURORA]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
26
- [CHAINS_ENUM.BOBA]: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
27
- [CHAINS_ENUM.KAVA]: "0x0000000000000000000000000000000000000000",
28
- [CHAINS_ENUM.METIS]: "0x0000000000000000000000000000000000000000",
29
- } as Record<CHAINS_ENUM, string>;
30
-
31
- const chainCode = {
32
- [CHAINS_ENUM.ETH]: "eth",
33
- [CHAINS_ENUM.POLYGON]: "polygon",
34
- [CHAINS_ENUM.BSC]: "bsc",
35
- [CHAINS_ENUM.OP]: "optimism",
36
- [CHAINS_ENUM.FTM]: "fantom",
37
- [CHAINS_ENUM.AVAX]: "avax",
38
- [CHAINS_ENUM.ARBITRUM]: "arbitrum",
39
- [CHAINS_ENUM.CRO]: "cronos",
40
- [CHAINS_ENUM.GNOSIS]: "xdai",
41
- [CHAINS_ENUM.AURORA]: "aurora",
42
- [CHAINS_ENUM.BOBA]: "boba",
43
- [CHAINS_ENUM.KAVA]: "kava",
44
- [CHAINS_ENUM.METIS]: "metis",
45
- };
46
-
47
- export const SUPPORT_CHAINS = [
48
- CHAINS_ENUM.ETH,
49
- CHAINS_ENUM.POLYGON,
50
- CHAINS_ENUM.BSC,
51
- CHAINS_ENUM.OP,
52
- CHAINS_ENUM.FTM,
53
- CHAINS_ENUM.AVAX,
54
- CHAINS_ENUM.ARBITRUM,
55
-
56
- CHAINS_ENUM.CRO,
57
-
58
- /**
59
- * 接口返回 swap error
60
- */
61
- // CHAINS_ENUM.METIS,
62
-
63
- /**
64
- * pre api v1
65
- */
66
- // CHAINS_ENUM.AURORA,
67
-
68
- /**
69
- * 未开源
70
- */
71
- // CHAINS_ENUM.GNOSIS,
72
- // CHAINS_ENUM.BOBA,
73
- // CHAINS_ENUM.KAVA, //pre api v1
74
- ];
75
-
76
- interface SwapParams {
77
- inTokenAddress: string;
78
- outTokenAddress: string;
79
- amount: string;
80
- gasPrice: number;
81
- slippage: number;
82
- account: string;
83
- referrer?: string;
84
- referrerFee?: number;
85
- }
86
-
87
- interface OpenOceanTokenItem {
88
- address: string;
89
- decimals: number;
90
- name: string;
91
- symbol: string;
92
- }
93
-
94
- interface SwapResponse extends Tx {
95
- minOutAmount: string;
96
- outAmount: string;
97
- inAmount: string;
98
- outToken: OpenOceanTokenItem;
99
- inToken: OpenOceanTokenItem;
100
- }
101
-
102
- export const getQuote = async (options: QuoteParams): Promise<QuoteResult> => {
103
- if (!SUPPORT_CHAINS.includes(options.chain)) {
104
- throw new Error(`${CHAINS[options.chain]} is not support on OpenOcean`);
105
- }
106
- const swapChainCode = chainCode[options.chain as keyof typeof chainCode];
107
- const baseURL = `https://open-api.openocean.finance/v3/${swapChainCode}`;
108
- const request = axios.create({
109
- baseURL,
110
- });
111
-
112
- request.interceptors.response.use((response) => {
113
- const code = response.data?.code;
114
- const msg = response.data?.reason;
115
-
116
- if (code === 200) return response.data;
117
-
118
- if (code && code !== 200) {
119
- if (msg) {
120
- let err;
121
- try {
122
- err = new Error(JSON.parse(msg));
123
- } catch (e) {
124
- err = new Error(msg);
125
- }
126
- throw err;
127
- }
128
- throw new Error(response.data);
129
- }
130
- return response;
131
- });
132
-
133
- const NATIVE_TOKEN = NATIVE_TOKENS[options.chain];
134
-
135
- const params: SwapParams = {
136
- inTokenAddress:
137
- options.fromToken === CHAINS[options.chain].nativeTokenAddress
138
- ? NATIVE_TOKEN
139
- : options.fromToken,
140
- outTokenAddress:
141
- options.toToken === CHAINS[options.chain].nativeTokenAddress
142
- ? NATIVE_TOKEN
143
- : options.toToken,
144
- amount: new BigNumber(options.amount)
145
- .div(10 ** options.fromTokenDecimals)
146
- .toFixed(),
147
- slippage: options.slippage,
148
- gasPrice: options.gasPrice || 1,
149
- account: options.userAddress,
150
- };
151
-
152
- if (options.feeRate !== null && options.feeRate !== undefined) {
153
- params.referrerFee = options.feeRate;
154
- params.referrer = options.feeAddress;
155
- }
156
-
157
- const { data } = await request.get<SwapResponse>("/swap_quote", {
158
- params,
159
- });
160
-
161
- return {
162
- tx: {
163
- data: data.data,
164
- to: data.to,
165
- value: data.value,
166
- from: data.from,
167
- },
168
- fromToken: isSameAddress(data.inToken.address, NATIVE_TOKEN)
169
- ? CHAINS[options.chain].nativeTokenAddress
170
- : data.inToken.address,
171
- spender: data.to,
172
- fromTokenAmount: data.inAmount,
173
- toToken: isSameAddress(data.outToken.address, NATIVE_TOKEN)
174
- ? CHAINS[options.chain].nativeTokenAddress
175
- : data.outToken.address,
176
- toTokenAmount: data.outAmount,
177
- };
178
- };
179
-
180
- export const decodeCalldata = (
181
- tx: TxWithChainId
182
- ): DecodeCalldataResult | null => {
183
- const chain = Object.values(CHAINS).find((item) => item.id === tx.chainId);
184
- if (!chain) return null;
185
- const contractInterface = new Interface(OpenOceanABI);
186
- const result = contractInterface.parseTransaction({ data: tx.data });
187
- if (result.name !== "swap") {
188
- return null;
189
- }
190
-
191
- const [caller, data] = result.args;
192
- const [
193
- srcToken,
194
- dstToken,
195
- srcReceiver,
196
- dstReceiver,
197
- amount,
198
- minReturnAmount,
199
- ] = data;
200
-
201
- if (!srcToken || !dstToken || !dstReceiver || !amount || !minReturnAmount)
202
- return null;
203
-
204
- const NATIVE_TOKEN = NATIVE_TOKENS[chain.enum];
205
-
206
- return {
207
- fromToken: isSameAddress(srcToken, NATIVE_TOKEN)
208
- ? chain.nativeTokenAddress
209
- : srcToken,
210
- fromTokenAmount: amount.toString(),
211
- toToken: isSameAddress(dstToken, NATIVE_TOKEN)
212
- ? chain.nativeTokenAddress
213
- : dstToken,
214
- minReceiveToTokenAmount: minReturnAmount.toString(),
215
- toTokenReceiver: tx.from,
216
- };
217
- };