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

[Feature] support simplify Provider type #1800

Open
sunliang711 opened this issue Dec 15, 2024 · 1 comment
Open

[Feature] support simplify Provider type #1800

sunliang711 opened this issue Dec 15, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@sunliang711
Copy link

Component

provider, pubsub

Describe the feature you would like

I want to create a function to return Provider type,but I must use type alias to define a looong provider type:

type Provider = alloy::providers::fillers::FillProvider<
    alloy::providers::fillers::JoinFill<
        alloy::providers::fillers::JoinFill<
            alloy::providers::Identity,
            alloy::providers::fillers::JoinFill<
                alloy::providers::fillers::GasFiller,
                alloy::providers::fillers::JoinFill<
                    alloy::providers::fillers::BlobGasFiller,
                    alloy::providers::fillers::JoinFill<
                        alloy::providers::fillers::NonceFiller,
                        alloy::providers::fillers::ChainIdFiller,
                    >,
                >,
            >,
        >,
        alloy::providers::fillers::WalletFiller<EthereumWallet>,
    >,
    alloy::providers::RootProvider<alloy::transports::http::Http<alloy::transports::http::Client>>,
    alloy::transports::http::Http<alloy::transports::http::Client>,
    alloy::network::Ethereum,
>;
pub fn provider_with(rpc_url: &str, wallet: EthereumWallet) -> Result<Provider> {
    let url = Url::parse(rpc_url)?;
    let provider_builder = ProviderBuilder::new().with_recommended_fillers();
    let provider = provider_builder.wallet(wallet).on_http(url);

    Ok(provider)
}

how do i simplify this code ? thanks in advance .

Additional context

No response

@sunliang711 sunliang711 added the enhancement New feature or request label Dec 15, 2024
@scaraven
Copy link

I struggled with this for a while as well, simply because I am relatively new to rust.

Assuming that you are happy with being constrained to an HTTP Provider, I would use either:

pub fn provider_with(rpc_url: &str, wallet: EthereumWallet) -> Result<impl Provider<Http<Client>>> {}

or

pub fn provider_with(rpc_url: &str, wallet: EthereumWallet) -> Result<Box<dyn Provider<Http<Client>>>> {}

depending on whether you only care about using this specific provider but want to hide the return type from any callers (impl) or you potentially want to work with lots of different providers (dyn).

Additionally, if you want to abstract even further, then it might be worth using on_builtin() rather than on_http() but that opens another can of worms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants