alloy_provider/provider/
wallet.rsuse crate::{
fillers::{FillProvider, JoinFill, TxFiller, WalletFiller},
Provider,
};
use alloy_network::{Ethereum, Network, NetworkWallet};
use alloy_primitives::Address;
use alloy_transport::Transport;
pub trait WalletProvider<N: Network = Ethereum> {
type Wallet: NetworkWallet<N>;
fn wallet(&self) -> &Self::Wallet;
fn wallet_mut(&mut self) -> &mut Self::Wallet;
fn default_signer_address(&self) -> Address {
self.wallet().default_signer_address()
}
fn has_signer_for(&self, address: &Address) -> bool {
self.wallet().has_signer_for(address)
}
fn signer_addresses(&self) -> impl Iterator<Item = Address> {
self.wallet().signer_addresses()
}
}
impl<W, N> WalletProvider<N> for WalletFiller<W>
where
W: NetworkWallet<N> + Clone,
N: Network,
{
type Wallet = W;
#[inline(always)]
fn wallet(&self) -> &Self::Wallet {
self.as_ref()
}
#[inline(always)]
fn wallet_mut(&mut self) -> &mut Self::Wallet {
self.as_mut()
}
}
impl<L, R, N> WalletProvider<N> for JoinFill<L, R>
where
R: WalletProvider<N>,
N: Network,
{
type Wallet = R::Wallet;
#[inline(always)]
fn wallet(&self) -> &Self::Wallet {
self.right().wallet()
}
#[inline(always)]
fn wallet_mut(&mut self) -> &mut Self::Wallet {
self.right_mut().wallet_mut()
}
}
impl<F, P, T, N> WalletProvider<N> for FillProvider<F, P, T, N>
where
F: TxFiller<N> + WalletProvider<N>,
P: Provider<T, N>,
T: Transport + Clone,
N: Network,
{
type Wallet = F::Wallet;
#[inline(always)]
fn wallet(&self) -> &Self::Wallet {
self.filler.wallet()
}
#[inline(always)]
fn wallet_mut(&mut self) -> &mut Self::Wallet {
self.filler.wallet_mut()
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::ProviderBuilder;
use itertools::Itertools;
#[test]
fn basic_usage() {
let provider = ProviderBuilder::new().on_anvil_with_wallet();
assert!(provider.signer_addresses().contains(&provider.default_signer_address()));
}
#[test]
fn bubbles_through_fillers() {
let provider = ProviderBuilder::new().with_recommended_fillers().on_anvil_with_wallet();
assert!(provider.signer_addresses().contains(&provider.default_signer_address()));
}
}