|
| 1 | +//! Helper functions to build Ethereum [providers](https://docs.rs/alloy/latest/alloy/providers/trait.Provider.html) |
| 2 | +//! Partial Credit: <https://github.com/EspressoSystems/espresso-network/tree/main/contracts/rust/deployer> |
| 3 | +
|
| 4 | +use alloy::{ |
| 5 | + network::{Ethereum, EthereumWallet}, |
| 6 | + providers::{ |
| 7 | + ProviderBuilder, RootProvider, |
| 8 | + fillers::{FillProvider, JoinFill, WalletFiller}, |
| 9 | + layers::AnvilProvider, |
| 10 | + utils::JoinedRecommendedFillers, |
| 11 | + }, |
| 12 | + signers::local::{MnemonicBuilder, PrivateKeySigner, coins_bip39::English}, |
| 13 | + transports::http::reqwest::Url, |
| 14 | +}; |
| 15 | + |
| 16 | +/// Type alias that connects to providers with recommended fillers and wallet |
| 17 | +/// use `<HttpProviderWithWallet as WalletProvider>::wallet()` to access internal wallet |
| 18 | +/// use `<HttpProviderWithWallet as WalletProvider>::default_signer_address(&provider)` to get |
| 19 | +/// wallet address |
| 20 | +pub type HttpProviderWithWallet = FillProvider< |
| 21 | + JoinFill<JoinedRecommendedFillers, WalletFiller<EthereumWallet>>, |
| 22 | + RootProvider, |
| 23 | + Ethereum, |
| 24 | +>; |
| 25 | + |
| 26 | +pub type TestProviderWithWallet = FillProvider< |
| 27 | + JoinFill<JoinedRecommendedFillers, WalletFiller<EthereumWallet>>, |
| 28 | + AnvilProvider<RootProvider>, |
| 29 | + Ethereum, |
| 30 | +>; |
| 31 | + |
| 32 | +/// Build a local signer from wallet mnemonic and account index |
| 33 | +pub fn build_signer(mnemonic: String, account_index: u32) -> PrivateKeySigner { |
| 34 | + MnemonicBuilder::<English>::default() |
| 35 | + .phrase(mnemonic) |
| 36 | + .index(account_index) |
| 37 | + .expect("wrong mnemonic or index") |
| 38 | + .build() |
| 39 | + .expect("fail to build signer") |
| 40 | +} |
| 41 | + |
| 42 | +/// a handy thin wrapper around wallet builder and provider builder that directly |
| 43 | +/// returns an instantiated `Provider` with default fillers with wallet, ready to send tx |
| 44 | +pub fn build_provider(mnemonic: String, account_index: u32, url: Url) -> HttpProviderWithWallet { |
| 45 | + let signer = build_signer(mnemonic, account_index); |
| 46 | + let wallet = EthereumWallet::from(signer); |
| 47 | + ProviderBuilder::new().wallet(wallet).connect_http(url) |
| 48 | +} |
0 commit comments