Skip to content

Commit

Permalink
module: added exponential backoff on axios
Browse files Browse the repository at this point in the history
- "exponential backoff" is a strategy for handling transient failures
in distributed systems with retries "5" times and retry delay adjustment.

- if string coingecko is empty then value is "null" and makes
the public limitation on api the looping exceptions.

Signed-off-by: Salman Wahib <sxlmnwb.dev@gmail.com>
  • Loading branch information
sxlmnwb committed Aug 18, 2023
1 parent e6485d7 commit 7565ba9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "sxlmnwb",
"dependencies": {
"axios": "^1.4.0",
"axios-retry": "^3.6.0",
"chalk": "^4.1.2",
"dotenv": "^16.3.1",
"is-root": "^3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/libs/getApi.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const axios = require('axios');
const axiosRetry = require('axios-retry');

axiosRetry(axios, { retries: 5, retryDelay: axiosRetry.exponentialDelay });

module.exports = async function getApiData({ apiUrl, coingecko, valoper, valcons }) {
const summaryApi = await axios.get(`${apiUrl}/cosmos/base/tendermint/v1beta1/blocks/latest`);

let coingeckoApi;
try {
// https://api.coingecko.com/api/v3/coins/${coingecko}
coingeckoApi = await axios.get(`https://api.coingecko.com/api/v3/simple/price?ids=${coingecko}&vs_currencies=usd`);
} catch (error) {
coingeckoApi = null;
if (error.response && error.response.status === 429) {
} else {
throw error;
Expand Down
15 changes: 10 additions & 5 deletions src/libs/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ module.exports = async function useApi({ apiUrl, valoper, valcons, denom, expone
const secondsAgo = Math.round((fetchedAt - latestBlockTime) / 1000);

let tokenPrice = ' -';
if (coingecko !== "") {
tokenPrice = apiData.coingeckoApi.data[coingecko].usd;
if (tokenPrice >= 1) {
tokenPrice = tokenPrice.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2});

if (apiData.coingeckoApi) {
if (apiData.coingeckoApi.data[coingecko]) {
if (coingecko !== null) {
tokenPrice = apiData.coingeckoApi.data[coingecko].usd;
if (tokenPrice >= 1) {
tokenPrice = tokenPrice.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2});
}
}
}
}
}

// summary

Expand Down

0 comments on commit 7565ba9

Please sign in to comment.