Skip to content

Commit

Permalink
fix logger calls on coingecko.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
moshmage committed Feb 16, 2023
1 parent 5260938 commit 880427c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 44 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on:
branches:
- master
- dev
push:
branches:
- dev
jobs:
test:
runs-on: ubuntu-latest
Expand Down
53 changes: 33 additions & 20 deletions src/services/coingecko.ts
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
}
21 changes: 0 additions & 21 deletions src/utils/logger-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,4 @@ export const warn = (message: string, ...rest) => Scribal.w(message, ...rest);
export const debug = (message: string, ...rest) => Scribal.d(message, ...rest);
export const trace = (message: string, ...rest) => Scribal.d(message, ...rest);

export class Logger {
constructor(readonly name: string) {
}

info(message, ...contents) {
Scribal.i(message, ...contents, {name: this.name});
}

warn(message, ...contents) {
Scribal.w(message, ...contents, {name: this.name});
}

error(message, ...contents) {
Scribal.e(message, ...contents, {name: this.name});
}

debug(message, ...contents) {
Scribal.d(message, ...contents, {name: this.name});
}
}

export default {error, info, warn, log, debug, trace}

0 comments on commit 880427c

Please sign in to comment.