From 1895594d4810aed2ae37a3432abe7bca510211c1 Mon Sep 17 00:00:00 2001 From: Tom Beynon Date: Thu, 6 Oct 2022 22:38:43 +0100 Subject: [PATCH] Add fallback block path for REST check --- src/utils/QueryClient.mjs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/utils/QueryClient.mjs b/src/utils/QueryClient.mjs index 8272fe20..3101fc97 100644 --- a/src/utils/QueryClient.mjs +++ b/src/utils/QueryClient.mjs @@ -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; @@ -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,