Skip to content

Commit

Permalink
feat(backend): Expose and use min_confirmations filter to get utxos
Browse files Browse the repository at this point in the history
  • Loading branch information
lmuntaner committed Oct 1, 2024
1 parent 056035c commit fddc93b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/backend/src/bitcoin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use ic_cdk::api::management_canister::bitcoin::{
async fn get_utxos(
network: BitcoinNetwork,
address: String,
maybe_next_page: Option<Vec<u8>>,
filter: Option<UtxoFilter>,
) -> Result<GetUtxosResponse, String> {
let utxos_res = bitcoin_get_utxos(GetUtxosRequest {
address,
network,
filter: maybe_next_page.map(UtxoFilter::Page),
filter,
})
.await
.map_err(|err| err.1)?;
Expand All @@ -25,13 +25,19 @@ async fn get_utxos(
}
/// Returns all the UTXOs of a specific address.
/// API interface returns a paginated view of the utxos but we need to get them all.
pub async fn get_all_utxos(network: BitcoinNetwork, address: String) -> Result<Vec<Utxo>, String> {
let mut utxos_response = get_utxos(network, address.clone(), None).await?;
pub async fn get_all_utxos(
network: BitcoinNetwork,
address: String,
min_confirmations: Option<u32>,
) -> Result<Vec<Utxo>, String> {
let filter = min_confirmations.map(UtxoFilter::MinConfirmations);
let mut utxos_response = get_utxos(network, address.clone(), filter).await?;

let mut all_utxos: Vec<Utxo> = utxos_response.utxos;
let mut next_page: Option<Vec<u8>> = utxos_response.next_page;
while next_page.is_some() {
utxos_response = get_utxos(network, address.clone(), next_page).await?;
utxos_response =
get_utxos(network, address.clone(), next_page.map(UtxoFilter::Page)).await?;
all_utxos.extend(utxos_response.utxos);
next_page = utxos_response.next_page;
}
Expand Down
12 changes: 9 additions & 3 deletions src/backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,19 @@ fn list_custom_tokens() -> Vec<CustomToken> {
read_state(|s| s.custom_token.get(&stored_principal).unwrap_or_default().0)
}

const MIN_CONFIRMATIONS_ACCEPTED_BTC_TX: u32 = 6;

#[update(guard = "may_read_user_data")]
async fn btc_select_user_utxos_fee(
params: SelectedUtxosFeeRequest,
) -> Result<SelectedUtxosFeeResponse, SelectedUtxosFeeError> {
let all_utxos = bitcoin_api::get_all_utxos(params.network, params.source_address)
.await
.map_err(|msg| SelectedUtxosFeeError::InternalError { msg })?;
let all_utxos = bitcoin_api::get_all_utxos(
params.network,
params.source_address,
Some(MIN_CONFIRMATIONS_ACCEPTED_BTC_TX),
)
.await
.map_err(|msg| SelectedUtxosFeeError::InternalError { msg })?;

let median_fee_millisatoshi_per_vbyte = bitcoin_api::get_fee_per_byte(params.network)
.await
Expand Down

0 comments on commit fddc93b

Please sign in to comment.