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, OutPoint, Script, Transaction, TxOut, Txid};
6
7use crate::provider::SimplicityNetwork;
8
9use super::error::ProviderError;
10use super::provider::ProviderTrait;
11
12use super::{ElementsRpc, EsploraProvider};
13
14pub struct SimplexProvider {
15    pub esplora: EsploraProvider,
16    pub elements: ElementsRpc,
17}
18
19impl SimplexProvider {
20    pub fn new(
21        esplora_url: String,
22        elements_url: String,
23        auth: Auth,
24        network: SimplicityNetwork,
25    ) -> Result<Self, ProviderError> {
26        Ok(Self {
27            esplora: EsploraProvider::new(esplora_url, network),
28            elements: ElementsRpc::new(elements_url, auth)?,
29        })
30    }
31}
32
33impl ProviderTrait for SimplexProvider {
34    fn get_network(&self) -> &SimplicityNetwork {
35        self.esplora.get_network()
36    }
37
38    fn broadcast_transaction(&self, tx: &Transaction) -> Result<Txid, ProviderError> {
39        let txid = self.esplora.broadcast_transaction(tx)?;
40
41        self.elements.generate_blocks(1)?;
42
43        Ok(txid)
44    }
45
46    fn wait(&self, txid: &Txid) -> Result<(), ProviderError> {
47        Ok(self.esplora.wait(txid)?)
48    }
49
50    fn fetch_transaction(&self, txid: &Txid) -> Result<Transaction, ProviderError> {
51        Ok(self.esplora.fetch_transaction(txid)?)
52    }
53
54    fn fetch_address_utxos(&self, address: &Address) -> Result<Vec<(OutPoint, TxOut)>, ProviderError> {
55        Ok(self.esplora.fetch_address_utxos(address)?)
56    }
57
58    fn fetch_scripthash_utxos(&self, script: &Script) -> Result<Vec<(OutPoint, TxOut)>, ProviderError> {
59        Ok(self.esplora.fetch_scripthash_utxos(script)?)
60    }
61
62    fn fetch_fee_estimates(&self) -> Result<HashMap<String, f64>, ProviderError> {
63        Ok(self.esplora.fetch_fee_estimates()?)
64    }
65}