Skip to content

Commit

Permalink
fix: rename chain service methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hydra-yse committed Sep 18, 2024
1 parent 3f93440 commit 9090ef9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
9 changes: 8 additions & 1 deletion lib/core/src/chain/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{thread, time::Duration};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use boltz_client::{Address, ToHex};
use electrum_client::{Client, ElectrumApi, GetBalanceRes, HeaderNotification};
use electrum_client::{Client, ElectrumApi, GetBalanceRes, HeaderNotification, ListUnspentRes};
use log::info;
use lwk_wollet::{
bitcoin::{
Expand Down Expand Up @@ -43,6 +43,9 @@ pub trait BitcoinChainService: Send + Sync {
retries: u64,
) -> Result<Vec<History>>;

/// Get the utxos associated with a script pubkey
async fn get_script_utxos(&self, script: &Script) -> Result<Vec<ListUnspentRes>>;

/// Return the confirmed and unconfirmed balances of a script hash
fn script_get_balance(&self, script: &Script) -> Result<GetBalanceRes>;

Expand Down Expand Up @@ -173,6 +176,10 @@ impl BitcoinChainService for HybridBitcoinChainService {
Ok(script_history)
}

async fn get_script_utxos(&self, script: &Script) -> Result<Vec<ListUnspentRes>> {
Ok(self.client.script_list_unspent(script)?)
}

fn script_get_balance(&self, script: &Script) -> Result<GetBalanceRes> {
Ok(self.client.script_get_balance(script)?)
}
Expand Down
47 changes: 25 additions & 22 deletions lib/core/src/chain/liquid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pub trait LiquidChainService: Send + Sync {
retries: u64,
) -> Result<Vec<History>>;

/// Get the tx outpoint associated with a script pubkey
async fn get_script_history_outpoint(&self, script: &Script) -> Result<(OutPoint, TxOut)>;
/// Get the utxos associated with a script pubkey
async fn get_script_utxos(&self, script: &Script) -> Result<Vec<(OutPoint, TxOut)>>;

/// Verify that a transaction appears in the address script history
async fn verify_tx(
Expand Down Expand Up @@ -196,31 +196,34 @@ impl LiquidChainService for HybridLiquidChainService {
Ok(script_history)
}

async fn get_script_history_outpoint(&self, script: &Script) -> Result<(OutPoint, TxOut)> {
async fn get_script_utxos(&self, script: &Script) -> Result<Vec<(OutPoint, TxOut)>> {
let history = self.get_script_history_with_retry(script, 3).await?;
if history.is_empty() {
return Err(anyhow!("Transaction history is empty."));
}

let txid = history
.last()
.ok_or(anyhow!("Expected at least one txid"))?
.txid;
let tx = self
.get_transaction_hex(&txid)
.await?
.ok_or(anyhow!("Transaction not found"))?;

let script_pubkey = script.clone();
for (vout, output) in tx.clone().output.into_iter().enumerate() {
if output.script_pubkey == script_pubkey {
let outpoint_0 = OutPoint::new(tx.txid(), vout as u32);

return Ok((outpoint_0, output));
let mut utxos: Vec<(OutPoint, TxOut)> = vec![];
for history_item in history {
match self.get_transaction_hex(&history_item.txid).await {
Ok(Some(tx)) => {
let mut new_utxos: Vec<(OutPoint, TxOut)> = tx
.output
.iter()
.enumerate()
.map(|(vout, output)| {
(
OutPoint::new(history_item.txid, vout as u32),
output.clone(),
)
})
.collect();
utxos.append(&mut new_utxos);
}
_ => {
log::warn!("Could not retrieve transaction from history item");
continue;
}
}
}

return Err(anyhow!("Transaction outpoint not found"));
return Ok(utxos);
}

async fn verify_tx(
Expand Down

0 comments on commit 9090ef9

Please sign in to comment.