Skip to content

Commit

Permalink
added api key when available
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher-Stevers committed Mar 4, 2023
1 parent 4c27609 commit a1547a1
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions utils/fetchCoinGeckoPrices.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const axios = require('axios');
const axios = require("axios");

async function fetchCoinGeckoPrices(client, tokens, network) {
const stringifiedTokens = tokens.join(',');
const stringifiedTokens = tokens.join(",");

const url = `https://api.coingecko.com/api/v3/simple/token_price/${network}?contract_addresses=${stringifiedTokens}&vs_currencies=usd`;
try {
const { data } = await axios.get(url);
let url = `https://api.coingecko.com/api/v3/simple/token_price/${network}?contract_addresses=${stringifiedTokens}&vs_currencies=usd`;
if (process.env.COINGECK_API_KEY) {
url = `${url}&x_cg_pro_api_key=${process.env.COINGECK_API_KEY}`;
}
try {
const { data } = await axios.get(url);

for (const [key, value] of Object.entries(data)) {
client.setex(key, 600, value['usd']);
}
return data;
} catch (error) {
console.error(`Failed loading data for ${tokens}: ${error}`);
return {};
}
for (const [key, value] of Object.entries(data)) {
client.setex(key, 600, value["usd"]);
}
return data;
} catch (error) {
console.error(`Failed loading data for ${tokens}: ${error}`);
return {};
}
}

module.exports = fetchCoinGeckoPrices;
module.exports = fetchCoinGeckoPrices;

0 comments on commit a1547a1

Please sign in to comment.