From 29696f3507c7348875a858f96047dba163cdb4fb Mon Sep 17 00:00:00 2001 From: Timur Badretdinov Date: Tue, 7 May 2024 16:59:59 +0100 Subject: [PATCH] Optimise: use cached data before any fetching --- pages/contract/[address].vue | 49 +++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/pages/contract/[address].vue b/pages/contract/[address].vue index d63ab1e..c0ce802 100644 --- a/pages/contract/[address].vue +++ b/pages/contract/[address].vue @@ -91,7 +91,38 @@ async function getCodeHash(chain: Chain): Promise { } async function fetchCode(): Promise { + function processCodeHash( + chain: Chain, + codeHash: Hex | null | undefined, + ): void { + if (!referenceCodeHash && codeHash) { + referenceCodeHash = codeHash; + } + const status = codeHash + ? referenceCodeHash === codeHash + ? 'success' + : 'warning' + : codeHash === null + ? 'empty' + : 'error'; + const chainIndex = chains.value.findIndex( + (chainStatus) => chainStatus.id === chain, + ); + const chainValue = chains.value[chainIndex]; + if (chainValue) { + chainValue.status = status; + } + } let referenceCodeHash: Hex | null = null; + // Get cached code first + const cachedCodeHashes = ( + cache as Record>> + )[address.value]; + for (const chainKey in cachedCodeHashes) { + const chain = parseInt(chainKey) as Chain; + const codeHash = cachedCodeHashes[chain]; + processCodeHash(chain, codeHash); + } // Split chains into batches to query contract code in parallel const batchSize = 10; const batchedChains: Chain[][] = []; @@ -105,23 +136,7 @@ async function fetchCode(): Promise { await Promise.all( batch.map(async (chain) => { const codeHash = await getCodeHash(chain); - if (!referenceCodeHash && codeHash) { - referenceCodeHash = codeHash; - } - const status = codeHash - ? referenceCodeHash === codeHash - ? 'success' - : 'warning' - : codeHash === null - ? 'empty' - : 'error'; - const chainIndex = chains.value.findIndex( - (chainStatus) => chainStatus.id === chain, - ); - const chainValue = chains.value[chainIndex]; - if (chainValue) { - chainValue.status = status; - } + processCodeHash(chain, codeHash); }), ); }