|
16 | 16 |
|
17 | 17 | 'use strict';
|
18 | 18 |
|
19 |
| -const pageResults = require('graph-results-pager'); |
20 |
| - |
21 | 19 | import axios from 'axios';
|
22 | 20 | import Assertions from '../../assertions';
|
23 | 21 |
|
24 | 22 | import {
|
25 | 23 | CoinGeckoCoinPrices,
|
26 | 24 | CoinGeckoTokenData,
|
27 |
| - SushiswapTokenData, |
28 | 25 | CoinGeckoTokenMap,
|
29 | 26 | CoinPricesParams,
|
30 |
| - PolygonMappedTokenData |
31 | 27 | } from '../../types';
|
32 | 28 |
|
33 | 29 | /**
|
@@ -95,14 +91,8 @@ export class CoinGeckoDataService {
|
95 | 91 | async fetchTokenList(): Promise<CoinGeckoTokenData[]> {
|
96 | 92 | if (this.tokenList !== undefined) return this.tokenList;
|
97 | 93 |
|
98 |
| - switch (this.chainId) { |
99 |
| - case 1: |
100 |
| - this.tokenList = await this.fetchEthereumTokenList(); |
101 |
| - break; |
102 |
| - case 137: |
103 |
| - this.tokenList = await this.fetchPolygonTokenList(); |
104 |
| - break; |
105 |
| - } |
| 94 | + const url = this.getCoingeckoUrl(); |
| 95 | + this.tokenList = await this.fetchCoingeckoTokenList(url); |
106 | 96 | this.tokenMap = this.convertTokenListToAddressMap(this.tokenList);
|
107 | 97 |
|
108 | 98 | return this.tokenList!;
|
@@ -135,107 +125,22 @@ export class CoinGeckoDataService {
|
135 | 125 | private getPlatform(): string {
|
136 | 126 | switch (this.chainId) {
|
137 | 127 | case 1: return 'ethereum';
|
| 128 | + case 10: return 'optimistic-ethereum'; |
138 | 129 | case 137: return 'polygon-pos';
|
139 | 130 | default: return '';
|
140 | 131 | }
|
141 | 132 | }
|
142 | 133 |
|
143 |
| - private async fetchEthereumTokenList(): Promise<CoinGeckoTokenData[]> { |
144 |
| - const url = 'https://tokens.coingecko.com/uniswap/all.json'; |
145 |
| - const response = await axios.get(url); |
146 |
| - return response.data.tokens; |
147 |
| - } |
148 |
| - |
149 |
| - private async fetchPolygonTokenList(): Promise<CoinGeckoTokenData[]> { |
150 |
| - const coingeckoEthereumTokens = await this.fetchEthereumTokenList(); |
151 |
| - const polygonMappedTokens = await this.fetchPolygonMappedTokenList(); |
152 |
| - const sushiPolygonTokenList = await this.fetchSushiPolygonTokenList(); |
153 |
| - const quickswapPolygonTokenList = await this.fetchQuickswapPolygonTokenList(); |
154 |
| - |
155 |
| - for (const token of sushiPolygonTokenList) { |
156 |
| - const quickswapToken = quickswapPolygonTokenList.find(t => t.address.toLowerCase() === token.address); |
157 |
| - |
158 |
| - if (quickswapToken) { |
159 |
| - token.logoURI = quickswapToken.logoURI; |
160 |
| - continue; |
161 |
| - } |
162 |
| - |
163 |
| - const ethereumAddress = polygonMappedTokens[token.address]; |
164 |
| - |
165 |
| - if (ethereumAddress !== undefined) { |
166 |
| - const ethereumToken = coingeckoEthereumTokens.find(t => t.address.toLowerCase() === ethereumAddress); |
167 |
| - |
168 |
| - if (ethereumToken) { |
169 |
| - token.logoURI = ethereumToken.logoURI; |
170 |
| - } |
171 |
| - } |
172 |
| - } |
173 |
| - |
174 |
| - return sushiPolygonTokenList; |
175 |
| - } |
176 |
| - |
177 |
| - private async fetchSushiPolygonTokenList() { |
178 |
| - let tokens: SushiswapTokenData[] = []; |
179 |
| - const url = 'https://api.thegraph.com/subgraphs/name/sushiswap/matic-exchange'; |
180 |
| - const properties = [ |
181 |
| - 'id', |
182 |
| - 'symbol', |
183 |
| - 'name', |
184 |
| - 'decimals', |
185 |
| - 'volumeUSD', |
186 |
| - ]; |
187 |
| - |
188 |
| - const response = await pageResults({ |
189 |
| - api: url, |
190 |
| - query: { |
191 |
| - entity: 'tokens', |
192 |
| - properties: properties, |
193 |
| - }, |
194 |
| - }); |
195 |
| - |
196 |
| - for (const token of response) { |
197 |
| - tokens.push({ |
198 |
| - chainId: 137, |
199 |
| - address: token.id, |
200 |
| - symbol: token.symbol, |
201 |
| - name: token.name, |
202 |
| - decimals: parseInt(token.decimals), |
203 |
| - volumeUSD: parseFloat(token.volumeUSD), |
204 |
| - }); |
205 |
| - } |
206 |
| - |
207 |
| - // Sort by volume and filter out untraded tokens |
208 |
| - tokens.sort((a, b) => b.volumeUSD - a.volumeUSD); |
209 |
| - tokens = tokens.filter(t => t.volumeUSD > 0); |
210 |
| - |
211 |
| - return tokens; |
212 |
| - } |
213 |
| - |
214 |
| - private async fetchPolygonMappedTokenList() { |
215 |
| - const tokens: PolygonMappedTokenData = {}; |
216 |
| - const url = 'https://api.thegraph.com/subgraphs/name/maticnetwork/mainnet-root-subgraphs'; |
217 |
| - const properties = ['id', 'rootToken', 'childToken']; |
218 |
| - |
219 |
| - const response = await pageResults({ |
220 |
| - api: url, |
221 |
| - query: { |
222 |
| - entity: 'tokenMappings', |
223 |
| - properties: properties, |
224 |
| - }, |
225 |
| - }); |
226 |
| - |
227 |
| - for (const tokenMapping of response) { |
228 |
| - tokens[tokenMapping.childToken.toLowerCase()] = tokenMapping.rootToken.toLowerCase(); |
| 134 | + private getCoingeckoUrl(): string { |
| 135 | + switch (this.chainId) { |
| 136 | + case 1: return 'https://tokens.coingecko.com/uniswap/all.json'; |
| 137 | + case 10: return 'https://tokens.coingecko.com/optimistic-ethereum/all.json'; |
| 138 | + case 137: return 'https://tokens.coingecko.com/polygon-pos/all.json'; |
229 | 139 | }
|
230 |
| - |
231 |
| - return tokens; |
232 | 140 | }
|
233 | 141 |
|
234 |
| - private async fetchQuickswapPolygonTokenList(): Promise<CoinGeckoTokenData[]> { |
235 |
| - const url = 'https://raw.githubusercontent.com/sameepsi/' + |
236 |
| - 'quickswap-default-token-list/master/src/tokens/mainnet.json'; |
237 |
| - |
238 |
| - const data = (await axios.get(url)).data; |
239 |
| - return data; |
| 142 | + private async fetchCoingeckoTokenList(url: string): Promise<CoinGeckoTokenData[]> { |
| 143 | + const response = await axios.get(url); |
| 144 | + return response.data.tokens; |
240 | 145 | }
|
241 | 146 | }
|
0 commit comments