logo
pub trait BlockchainFactory {
    type Inner: Blockchain;
    fn build(
        &self,
        wallet_name: &str,
        override_skip_blocks: Option<u32>
    ) -> Result<Self::Inner, Error>; fn build_for_wallet<D: BatchDatabase>(
        &self,
        wallet: &Wallet<D>,
        override_skip_blocks: Option<u32>
    ) -> Result<Self::Inner, Error> { ... }
fn sync_wallet<D: BatchDatabase>(
        &self,
        wallet: &Wallet<D>,
        override_skip_blocks: Option<u32>,
        sync_options: SyncOptions
    ) -> Result<(), Error> { ... } }
Expand description

Trait for a factory of blockchains that share the underlying connection or configuration

Example

This example shows how to sync multiple walles and return the sum of their balances

fn sum_of_balances<B: BlockchainFactory>(blockchain_factory: B, wallets: &[Wallet<MemoryDatabase>]) -> Result<Balance, Error> {
    Ok(wallets
        .iter()
        .map(|w| -> Result<_, Error> {
            blockchain_factory.sync_wallet(&w, None, SyncOptions::default())?;
            w.get_balance()
        })
        .collect::<Result<Vec<_>, _>>()?
        .into_iter()
        .sum())
}

Associated Types

The type returned when building a blockchain from this factory

Required methods

Build a new blockchain for the given descriptor wallet_name

If override_skip_blocks is None, the returned blockchain will inherit the number of blocks from the factory. Since it’s not possible to override the value to None, set it to Some(0) to rescan from the genesis.

Provided methods

Build a new blockchain for a given wallet

Internally uses wallet_name_from_descriptor to derive the name, and then calls BlockchainFactory::build to create the blockchain instance.

This is supported on neither WebAssembly nor crate feature async-interface.

Use BlockchainFactory::build_for_wallet to get a blockchain, then sync the wallet

This can be used when a new blockchain would only be used to sync a wallet and then immediately dropped. Keep in mind that specific blockchain factories may perform slow operations to build a blockchain for a given wallet, so if a wallet needs to be synced often it’s recommended to use BlockchainFactory::build_for_wallet to reuse the same blockchain multiple times.

Implementations on Foreign Types

Implementors