Skip to content

Commit

Permalink
Merge pull request #634 from eco-stake/rest-api-fallback-check
Browse files Browse the repository at this point in the history
Add fallback block path for REST check
  • Loading branch information
tombeynon authored Oct 6, 2022
2 parents 92bcd57 + 1895594 commit f8badc8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/utils/QueryClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,10 @@ const QueryClient = async (chainId, restUrls, opts) => {
}
}
const path = type === "rest" ? "/cosmos/base/tendermint/v1beta1/blocks/latest" : "/block";
const { timeout } = opts || {}
return Promise.any(urls.map(async (url) => {
url = url.replace(/\/$/, '')
try {
let data = await axios.get(url + path, { timeout })
.then((res) => res.data)
let data = await getLatestBlock(url, type, path, opts)
if (type === "rpc") data = data.result;
if (data.block?.header?.chain_id === chainId) {
return url;
Expand All @@ -237,6 +235,20 @@ const QueryClient = async (chainId, restUrls, opts) => {
}));
}

async function getLatestBlock(url, type, path, opts){
const { timeout } = opts || {}
try {
return await axios.get(url + path, { timeout })
.then((res) => res.data)
} catch (error) {
const fallback = type === 'rest' && '/blocks/latest'
if (fallback && fallback !== path && error.response?.status === 501) {
return getLatestBlock(url, type, fallback, opts)
}
throw(error)
}
}

return {
connected: !!restUrl,
restUrl,
Expand Down

0 comments on commit f8badc8

Please sign in to comment.