Skip to content

Commit

Permalink
Optimise: use cached data before any fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
Destiner committed May 7, 2024
1 parent 4076b02 commit 29696f3
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions pages/contract/[address].vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,38 @@ async function getCodeHash(chain: Chain): Promise<Hex | null | undefined> {
}
async function fetchCode(): Promise<void> {
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, Partial<Record<Chain, Hex>>>
)[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[][] = [];
Expand All @@ -105,23 +136,7 @@ async function fetchCode(): Promise<void> {
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);
}),
);
}
Expand Down

0 comments on commit 29696f3

Please sign in to comment.