Skip to content

Commit

Permalink
Refactoring messy method
Browse files Browse the repository at this point in the history
  • Loading branch information
arboleya committed Jan 2, 2025
1 parent ca6f05f commit cde60c3
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,43 +605,36 @@ export default class Provider {
* @returns A promise that resolves to the Chain and NodeInfo.
*/
async fetchChainAndNodeInfo(ignoreCache: boolean = false) {
let nodeInfo: NodeInfo;
let chain: ChainInfo;
let nodeInfo = Provider.nodeInfoCache[this.urlWithoutAuth];
let chain = Provider.chainInfoCache[this.urlWithoutAuth];

try {
nodeInfo = Provider.nodeInfoCache[this.urlWithoutAuth];
chain = Provider.chainInfoCache[this.urlWithoutAuth];
// if we have cache, return it
const cashIsPresent = !nodeInfo || !chain;
if (!ignoreCache && cashIsPresent) {
return { nodeInfo, chain };
}

const noCache = !nodeInfo || !chain;
// otherwise, fetch data, save to cache, and return it
const data = await this.operations.getChainAndNodeInfo();

if (ignoreCache || noCache) {
throw new Error(`Jumps to the catch block and re-fetch`);
}
} catch (_err) {
const data = await this.operations.getChainAndNodeInfo();

nodeInfo = {
maxDepth: bn(data.nodeInfo.maxDepth),
maxTx: bn(data.nodeInfo.maxTx),
nodeVersion: data.nodeInfo.nodeVersion,
utxoValidation: data.nodeInfo.utxoValidation,
vmBacktrace: data.nodeInfo.vmBacktrace,
};
nodeInfo = {
maxDepth: bn(data.nodeInfo.maxDepth),
maxTx: bn(data.nodeInfo.maxTx),
nodeVersion: data.nodeInfo.nodeVersion,
utxoValidation: data.nodeInfo.utxoValidation,
vmBacktrace: data.nodeInfo.vmBacktrace,
};

Provider.ensureClientVersionIsSupported(nodeInfo);
chain = processGqlChain(data.chain);

chain = processGqlChain(data.chain);
Provider.chainInfoCache[this.urlWithoutAuth] = chain;
Provider.nodeInfoCache[this.urlWithoutAuth] = nodeInfo;

Provider.chainInfoCache[this.urlWithoutAuth] = chain;
Provider.nodeInfoCache[this.urlWithoutAuth] = nodeInfo;
this.consensusParametersTimestamp = Date.now();

this.consensusParametersTimestamp = Date.now();
}
Provider.ensureClientVersionIsSupported(nodeInfo);

return {
chain,
nodeInfo,
};
return { chain, nodeInfo };
}

/**
Expand Down

0 comments on commit cde60c3

Please sign in to comment.