Skip to content

Commit

Permalink
DRYer
Browse files Browse the repository at this point in the history
  • Loading branch information
mhagel committed Feb 15, 2024
1 parent 23c3ed1 commit fb4f874
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions packages/commonwealth/server/util/cosmosProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function setupCosmosProxy(
cosmos_chain_id,
chainNodeUrl,
);
return queryRPCProxy(req, chain);
return queryExternalProxy(req, cosmos_chain_id, 'rpc');
});

await flagHealthyNode(
Expand All @@ -84,7 +84,11 @@ function setupCosmosProxy(
chainNodeUrl,
);
} else {
response = await queryRPCProxy(req, chain.ChainNode.cosmos_chain_id);
response = await queryExternalProxy(
req,
chain.ChainNode.cosmos_chain_id,
'rpc',
);
}

log.trace(
Expand Down Expand Up @@ -167,7 +171,7 @@ function setupCosmosProxy(
cosmos_chain_id,
chainNodeRestUrl,
);
return queryRESTProxy(req, cosmos_chain_id);
return queryExternalProxy(req, cosmos_chain_id, 'rest');
});

await flagHealthyNode(
Expand All @@ -177,7 +181,7 @@ function setupCosmosProxy(
chainNodeRestUrl,
);
} else {
response = await queryRESTProxy(req, cosmos_chain_id);
response = await queryExternalProxy(req, cosmos_chain_id, 'rest');
}

log.trace(`Got response: ${JSON.stringify(response.data, null, 2)}`);
Expand All @@ -195,23 +199,12 @@ function setupCosmosProxy(
},
);

const queryRESTProxy = async (req, cosmos_chain_id) => {
const proxyUrl = `https://rest.cosmos.directory/${cosmos_chain_id}`;
const rewrite = req.originalUrl.replace(req.baseUrl, proxyUrl);
const body = _.isEmpty(req.body) ? null : req.body;
log.trace(`Querying proxy: ${proxyUrl}`);

const response = await axios.post(rewrite, body, {
headers: {
origin: 'https://commonwealth.im',
Referer: process.env.COSMOS_PROXY_REFERER || 'https://commonwealth.im',
},
});
return response;
};

const queryRPCProxy = async (req, cosmos_chain_id) => {
const proxyUrl = `https://rpc.cosmos.directory/${cosmos_chain_id}`;
const queryExternalProxy = async (
req,
cosmos_chain_id: string,
web_protocol: 'rpc' | 'rest',
) => {
const proxyUrl = `https://${web_protocol}.cosmos.directory/${cosmos_chain_id}`;
const rewrite = req.originalUrl.replace(req.originalUrl, proxyUrl);
const body = _.isEmpty(req.body) ? null : req.body;
log.trace(`Querying proxy: ${proxyUrl}`);
Expand Down

0 comments on commit fb4f874

Please sign in to comment.