Skip to main content

smplx_sdk/provider/
simplex.rs

1use std::collections::HashMap;
2
3use electrsd::bitcoind::bitcoincore_rpc::Auth;
4
5use simplicityhl::elements::{Address, Script, Transaction, Txid};
6
7use crate::provider::SimplicityNetwork;
8use crate::transaction::{TxReceipt, UTXO};
9
10use super::core::ProviderTrait;
11use super::error::ProviderError;
12use super::{ElementsRpc, EsploraProvider};
13
14/// A local provider used during Regtest or local development.
15/// It wraps an `EsploraProvider` for REST API queries and an `ElementsRpc` for direct node interactions.
16#[derive(Debug)]
17pub struct SimplexProvider {
18    /// The Esplora provider for handling REST API queries.
19    pub esplora: EsploraProvider,
20    /// The Elements RPC provider for direct node operations and wallet interaction.
21    pub elements: ElementsRpc,
22}
23
24impl SimplexProvider {
25    /// Creates a new `SimplexProvider` with the given URLs, authentication, and network.
26    ///
27    /// # Panics
28    /// Panics if the `ElementsRpc` client fails to initialize.
29    #[must_use]
30    pub fn new(esplora_url: String, elements_url: String, auth: Auth, network: SimplicityNetwork) -> Self {
31        Self {
32            esplora: EsploraProvider::new(esplora_url, network),
33            elements: ElementsRpc::new(elements_url, auth).unwrap(),
34        }
35    }
36}
37
38impl ProviderTrait for SimplexProvider {
39    fn get_network(&self) -> &SimplicityNetwork {
40        self.esplora.get_network()
41    }
42
43    fn broadcast_transaction(&self, tx: &Transaction) -> Result<TxReceipt<'_>, ProviderError> {
44        let tx_receipt = self.esplora.broadcast_transaction(tx)?;
45
46        self.elements.generate_blocks(1)?;
47
48        Ok(tx_receipt)
49    }
50
51    fn wait(&self, txid: &Txid) -> Result<(), ProviderError> {
52        self.esplora.wait(txid)
53    }
54
55    fn fetch_tip_height(&self) -> Result<u32, ProviderError> {
56        self.esplora.fetch_tip_height()
57    }
58
59    fn fetch_tip_timestamp(&self) -> Result<u64, ProviderError> {
60        self.esplora.fetch_tip_timestamp()
61    }
62
63    fn fetch_transaction(&self, txid: &Txid) -> Result<Transaction, ProviderError> {
64        self.esplora.fetch_transaction(txid)
65    }
66
67    fn fetch_address_utxos(&self, address: &Address) -> Result<Vec<UTXO>, ProviderError> {
68        self.esplora.fetch_address_utxos(address)
69    }
70
71    fn fetch_scripthash_utxos(&self, script: &Script) -> Result<Vec<UTXO>, ProviderError> {
72        self.esplora.fetch_scripthash_utxos(script)
73    }
74
75    fn fetch_fee_estimates(&self) -> Result<HashMap<String, f64>, ProviderError> {
76        self.esplora.fetch_fee_estimates()
77    }
78}