Skip to content

Commit a1547a1

Browse files
added api key when available
1 parent 4c27609 commit a1547a1

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

utils/fetchCoinGeckoPrices.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
const axios = require('axios');
1+
const axios = require("axios");
22

33
async function fetchCoinGeckoPrices(client, tokens, network) {
4-
const stringifiedTokens = tokens.join(',');
4+
const stringifiedTokens = tokens.join(",");
55

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

10-
for (const [key, value] of Object.entries(data)) {
11-
client.setex(key, 600, value['usd']);
12-
}
13-
return data;
14-
} catch (error) {
15-
console.error(`Failed loading data for ${tokens}: ${error}`);
16-
return {};
17-
}
13+
for (const [key, value] of Object.entries(data)) {
14+
client.setex(key, 600, value["usd"]);
15+
}
16+
return data;
17+
} catch (error) {
18+
console.error(`Failed loading data for ${tokens}: ${error}`);
19+
return {};
20+
}
1821
}
1922

20-
module.exports = fetchCoinGeckoPrices;
23+
module.exports = fetchCoinGeckoPrices;

0 commit comments

Comments
 (0)