Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Jan 6, 2025
1 parent 8f9f92d commit be9daf1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 38 deletions.
31 changes: 2 additions & 29 deletions lib/core/src/chain/liquid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use boltz_client::ToHex;
use log::info;
use lwk_wollet::elements::hex::FromHex;
use lwk_wollet::{
elements::{Address, BlockHash, OutPoint, Script, Transaction, Txid},
elements::{Address, OutPoint, Script, Transaction, Txid},
hashes::{sha256, Hash},
BlockchainBackend, ElectrumClient, ElectrumUrl, History,
};
use serde::Deserialize;

use crate::prelude::Utxo;
use crate::{model::Config, utils};
Expand All @@ -29,9 +28,7 @@ pub trait LiquidChainService: Send + Sync {
/// Get a list of transactions
async fn get_transactions(&self, txids: &[Txid]) -> Result<Vec<Transaction>>;

/// Get the transactions involved in a script.
///
/// On mainnet, the data is fetched from Esplora. On testnet, it's fetched from Electrum.
/// Get the transactions involved in a script
async fn get_script_history(&self, scripts: &Script) -> Result<Vec<History>>;

/// Get the transactions involved in a list of scripts.
Expand Down Expand Up @@ -59,18 +56,6 @@ pub trait LiquidChainService: Send + Sync {
) -> Result<Transaction>;
}

#[derive(Deserialize)]
struct EsploraTx {
txid: Txid,
status: Status,
}

#[derive(Deserialize)]
struct Status {
block_height: Option<i32>,
block_hash: Option<BlockHash>,
}

pub(crate) struct HybridLiquidChainService {
electrum_client: ElectrumClient,
}
Expand Down Expand Up @@ -212,15 +197,3 @@ impl LiquidChainService for HybridLiquidChainService {
}
}
}

impl From<EsploraTx> for History {
fn from(value: EsploraTx) -> Self {
let status = value.status;
History {
txid: value.txid,
height: status.block_height.unwrap_or_default(),
block_hash: status.block_hash,
block_timestamp: None,
}
}
}
8 changes: 6 additions & 2 deletions lib/core/src/chain_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use lwk_wollet::{
};
use tokio::sync::{broadcast, Mutex};

use crate::model::{BlockListener, ChainSwapUpdate};
use crate::model::{BlockListener, ChainSwapUpdate, LIQUID_FEE_RATE_MSAT_PER_VBYTE};
use crate::{
chain::{bitcoin::BitcoinChainService, liquid::LiquidChainService},
ensure_sdk,
Expand Down Expand Up @@ -734,7 +734,11 @@ impl ChainSwapHandler {

let lockup_tx = self
.onchain_wallet
.build_tx_or_drain_tx(&lockup_details.lockup_address, lockup_details.amount)
.build_tx_or_drain_tx(
Some(LIQUID_FEE_RATE_MSAT_PER_VBYTE),
&lockup_details.lockup_address,
lockup_details.amount,
)
.await?;

let lockup_tx_id = self
Expand Down
1 change: 1 addition & 0 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::utils;

// Uses f64 for the maximum precision when converting between units
pub const LIQUID_FEE_RATE_SAT_PER_VBYTE: f64 = 0.1;
pub const LIQUID_FEE_RATE_MSAT_PER_VBYTE: f32 = (LIQUID_FEE_RATE_SAT_PER_VBYTE * 1000.0) as f32;
const BREEZ_SYNC_SERVICE_URL: &str = "https://datasync.breez.technology";

/// Configuration for the Liquid SDK
Expand Down
14 changes: 11 additions & 3 deletions lib/core/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ impl LiquidSdk {
) -> Result<u64, PaymentError> {
Ok(self
.onchain_wallet
.build_tx(address, amount_sat)
.build_tx(Some(LIQUID_FEE_RATE_MSAT_PER_VBYTE), address, amount_sat)
.await?
.all_fees()
.values()
Expand Down Expand Up @@ -824,7 +824,11 @@ impl LiquidSdk {
let receipent_address = address.unwrap_or(self.get_temp_p2tr_addr());
let fee_sat = self
.onchain_wallet
.build_drain_tx(receipent_address, enforce_amount_sat)
.build_drain_tx(
Some(LIQUID_FEE_RATE_MSAT_PER_VBYTE),
receipent_address,
enforce_amount_sat,
)
.await?
.all_fees()
.values()
Expand Down Expand Up @@ -1214,7 +1218,11 @@ impl LiquidSdk {

let tx = self
.onchain_wallet
.build_tx_or_drain_tx(&address_data.address, receiver_amount_sat)
.build_tx_or_drain_tx(
Some(LIQUID_FEE_RATE_MSAT_PER_VBYTE),
&address_data.address,
receiver_amount_sat,
)
.await?;
let tx_fees_sat = tx.all_fees().values().sum::<u64>();
ensure_sdk!(tx_fees_sat <= fees_sat, PaymentError::InvalidOrExpiredFees);
Expand Down
10 changes: 8 additions & 2 deletions lib/core/src/send_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ use sdk_common::prelude::{AesSuccessActionDataResult, SuccessAction, SuccessActi
use tokio::sync::{broadcast, Mutex};

use crate::chain::liquid::LiquidChainService;
use crate::model::{BlockListener, Config, PaymentState::*, SendSwap};
use crate::model::{
BlockListener, Config, PaymentState::*, SendSwap, LIQUID_FEE_RATE_MSAT_PER_VBYTE,
};
use crate::persist::model::PaymentTxDetails;
use crate::prelude::{PaymentTxData, PaymentType, Swap};
use crate::recover::recoverer::Recoverer;
Expand Down Expand Up @@ -194,7 +196,11 @@ impl SendSwapHandler {

let lockup_tx = self
.onchain_wallet
.build_tx_or_drain_tx(&create_response.address, create_response.expected_amount)
.build_tx_or_drain_tx(
Some(LIQUID_FEE_RATE_MSAT_PER_VBYTE),
&create_response.address,
create_response.expected_amount,
)
.await?;
let lockup_tx_id = lockup_tx.txid().to_string();

Expand Down
3 changes: 3 additions & 0 deletions lib/core/src/test_utils/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl OnchainWallet for MockWallet {

async fn build_tx(
&self,
_fee_rate: Option<f32>,
_recipient_address: &str,
_amount_sat: u64,
) -> Result<Transaction, PaymentError> {
Expand All @@ -61,6 +62,7 @@ impl OnchainWallet for MockWallet {

async fn build_drain_tx(
&self,
_fee_rate_sats_per_kvb: Option<f32>,
_recipient_address: &str,
_enforce_amount_sat: Option<u64>,
) -> Result<Transaction, PaymentError> {
Expand All @@ -69,6 +71,7 @@ impl OnchainWallet for MockWallet {

async fn build_tx_or_drain_tx(
&self,
_fee_rate_sats_per_kvb: Option<f32>,
_recipient_address: &str,
_amount_sat: u64,
) -> Result<Transaction, PaymentError> {
Expand Down
16 changes: 14 additions & 2 deletions lib/core/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ pub trait OnchainWallet: Send + Sync {
/// Build a transaction to send funds to a recipient
async fn build_tx(
&self,
fee_rate_sats_per_kvb: Option<f32>,
recipient_address: &str,
amount_sat: u64,
) -> Result<Transaction, PaymentError>;

/// Builds a drain tx.
///
/// ### Arguments
/// - `fee_rate_sats_per_kvb`: custom drain tx feerate
/// - `recipient_address`: drain tx recipient
/// - `enforce_amount_sat`: if set, the drain tx will only be built if the amount transferred is
/// this amount, otherwise it will fail with a validation error
async fn build_drain_tx(
&self,
fee_rate_sats_per_kvb: Option<f32>,
recipient_address: &str,
enforce_amount_sat: Option<u64>,
) -> Result<Transaction, PaymentError>;
Expand All @@ -65,6 +68,7 @@ pub trait OnchainWallet: Send + Sync {
/// validating that the `amount_sat` matches the drain output.
async fn build_tx_or_drain_tx(
&self,
fee_rate_sats_per_kvb: Option<f32>,
recipient_address: &str,
amount_sat: u64,
) -> Result<Transaction, PaymentError>;
Expand Down Expand Up @@ -197,6 +201,7 @@ impl OnchainWallet for LiquidOnchainWallet {
/// Build a transaction to send funds to a recipient
async fn build_tx(
&self,
fee_rate_sats_per_kvb: Option<f32>,
recipient_address: &str,
amount_sat: u64,
) -> Result<Transaction, PaymentError> {
Expand All @@ -212,6 +217,7 @@ impl OnchainWallet for LiquidOnchainWallet {
})?,
amount_sat,
)?
.fee_rate(fee_rate_sats_per_kvb)
.enable_ct_discount()
.finish(&lwk_wollet)?;
self.signer
Expand All @@ -224,6 +230,7 @@ impl OnchainWallet for LiquidOnchainWallet {

async fn build_drain_tx(
&self,
fee_rate_sats_per_kvb: Option<f32>,
recipient_address: &str,
enforce_amount_sat: Option<u64>,
) -> Result<Transaction, PaymentError> {
Expand All @@ -239,6 +246,7 @@ impl OnchainWallet for LiquidOnchainWallet {
.tx_builder()
.drain_lbtc_wallet()
.drain_lbtc_to(address)
.fee_rate(fee_rate_sats_per_kvb)
.enable_ct_discount()
.finish()?;

Expand Down Expand Up @@ -269,14 +277,18 @@ impl OnchainWallet for LiquidOnchainWallet {

async fn build_tx_or_drain_tx(
&self,
fee_rate_sats_per_kvb: Option<f32>,
recipient_address: &str,
amount_sat: u64,
) -> Result<Transaction, PaymentError> {
match self.build_tx(recipient_address, amount_sat).await {
match self
.build_tx(fee_rate_sats_per_kvb, recipient_address, amount_sat)
.await
{
Ok(tx) => Ok(tx),
Err(PaymentError::InsufficientFunds) => {
warn!("Cannot build tx due to insufficient funds, attempting to build drain tx");
self.build_drain_tx(recipient_address, Some(amount_sat))
self.build_drain_tx(fee_rate_sats_per_kvb, recipient_address, Some(amount_sat))
.await
}
Err(e) => Err(e),
Expand Down

0 comments on commit be9daf1

Please sign in to comment.