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

Fetch & cache consensus parameters when Provider is initialized #1176

Closed
Dhaiwat10 opened this issue Aug 8, 2023 · 1 comment · Fixed by #1181
Closed

Fetch & cache consensus parameters when Provider is initialized #1176

Dhaiwat10 opened this issue Aug 8, 2023 · 1 comment · Fixed by #1181
Assignees
Labels
feat Issue is a feature

Comments

@Dhaiwat10
Copy link
Member

This API change/feature attempts to solve #1050

Similar to the Rust SDK's Provider::connect:

/// Connects to an existing node at the given address.
    pub async fn connect(url: impl AsRef<str>) -> Result<Provider> {
        let client = FuelClient::new(url).map_err(|err| error!(InfrastructureError, "{err}"))?;
        let consensus_parameters = client.chain_info().await?.consensus_parameters.into();
        Ok(Provider::new(client, consensus_parameters))
    }

In the TS SDK, we could have:

// new API
const provider = await Provider.connect('https://node.url');

// old API
const provider = new Provider('https://node.url')

Motivation behind this API change

Making the initialization of the provider asynchronous will let us internally fetch consensus parameters from the chain and cache them on our provider object for later use.

This means that all of our internal logic related to contract calls can stay synchronous as it is with minimal modifications, and just consume these cached consensus parameters from the provider object whenever needed.

Please correct me if I am wrong - but I think the consensus parameters of the chain should not change often. Still, if the user wants to invalidate the cache and wants the provider to use the latest consensus parameters, we can expose a method called invalidateConsensusParamsCache from the Provider class that lets them do that explicitly.

@maldiss
Copy link

maldiss commented Aug 12, 2023

great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants