smplx-sdk 0.0.5

Simplex sdk to simplify the development with simplicity
Documentation
use std::collections::HashMap;

use electrsd::bitcoind::bitcoincore_rpc::Auth;

use simplicityhl::elements::{Address, Script, Transaction, Txid};

use crate::provider::SimplicityNetwork;
use crate::transaction::{TxReceipt, UTXO};

use super::core::ProviderTrait;
use super::error::ProviderError;
use super::{ElementsRpc, EsploraProvider};

/// A local provider used during Regtest or local development.
/// It wraps an `EsploraProvider` for REST API queries and an `ElementsRpc` for direct node interactions.
#[derive(Debug)]
pub struct SimplexProvider {
    /// The Esplora provider for handling REST API queries.
    pub esplora: EsploraProvider,
    /// The Elements RPC provider for direct node operations and wallet interaction.
    pub elements: ElementsRpc,
}

impl SimplexProvider {
    /// Creates a new `SimplexProvider` with the given URLs, authentication, and network.
    ///
    /// # Panics
    /// Panics if the `ElementsRpc` client fails to initialize.
    #[must_use]
    pub fn new(esplora_url: String, elements_url: String, auth: Auth, network: SimplicityNetwork) -> Self {
        Self {
            esplora: EsploraProvider::new(esplora_url, network),
            elements: ElementsRpc::new(elements_url, auth).unwrap(),
        }
    }
}

impl ProviderTrait for SimplexProvider {
    fn get_network(&self) -> &SimplicityNetwork {
        self.esplora.get_network()
    }

    fn broadcast_transaction(&self, tx: &Transaction) -> Result<TxReceipt<'_>, ProviderError> {
        let tx_receipt = self.esplora.broadcast_transaction(tx)?;

        self.elements.generate_blocks(1)?;

        Ok(tx_receipt)
    }

    fn wait(&self, txid: &Txid) -> Result<(), ProviderError> {
        self.esplora.wait(txid)
    }

    fn fetch_tip_height(&self) -> Result<u32, ProviderError> {
        self.esplora.fetch_tip_height()
    }

    fn fetch_tip_timestamp(&self) -> Result<u64, ProviderError> {
        self.esplora.fetch_tip_timestamp()
    }

    fn fetch_transaction(&self, txid: &Txid) -> Result<Transaction, ProviderError> {
        self.esplora.fetch_transaction(txid)
    }

    fn fetch_address_utxos(&self, address: &Address) -> Result<Vec<UTXO>, ProviderError> {
        self.esplora.fetch_address_utxos(address)
    }

    fn fetch_scripthash_utxos(&self, script: &Script) -> Result<Vec<UTXO>, ProviderError> {
        self.esplora.fetch_scripthash_utxos(script)
    }

    fn fetch_fee_estimates(&self) -> Result<HashMap<String, f64>, ProviderError> {
        self.esplora.fetch_fee_estimates()
    }
}