@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.
@@ -1,280 +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 { ParaSwapABI } from "../abi";
13
- import { DEX_ENUM, DEX_SPENDER_WHITELIST } from "../consts";
14
-
15
- const NATIVE_TOKEN = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
16
-
17
- const request = axios.create({
18
- baseURL: "https://apiv5.paraswap.io",
19
- });
20
-
21
- request.interceptors.response.use((response) => {
22
- const code = response.data?.statusCode;
23
- const msg = response.data?.description || response.data?.error;
24
-
25
- if (code && code !== 200) {
26
- if (msg) {
27
- let err;
28
- try {
29
- err = new Error(JSON.parse(msg));
30
- } catch (e) {
31
- err = new Error(msg);
32
- }
33
- throw err;
34
- }
35
- throw new Error(response.data);
36
- }
37
- return response;
38
- });
39
-
40
- export const SUPPORT_CHAINS = [
41
- CHAINS_ENUM.ETH,
42
- CHAINS_ENUM.POLYGON,
43
- CHAINS_ENUM.BSC,
44
- CHAINS_ENUM.AVAX,
45
- CHAINS_ENUM.OP,
46
- CHAINS_ENUM.ARBITRUM,
47
- CHAINS_ENUM.FTM,
48
- ];
49
-
50
- interface PriceParams {
51
- srcToken: string;
52
- srcDecimals: number;
53
- destToken: string;
54
- amount: string;
55
- userAddress: string;
56
- network: number;
57
- includeContractMethods: string;
58
- partner: string;
59
- excludeDEXS: string;
60
- }
61
-
62
- interface PriceResponse {
63
- priceRoute: PriceRoute;
64
- }
65
-
66
- interface PriceRoute {
67
- blockNumber: number;
68
- network: number;
69
- srcToken: string;
70
- srcDecimals: number;
71
- srcAmount: string;
72
- destToken: string;
73
- destDecimals: number;
74
- destAmount: string;
75
- bestRoute: any[];
76
- gasCostUSD: string;
77
- gasCost: string;
78
- side: string;
79
- tokenTransferProxy: string;
80
- contractAddress: string;
81
- contractMethod: string;
82
- partnerFee: number;
83
- srcUSD: string;
84
- destUSD: string;
85
- partner: string;
86
- maxImpactReached: boolean;
87
- hmac: string;
88
- }
89
-
90
- interface SwapParams {
91
- srcToken: string;
92
- srcDecimals: number;
93
- destToken: string;
94
- slippage: number;
95
- partnerAddress?: string;
96
- partnerFeeBps?: string;
97
- userAddress: string;
98
- srcAmount: string;
99
- priceRoute: PriceRoute;
100
- partner: string;
101
- deadline: number;
102
- }
103
-
104
- const getPriceRoute = async (params: PriceParams): Promise<PriceResponse> => {
105
- const { data } = await request.get("/prices", {
106
- params,
107
- });
108
- return data;
109
- };
110
-
111
- export const getQuote = async (options: QuoteParams): Promise<QuoteResult> => {
112
- const chainId = CHAINS[options.chain].id;
113
-
114
- if (!SUPPORT_CHAINS.includes(options.chain)) {
115
- throw new Error(`${CHAINS[options.chain]} is not support on 1inch`);
116
- }
117
-
118
- const { priceRoute } = await getPriceRoute({
119
- srcDecimals: options.fromTokenDecimals,
120
- srcToken:
121
- options.fromToken === CHAINS[options.chain].nativeTokenAddress
122
- ? NATIVE_TOKEN
123
- : options.fromToken,
124
- destToken:
125
- options.toToken === CHAINS[options.chain].nativeTokenAddress
126
- ? NATIVE_TOKEN
127
- : options.toToken,
128
- amount: options.amount,
129
- userAddress: options.userAddress,
130
- network: chainId,
131
- includeContractMethods: "simpleSwap,multiSwap,megaSwap",
132
- excludeDEXS: "ParaSwapPool,ParaSwapLimitOrders,Hashflow",
133
- partner: "Rabby",
134
- });
135
-
136
- const params: SwapParams = {
137
- srcToken:
138
- options.fromToken === CHAINS[options.chain].nativeTokenAddress
139
- ? NATIVE_TOKEN
140
- : options.fromToken,
141
- srcDecimals: options.fromTokenDecimals,
142
- destToken:
143
- options.toToken === CHAINS[options.chain].nativeTokenAddress
144
- ? NATIVE_TOKEN
145
- : options.toToken,
146
- srcAmount: options.amount,
147
- userAddress: options.userAddress,
148
- slippage: Math.floor(options.slippage * 100),
149
- priceRoute: priceRoute,
150
- partner: "Rabby",
151
- deadline: Math.floor(Date.now() / 1000) + 21600,
152
- };
153
-
154
- if (options.feeRate !== null && options.feeRate !== undefined) {
155
- params.partnerFeeBps = (options.feeRate * 100).toString();
156
- params.partnerAddress = options.feeAddress;
157
- }
158
-
159
- const { data } = await request.post<Tx>(`/transactions/${chainId}`, params, {
160
- params: {
161
- ignoreChecks: true,
162
- ignoreGasEstimate: true,
163
- },
164
- });
165
-
166
- return {
167
- tx: data,
168
- fromToken:
169
- priceRoute.srcToken === NATIVE_TOKEN
170
- ? CHAINS[options.chain].nativeTokenAddress
171
- : priceRoute.srcToken,
172
- fromTokenAmount: priceRoute.srcAmount,
173
- fromTokenDecimals: priceRoute.srcDecimals,
174
- toToken:
175
- priceRoute.destToken === NATIVE_TOKEN
176
- ? CHAINS[options.chain].nativeTokenAddress
177
- : priceRoute.destToken,
178
- toTokenAmount: priceRoute.destAmount,
179
- toTokenDecimals: priceRoute.destDecimals,
180
- spender:
181
- DEX_SPENDER_WHITELIST[DEX_ENUM.PARASWAP][
182
- options.chain as keyof (typeof DEX_SPENDER_WHITELIST)[DEX_ENUM.PARASWAP]
183
- ],
184
- };
185
- };
186
-
187
- export const decodeCalldata = (
188
- tx: TxWithChainId
189
- ): DecodeCalldataResult | null => {
190
- const chain = Object.values(CHAINS).find((item) => item.id === tx.chainId);
191
- const nullAddress = "0x0000000000000000000000000000000000000000";
192
-
193
- if (!chain) return null;
194
-
195
- const contractInterface = new Interface(ParaSwapABI);
196
- const result = contractInterface.parseTransaction({
197
- data: tx.data,
198
- });
199
- switch (result.name) {
200
- case "simpleSwap": {
201
- const [data] = result.args;
202
- const [
203
- fromToken,
204
- toToken,
205
- fromAmount,
206
- toAmount,
207
- expectedAmount,
208
- callees,
209
- exchangeData,
210
- startIndexes,
211
- values,
212
- beneficiary,
213
- ] = data;
214
- if (!fromToken || !toToken || !fromAmount || !toAmount) return null;
215
- return {
216
- fromToken: isSameAddress(fromToken, NATIVE_TOKEN)
217
- ? chain.nativeTokenAddress
218
- : fromToken,
219
- fromTokenAmount: fromAmount.toString(),
220
- toToken: isSameAddress(toToken, NATIVE_TOKEN)
221
- ? chain.nativeTokenAddress
222
- : toToken,
223
- minReceiveToTokenAmount: toAmount.toString(),
224
- toTokenReceiver: beneficiary === nullAddress ? tx.from : beneficiary,
225
- };
226
- }
227
- case "multiSwap": {
228
- const [data] = result.args;
229
- const [
230
- fromToken,
231
- fromAmount,
232
- toAmount,
233
- expectedAmount,
234
- beneficiary,
235
- path,
236
- ] = data;
237
- if (!fromToken || !fromAmount || !toAmount || !path || path.length <= 0)
238
- return null;
239
- const [toToken] = path[path.length - 1];
240
- return {
241
- fromToken: isSameAddress(fromToken, NATIVE_TOKEN)
242
- ? chain.nativeTokenAddress
243
- : fromToken,
244
- fromTokenAmount: fromAmount.toString(),
245
- toToken: isSameAddress(toToken, NATIVE_TOKEN)
246
- ? chain.nativeTokenAddress
247
- : toToken,
248
- minReceiveToTokenAmount: toAmount.toString(),
249
- toTokenReceiver: beneficiary === nullAddress ? tx.from : beneficiary,
250
- };
251
- }
252
- case "megaSwap":
253
- const [data] = result.args;
254
- const [
255
- fromToken,
256
- fromAmount,
257
- toAmount,
258
- expectedAmount,
259
- beneficiary,
260
- path,
261
- ] = data;
262
- if (!fromToken || !fromAmount || !toAmount || !path || path.length <= 0)
263
- return null;
264
- const [fromAmountPercent, swapPath] = path[0];
265
- const [toToken] = swapPath[swapPath.length - 1];
266
- return {
267
- fromToken: isSameAddress(fromToken, NATIVE_TOKEN)
268
- ? chain.nativeTokenAddress
269
- : fromToken,
270
- fromTokenAmount: fromAmount.toString(),
271
- toToken: isSameAddress(toToken, NATIVE_TOKEN)
272
- ? chain.nativeTokenAddress
273
- : toToken,
274
- minReceiveToTokenAmount: toAmount.toString(),
275
- toTokenReceiver: beneficiary === nullAddress ? tx.from : beneficiary,
276
- };
277
- default:
278
- return null;
279
- }
280
- };
@@ -1,74 +0,0 @@
1
- import abiCoder from "web3-eth-abi";
2
- import { CHAINS_ENUM, CHAINS } from "@debank/common";
3
- import { WrapTokenAddressMap } from "../consts";
4
- import { QuoteParams, Tx, QuoteResult } from "../quote";
5
- import { isSameAddress } from "../utils";
6
-
7
- export const SUPPORT_CHAINS = [
8
- CHAINS_ENUM.ETH,
9
- CHAINS_ENUM.POLYGON,
10
- CHAINS_ENUM.BSC,
11
- CHAINS_ENUM.OP,
12
- CHAINS_ENUM.FTM,
13
- CHAINS_ENUM.AVAX,
14
- CHAINS_ENUM.ARBITRUM,
15
- CHAINS_ENUM.KLAY,
16
- CHAINS_ENUM.GNOSIS,
17
- CHAINS_ENUM.CELO,
18
- ];
19
-
20
- export const getQuote = async (options: QuoteParams): Promise<QuoteResult> => {
21
- const chain = CHAINS[options.chain];
22
-
23
- if (!SUPPORT_CHAINS.includes(options.chain)) {
24
- throw new Error(`${CHAINS[options.chain]} is not support on 1inch`);
25
- }
26
- const wrapTokenAddress = WrapTokenAddressMap[options.chain as keyof typeof WrapTokenAddressMap];
27
- let calldata = "";
28
- let value = "0";
29
- if (
30
- options.fromToken === chain.nativeTokenAddress &&
31
- isSameAddress(
32
- options.toToken,
33
- wrapTokenAddress
34
- )
35
- ) {
36
- calldata = abiCoder.encodeFunctionCall(
37
- {
38
- constant: false,
39
- inputs: [],
40
- name: "deposit",
41
- outputs: [],
42
- payable: true,
43
- stateMutability: "payable",
44
- type: "function",
45
- },
46
- []
47
- );
48
- value = options.amount;
49
- } else {
50
- calldata = abiCoder.encodeFunctionCall({
51
- constant: false,
52
- inputs: [{ name: "wad", type: "uint256" }],
53
- name: "withdraw",
54
- outputs: [],
55
- payable: false,
56
- stateMutability: "nonpayable",
57
- type: "function",
58
- }, [options.amount]);
59
- }
60
-
61
- return {
62
- tx: {
63
- from: options.userAddress,
64
- to: wrapTokenAddress,
65
- value,
66
- data: calldata,
67
- },
68
- fromToken: options.fromToken,
69
- fromTokenAmount: options.amount,
70
- toToken: options.toToken,
71
- toTokenAmount: options.amount,
72
- spender: wrapTokenAddress,
73
- }
74
- };
package/src/index.ts DELETED
@@ -1,17 +0,0 @@
1
- import {
2
- WrapTokenAddressMap,
3
- DEX_ENUM,
4
- DEX_SUPPORT_CHAINS,
5
- DEX_ROUTER_WHITELIST,
6
- DEX_SPENDER_WHITELIST,
7
- } from "./consts";
8
- import { getQuote } from "./quote";
9
-
10
- export {
11
- WrapTokenAddressMap,
12
- DEX_ENUM,
13
- DEX_SUPPORT_CHAINS,
14
- getQuote,
15
- DEX_ROUTER_WHITELIST,
16
- DEX_SPENDER_WHITELIST,
17
- };
package/src/quote.ts DELETED
@@ -1,85 +0,0 @@
1
- import { CHAINS_ENUM } from '@debank/common';
2
- import { DEX_ENUM } from './consts';
3
- import { getQuote as wrapTokenGetQuote } from './dexs/wrapToken';
4
- import { getQuote as oneInchGetQuote, decodeCalldata as oneInchDecodeCalldata } from './dexs/1inch';
5
- import { getQuote as zeroXGetQuote, decodeCalldata as zeroXDecodeCalldata } from './dexs/0xapi';
6
- import { getQuote as paraSwapGetQuote, decodeCalldata as paraSwapDecodeCalldata } from './dexs/paraswap';
7
- import { getQuote as openOceanGetQuote, decodeCalldata as openOceanDecodeCalldata } from './dexs/openocean';
8
-
9
- export interface QuoteParams {
10
- fromToken: string;
11
- fromTokenDecimals: number;
12
- toToken: string;
13
- amount: string;
14
- userAddress: string;
15
- slippage: number;
16
- feeRate?: number;
17
- feeAddress?: string;
18
- gasPrice?: number;
19
- chain: CHAINS_ENUM;
20
- }
21
-
22
- export interface Tx {
23
- from: string;
24
- to: string;
25
- data: string;
26
- value: string;
27
- chainId?: number;
28
- gas?: number;
29
- gasPrice?: string;
30
- }
31
-
32
- export interface TxWithChainId extends Tx {
33
- chainId: number;
34
- }
35
-
36
- export interface QuoteResult {
37
- tx: Tx;
38
- fromToken: string;
39
- fromTokenDecimals?: number;
40
- fromTokenAmount: string;
41
- toToken: string;
42
- toTokenDecimals?: number;
43
- toTokenAmount: string;
44
- spender: string;
45
- }
46
-
47
- export const getQuote = async (id: DEX_ENUM, params: QuoteParams) => {
48
- switch(id) {
49
- case DEX_ENUM.WRAPTOKEN:
50
- return await wrapTokenGetQuote(params);
51
- // case DEX_ENUM.ONEINCH:
52
- // return await oneInchGetQuote(params);
53
- case DEX_ENUM.PARASWAP:
54
- return await paraSwapGetQuote(params);
55
- case DEX_ENUM.ZEROXAPI:
56
- return await zeroXGetQuote(params);
57
- case DEX_ENUM.OPENOCEAN:
58
- return await openOceanGetQuote(params);
59
- default:
60
- throw new Error(`${id} is not supported!`);
61
- }
62
- }
63
-
64
- export interface DecodeCalldataResult {
65
- fromToken: string;
66
- fromTokenAmount: string;
67
- toToken: string;
68
- minReceiveToTokenAmount: string;
69
- toTokenReceiver: string;
70
- }
71
-
72
- export const decodeCalldata = (id: DEX_ENUM, tx: TxWithChainId): DecodeCalldataResult | null => {
73
- switch(id) {
74
- // case DEX_ENUM.ONEINCH:
75
- // return oneInchDecodeCalldata(tx);
76
- case DEX_ENUM.PARASWAP:
77
- return paraSwapDecodeCalldata(tx);
78
- case DEX_ENUM.ZEROXAPI:
79
- return zeroXDecodeCalldata(tx);
80
- case DEX_ENUM.OPENOCEAN:
81
- return openOceanDecodeCalldata(tx);
82
- default:
83
- throw new Error(`${id} is not supported!`);
84
- }
85
- };
package/src/utils.ts DELETED
@@ -1,4 +0,0 @@
1
- export const isSameAddress = (addr1: string, addr2: string) => {
2
- if (typeof addr1 !== 'string' || typeof addr2 !== 'string') return false;
3
- return addr1.toLowerCase() === addr2.toLowerCase();
4
- }
package/svgTransform.js DELETED
@@ -1,11 +0,0 @@
1
- module.exports = {
2
- process() {
3
- return {
4
- code: 'module.exports = {};'
5
- };
6
- },
7
- getCacheKey() {
8
- // The output is always the same.
9
- return 'svgTransform';
10
- }
11
- };
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2016",
4
- "moduleResolution": "node",
5
- "esModuleInterop": true,
6
- "forceConsistentCasingInFileNames": true,
7
- "strict": true,
8
- "noImplicitAny": true,
9
- "skipLibCheck": true,
10
- "outDir": "dist",
11
- "declaration": true
12
- },
13
- "include": ["src"]
14
- }