Skip to content

Commit

Permalink
feat: replace FeeRateInput by FeeRateSelector on Create and Spend
Browse files Browse the repository at this point in the history
  • Loading branch information
luisschwab committed Mar 5, 2025
1 parent aabbaf5 commit c6f15c2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
48 changes: 42 additions & 6 deletions src/components/create.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
//! Create escrow transaction component.
use crate::error::Error;

use bitcoin::{Amount, Txid, consensus, hex::DisplayHex};
use dioxus::prelude::*;
use std::collections::HashMap;

#[cfg(debug_assertions)]
use dioxus::logger::tracing::{info, trace};

use crate::{
NETWORK, Route,
ESPLORA_ENDPOINT, NETWORK, Route,
esplora::{create_client, get_fee_estimates},
scripts::escrow_address,
tx::escrow_tx,
util::{
Expand All @@ -17,11 +21,19 @@ use crate::{
};

use super::{
BitcoinInput, ContinueButton, CopyButton, DerivedAddressOutput, FeeRateInput, Footer,
NetworkInput, NpubInput, NpubInputDerivedAddress, PrimaryButton, TimelockInput,
BitcoinInput, ContinueButton, CopyButton, DerivedAddressOutput, FeeRateSelector,
Footer, NetworkInput, NpubInput, NpubInputDerivedAddress, PrimaryButton, TimelockInput,
TransactionOutput, TxidInput,
};

/// Fetch fee rates from the configured Esplora endpoint.
async fn fetch_fee_estimates() -> Result<HashMap<u16, f64>, Error> {
let esplora_client = create_client(&ESPLORA_ENDPOINT.read()).unwrap();
let fee_rates = get_fee_estimates(&esplora_client).await.unwrap();

Ok(fee_rates)
}

/// Create escrow transaction component.
#[component]
pub(crate) fn Create() -> Element {
Expand All @@ -30,14 +42,36 @@ pub(crate) fn Create() -> Element {
let npub_arbitrator = use_signal(String::new);
let amount_buyer = use_signal(String::new);
let amount_seller = use_signal(String::new);
let fee_rate = use_signal(|| "1".to_string());
let mut fee_rate = use_signal(String::new);
let fee_estimates = use_signal(|| Option::<HashMap<u16, f64>>::None);
let timelock_days = use_signal(String::new);
let timelock_hours = use_signal(String::new);
let funding_txid = use_signal(String::new);
let mut escrow_address_str = use_signal(String::new);
let mut escrow_transaction = use_signal(String::new);
let mut derived_address_buyer = use_signal(String::new);
let mut derived_address_seller = use_signal(String::new);

use_effect(move || {
to_owned![fee_estimates];

spawn(async move {
match fetch_fee_estimates().await {
Ok(estimates) => {
#[cfg(debug_assertions)]
trace!("Fee estimates fetched successfully: {:?}", estimates);
fee_estimates.set(Some(estimates));
}
Err(e) => {
#[cfg(debug_assertions)]
trace!("Error fetching fee estimates: {}", e);
// Fall back to 3 sat/vB
fee_rate.set("3".to_string());
}
}
});
});

rsx! {
main { class: "max-w-7xl mx-auto py-6 sm:px-6 lg:px-8",
div { class: "px-4 py-6 sm:px-0",
Expand Down Expand Up @@ -76,10 +110,12 @@ pub(crate) fn Create() -> Element {
update_var: amount_seller,
}

FeeRateInput {
FeeRateSelector {
id: "fee",
label: "Fee rate (sats/vByte)",
label_input: "Fee rate (sats/vByte)",
label_dropdown: "Target Blocks",
update_var: fee_rate,
fee_estimates,
}

NetworkInput { id: "network", label: "Bitcoin Network" }
Expand Down
45 changes: 40 additions & 5 deletions src/components/spend.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,69 @@
//! Spend from resolution address component.
use crate::error::Error;

use bitcoin::{Address, Amount, TxOut, Txid, consensus, hex::DisplayHex};
use dioxus::prelude::*;
use std::collections::HashMap;

#[cfg(debug_assertions)]
use dioxus::logger::tracing::trace;

use crate::{
NETWORK, Route,
ESPLORA_ENDPOINT, NETWORK, Route,
esplora::{create_client, get_fee_estimates},
sign::sign_resolution_tx,
tx::resolution_tx,
util::{P2TR_TX_VBYTE_KEY_PATH, parse_network, parse_nsec},
};

use super::{
AddressInput, BitcoinInput, ContinueButton, CopyButton, DerivedAddressOutput, FeeRateInput,
AddressInput, BitcoinInput, ContinueButton, CopyButton, DerivedAddressOutput, FeeRateSelector,
Footer, NetworkInput, NpubInputDerivedAddress, NsecInput, PrimaryButton, TransactionOutput,
TxidInput, VoutInput,
};

/// Fetch fee rates from the configured Esplora endpoint.
async fn fetch_fee_estimates() -> Result<HashMap<u16, f64>, Error> {
let esplora_client = create_client(&ESPLORA_ENDPOINT.read()).unwrap();
let fee_rates = get_fee_estimates(&esplora_client).await.unwrap();

Ok(fee_rates)
}
/// Spend from resolution address component.
#[component]
pub(crate) fn Spend() -> Element {
let npub = use_signal(String::new);
let escrow_txid = use_signal(String::new);
let destination_address = use_signal(String::new);
let amount = use_signal(String::new);
let fee_rate = use_signal(|| "1".to_string());
let mut fee_rate = use_signal(String::new);
let fee_estimates = use_signal(|| Option::<HashMap<u16, f64>>::None);
let vout = use_signal(|| "0".to_string());
let derived_address = use_signal(String::new);
let nsec = use_signal(String::new);
let mut signed_tx_str = use_signal(String::new);

use_effect(move || {
to_owned![fee_estimates];

spawn(async move {
match fetch_fee_estimates().await {
Ok(estimates) => {
#[cfg(debug_assertions)]
trace!("Fee estimates fetched successfully: {:?}", estimates);
fee_estimates.set(Some(estimates));
}
Err(e) => {
#[cfg(debug_assertions)]
trace!("Error fetching fee estimates: {}", e);
// Fall back to 3 sat/vB
fee_rate.set("3".to_string());
}
}
});
});

rsx! {
main { class: "max-w-7xl mx-auto py-6 sm:px-6 lg:px-8",
div { class: "px-4 py-6 sm:px-0",
Expand Down Expand Up @@ -70,10 +103,12 @@ pub(crate) fn Spend() -> Element {
update_var: amount,
}

FeeRateInput {
FeeRateSelector {
id: "fee",
label: "Fee rate (sats/vByte)",
label_input: "Fee rate (sats/vByte)",
label_dropdown: "Target Blocks",
update_var: fee_rate,
fee_estimates,
}

DerivedAddressOutput {
Expand Down
2 changes: 1 addition & 1 deletion src/esplora.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Interactions with Esplora backends.
#![allow(dead_code)] // TODO: Dynamic fee estimates
#![allow(dead_code)]

use std::collections::HashMap;

Expand Down

0 comments on commit c6f15c2

Please sign in to comment.