-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (106 loc) · 2.95 KB
/
index.js
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
const config = require("./constants/index");
const helper = require("./utils/helper");
const quickswapSdk = require("quickswap-default-token-list");
const coins = require("./constants/coins");
async function getSupportedTokens(chain, dex) {
if (!config.supportedChains.includes(chain)) {
return { error: config.errorMessages.INVALID_CHAIN };
}
if (dex === "oneInch") {
const { tokens } = await getTokensOneInch(chain);
return { tokens };
}
if (dex === "uniswap") {
const { tokens } = await getTokensUniswap(chain);
return { tokens };
}
if (dex === "quickswap") {
const { tokens } = await getTokensQuickswap(chain);
return { tokens };
}
if (dex === "pancakeswap") {
const { tokens } = await getTokensPancakeswap(chain);
return { tokens };
}
}
async function getTokensOneInch(chain) {
if (chain === "ethereum") {
url = `${config.oneInchBaseURL}/1/tokens`;
} else if (chain === "polygon") {
url = `${config.oneInchBaseURL}/137/tokens`;
} else if (chain === "bsc") {
url = `${config.oneInchBaseURL}/56/tokens`;
} else {
return { error: config.errorMessages.INVALID_CHAIN };
}
const { response, error } = await helper.getRequest({ url, token: config.oneinchAuthToken });
if (error)
return error
let tokens = Object.values(response.tokens);
tokens.forEach(function (obj) {
delete obj.tags;
});
return { tokens };
}
async function getTokensPancakeswap(chain) {
if (chain === "bsc") {
url = `${config.pancakeBaseURL}`;
} else {
return { error: config.errorMessages.INVALID_CHAIN };
}
const { response, error } = await helper.getRequest({ url });
if (error) {
return error;
}
let tokens = response.tokens;
tokens.forEach(function (obj) {
delete obj.chainId;
});
tokens.push(coins[chain]);
return { tokens };
}
async function getTokensQuickswap(chain) {
if (chain !== "polygon") {
return { error: config.errorMessages.INVALID_CHAIN };
}
let tokens = quickswapSdk.tokens;
tokens.forEach(function (obj) {
delete obj.chainId;
});
tokens.push(coins[chain]);
return { tokens };
}
async function getTokensUniswap(chain) {
if (chain === "ethereum" || chain === "polygon") {
url = `${config.uniswapBaseURL}`;
} else {
return { error: config.errorMessages.INVALID_CHAIN };
}
const { response, error } = await helper.getRequest({ url });
if (error) {
return error;
}
let tokens = response.tokens;
if (chain === "polygon") {
tokens = tokens.filter((obj) => obj.chainId === 137);
tokens.forEach(function (obj) {
delete obj.chainId;
delete obj.extensions;
});
} else {
tokens = tokens.filter((obj) => obj.chainId === 1);
tokens.forEach(function (obj) {
delete obj.chainId;
delete obj.extensions;
});
}
tokens.push(coins[chain]);
return { tokens };
}
module.exports = {
getSupportedTokens,
getTokensOneInch,
getTokensPancakeswap,
getTokensQuickswap,
getTokensUniswap,
};