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};
#[derive(Debug)]
pub struct SimplexProvider {
pub esplora: EsploraProvider,
pub elements: ElementsRpc,
}
impl SimplexProvider {
#[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()
}
}