Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor - getContract for evm #621

Merged
merged 3 commits into from
Oct 16, 2023

Conversation

Viterbo
Copy link
Collaborator

@Viterbo Viterbo commented Sep 28, 2023

Fixes #598

Description

This PR includes a refactoring solution for the problem described in #598. Now we have only one function getContract in the Contract Store which would use the Indexer or the Hyperion to retrieve the contract's metadata to create the contract instance and finally cache the metadata in the localStorage to avoid future fetchings for the same immutable data.

Technical detail

The new structure of variables now takes into account the different possible networks to store the contracts separately.

export interface ContractStoreState {
__factory: EvmContractFactory;
__contracts: {
[network: string]: {
metadata: Record<string, EvmContractFactoryData | null>
cached: Record<string, EvmContract | null>
processing: Record<string, Promise<EvmContract | null>>
},
}
}

The saveCache and loadCache functions perform the job of remembering locally all metadata to quickly recreate contract instances and speed up future loadings.

/**
* This function takes all contract's metadata and stores it in the localStorage
*/
saveCache() {
this.trace('saveCache');
const cacheData: Record<string, Record<string, EvmContractFactoryData | null>> = {};
for (const network in this.__contracts) {

/**
* This function loads all contract's metadata from the localStorage.
*/
loadCache() {
this.trace('loadCache');
const cacheString = localStorage.getItem(LOCAL_SORAGE_CONTRACTS_KEY);
if (cacheString) {

Finally, the getContract function now is divided into three parts:
1 - It asserts the structure exists for the network

// we assert the network structure existence
if (!this.__contracts[network]) {
this.__contracts[network] = {
metadata: {},
cached: {},
processing: {},
};
}

2 - then tries to retrieve data from the cache (to skip fetchings)

// if we have it in cache, return it
if (typeof this.__contracts[network].cached[addressLower] !== 'undefined') {
this.trace('getContract', 'returning cached contract', address, [this.__contracts[network].cached[addressLower]]);
return this.__contracts[network].cached[addressLower];
}
// if we have the metadata, we can create the contract and return it
if (typeof this.__contracts[network].metadata[addressLower] !== 'undefined') {
const metadata = this.__contracts[network].metadata[addressLower] as EvmContractFactoryData;
this.trace('getContract', 'returning cached metadata', address, [metadata]);
return this.createAndStoreContract(label, addressLower, metadata);
}
// maybe we already starting processing it, return the promise
if (typeof this.__contracts[network].processing[addressLower] !== 'undefined') {
this.trace('getContract', 'returning processing contract', address);
return this.__contracts[network].processing[addressLower];
}

3 - It takes into account the health of the Indexer before using it and falls back into a Hyperion-based solution if the first one fails.

// we don't have it, let's get it
if (chainSettings.isIndexerHealthy()) {
try {
// we have a healthy indexer, let's get it from there first
return await this.fetchContractUsingIndexer(label, address, suspectedToken);
} catch (e) {
console.warn('Indexer did not worked, falling back to hyperion');
return await this.fetchContractUsingHyperion(label, address, suspectedToken);
}
} else {
// we don't have a healthy indexer, let's get it from hyperion
return await this.fetchContractUsingHyperion(label, address, suspectedToken);
}

Test scenarios

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have cleaned up the code in the areas my change touches
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings
  • I have removed any unnecessary console messages
  • I have included all english text to the translation file and/or created a new issue with the required translations for the currently supported languages
  • I have tested for mobile functionality and responsiveness

@Viterbo Viterbo linked an issue Sep 28, 2023 that may be closed by this pull request
@netlify
Copy link

netlify bot commented Sep 28, 2023

Deploy Preview for wallet-develop-mainnet ready!

Name Link
🔨 Latest commit 4eeb2db
🔍 Latest deploy log https://app.netlify.com/sites/wallet-develop-mainnet/deploys/65243b6ec6e2ab0008166225
😎 Deploy Preview https://deploy-preview-621--wallet-develop-mainnet.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@netlify
Copy link

netlify bot commented Sep 28, 2023

Deploy Preview for wallet-staging ready!

Name Link
🔨 Latest commit 4eeb2db
🔍 Latest deploy log https://app.netlify.com/sites/wallet-staging/deploys/65243b6e773893000800e2ee
😎 Deploy Preview https://deploy-preview-621--wallet-staging.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@Viterbo Viterbo self-assigned this Sep 28, 2023
@ezra-sg ezra-sg changed the base branch from develop to master October 9, 2023 17:01
@ezra-sg ezra-sg changed the base branch from master to develop October 9, 2023 17:01
Copy link
Contributor

@ezra-sg ezra-sg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice work!

@donnyquixotic donnyquixotic merged commit 3112e86 into develop Oct 16, 2023
9 checks passed
@donnyquixotic donnyquixotic deleted the 598-refactor-getcontract-for-evm branch October 16, 2023 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor - getContract for EVM
3 participants