Skip to content

Commit

Permalink
Merge pull request #530 from MutinyWallet/estimate-chan-fee
Browse files Browse the repository at this point in the history
Add function for estimating channel open fee
  • Loading branch information
TonyGiorgio committed May 24, 2023
2 parents 3445b49 + 2ed7491 commit 2115429
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
18 changes: 17 additions & 1 deletion mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use bdk::chain::{BlockId, ConfirmationTime};
use bdk::{wallet::AddressIndex, LocalUtxo};
use bdk_esplora::esplora_client::AsyncClient;
use bip39::Mnemonic;
use bitcoin::blockdata::script;
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::secp256k1::{rand, PublicKey};
Expand Down Expand Up @@ -712,7 +713,22 @@ impl<S: MutinyStorage> NodeManager<S> {
fee_rate: Option<f32>,
) -> Result<u64, MutinyError> {
self.wallet
.estimate_tx_fee(destination_address, amount, fee_rate)
.estimate_tx_fee(destination_address.script_pubkey(), amount, fee_rate)
}

/// Estimates the onchain fee for a opening a lightning channel.
/// The amount is in satoshis and the fee rate is in sat/vbyte.
pub fn estimate_channel_open_fee(
&self,
amount: u64,
fee_rate: Option<f32>,
) -> Result<u64, MutinyError> {
// Dummy p2wsh script for the channel output
let script = script::Builder::new()
.push_int(0)
.push_slice(&[0; 32])
.into_script();
self.wallet.estimate_tx_fee(script, amount, fee_rate)
}

/// Checks if the given address has any transactions.
Expand Down
5 changes: 2 additions & 3 deletions mutiny-core/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,11 @@ impl<S: MutinyStorage> OnChainWallet<S> {

pub fn estimate_tx_fee(
&self,
destination_address: Address,
spk: Script,
amount: u64,
fee_rate: Option<f32>,
) -> Result<u64, MutinyError> {
let psbt =
self.create_signed_psbt_to_spk(destination_address.script_pubkey(), amount, fee_rate)?;
let psbt = self.create_signed_psbt_to_spk(spk, amount, fee_rate)?;

psbt.fee_amount().ok_or(MutinyError::WalletOperationFailed)
}
Expand Down
13 changes: 13 additions & 0 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ impl MutinyWallet {
.estimate_tx_fee(addr, amount, fee_rate)?)
}

/// Estimates the onchain fee for a opening a lightning channel.
/// The amount is in satoshis and the fee rate is in sat/vbyte.
pub fn estimate_channel_open_fee(
&self,
amount: u64,
fee_rate: Option<f32>,
) -> Result<u64, MutinyJsError> {
Ok(self
.inner
.node_manager
.estimate_channel_open_fee(amount, fee_rate)?)
}

/// Checks if the given address has any transactions.
/// If it does, it returns the details of the first transaction.
///
Expand Down

0 comments on commit 2115429

Please sign in to comment.