Skip to content

Commit

Permalink
Add fallback block path for REST check
Browse files Browse the repository at this point in the history
  • Loading branch information
tombeynon committed Oct 6, 2022
1 parent 2b68593 commit 1895594
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 1895594

Please sign in to comment.