-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
33 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,6 @@ on: | |
branches: | ||
- master | ||
- dev | ||
push: | ||
branches: | ||
- dev | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,50 @@ | ||
import axios from "axios"; | ||
|
||
import Logger from "src/utils/logger-handler"; | ||
|
||
|
||
const { | ||
NEXT_PUBLIC_CURRENCY_MAIN: currency, | ||
NEXT_ENABLE_COINGECKO: enableCoinGecko, | ||
} = process.env; | ||
NEXT_PUBLIC_CURRENCY_MAIN: currency, | ||
NEXT_ENABLE_COINGECKO: enableCoinGecko, | ||
} = process.env; | ||
|
||
const COINGECKO_API = axios.create({baseURL: "https://api.coingecko.com/api/v3"}); | ||
|
||
async function getCoinPrice(search: string, fiat = currency) { | ||
if (!enableCoinGecko) | ||
return 0; | ||
|
||
const coins = await COINGECKO_API.get(`/coins/list?include_platform=false`).then(value => value.data); | ||
const symbols = search.toLowerCase().split(',') | ||
const coinsData = coins.filter(({symbol}) => symbols.includes(symbol)) | ||
if (!enableCoinGecko) { | ||
Logger.warn("enableCoinGecko env is disabled") | ||
return 0; | ||
} | ||
|
||
const coins = await COINGECKO_API.get(`/coins/list?include_platform=false`).then(value => value.data); | ||
|
||
if (coinsData.length < 1) | ||
return 0; | ||
|
||
const ids = coinsData.map(({id}) => id).join() | ||
if (!Array.isArray(coins)) | ||
Logger.warn(coins, "Error to get list coingecko") | ||
|
||
const price = await COINGECKO_API.get(`/simple/price?ids=${ids}&vs_currencies=${fiat || 'eur'}`); | ||
const symbols = search.toLowerCase().split(',') | ||
const coinsData = coins.filter(({symbol}) => symbols.includes(symbol)) | ||
|
||
if (!price?.data) | ||
return 0; | ||
if (coinsData.length < 1) { | ||
Logger.warn(coinsData, "Error to filter symbol coingecko") | ||
return 0; | ||
} | ||
|
||
const result: any = {} | ||
const ids = coinsData.map(({id}) => id).join() | ||
|
||
coinsData.map(({symbol, id}) => result[symbol] = {[fiat || 'eur']: price?.data?.[id]?.[fiat || 'eur']}) | ||
const price = await COINGECKO_API.get(`/simple/price?ids=${ids}&vs_currencies=${fiat || 'eur'}`); | ||
|
||
return result | ||
if (!price?.data) { | ||
Logger.log(price.statusText, "Error to get prices coingecko") | ||
return 0; | ||
} | ||
|
||
const result: any = {} | ||
|
||
coinsData.map(({symbol, id}) => result[symbol] = {[fiat || 'eur']: price?.data?.[id]?.[fiat || 'eur']}) | ||
|
||
return result | ||
} | ||
|
||
export { | ||
getCoinPrice | ||
getCoinPrice | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters