From d3dba385d97c0a60e06c15a2f5b859aba50fef55 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 12:07:18 +0200 Subject: [PATCH 1/8] feat: get_change_output_and_fees match collateral value --- dlc/src/lib.rs | 134 +++++++++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 59 deletions(-) diff --git a/dlc/src/lib.rs b/dlc/src/lib.rs index 0aede2ee..a39d81a5 100644 --- a/dlc/src/lib.rs +++ b/dlc/src/lib.rs @@ -275,65 +275,81 @@ impl PartyParams { fee_rate_per_vb: u64, extra_fee: u64, ) -> Result<(TxOut, u64, u64), Error> { - let mut inputs_weight: usize = 0; - - for w in &self.inputs { - let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) - .len() - .checked_mul(4) - .ok_or(Error::InvalidArgument(format!("[get_change_output_and_fees] error: failed to transform a redeem script for a p2sh-p2w* output to a script signature")))?; - inputs_weight = checked_add!( - inputs_weight, - TX_INPUT_BASE_WEIGHT, - script_weight, - w.max_witness_len - )?; - } - - // Value size + script length var_int + ouput script pubkey size - let change_size = self.change_script_pubkey.len(); - // Change size is scaled by 4 from vBytes to weight units - let change_weight = change_size - .checked_mul(4) - .ok_or(Error::InvalidArgument(format!( - "[get_change_output_and_fees] error: failed to calculate change weight" - )))?; - - // Base weight (nLocktime, nVersion, ...) is distributed among parties - // independently of inputs contributed - let this_party_fund_base_weight = FUND_TX_BASE_WEIGHT / 2; - - let total_fund_weight = checked_add!( - this_party_fund_base_weight, - inputs_weight, - change_weight, - 36 - )?; - let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; - - // Base weight (nLocktime, nVersion, funding input ...) is distributed - // among parties independently of output types - let this_party_cet_base_weight = CET_BASE_WEIGHT / 2; - - // size of the payout script pubkey scaled by 4 from vBytes to weight units - let output_spk_weight = - self.payout_script_pubkey - .len() - .checked_mul(4) - .ok_or(Error::InvalidArgument(format!( - "[get_change_output_and_fees] error: failed to calculate payout script pubkey weight" - )))?; - let total_cet_weight = checked_add!(this_party_cet_base_weight, output_spk_weight)?; - let cet_or_refund_fee = util::weight_to_fee(total_cet_weight, fee_rate_per_vb)?; - let required_input_funds = - checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)?; - if self.input_amount < required_input_funds { - return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); - } - - let change_output = TxOut { - value: self.input_amount - required_input_funds, - script_pubkey: self.change_script_pubkey.clone(), + let (change_output, fund_fee, cet_or_refund_fee) = match self.collateral { + 0 => ( + TxOut { + value: self.input_amount, + script_pubkey: self.change_script_pubkey.clone(), + }, + 0, + 0, + ), + _ => { + let mut inputs_weight: usize = 0; + + for w in &self.inputs { + let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) + .len() + .checked_mul(4) + .ok_or(Error::InvalidArgument(format!("[get_change_output_and_fees] error: failed to transform a redeem script for a p2sh-p2w* output to a script signature")))?; + inputs_weight = checked_add!( + inputs_weight, + TX_INPUT_BASE_WEIGHT, + script_weight, + w.max_witness_len + )?; + } + + // Value size + script length var_int + ouput script pubkey size + let change_size = self.change_script_pubkey.len(); + // Change size is scaled by 4 from vBytes to weight units + let change_weight = + change_size + .checked_mul(4) + .ok_or(Error::InvalidArgument(format!( + "[get_change_output_and_fees] error: failed to calculate change weight" + )))?; + + // Base weight (nLocktime, nVersion, ...) is distributed among parties + let this_party_fund_base_weight = FUND_TX_BASE_WEIGHT; + + let total_fund_weight = checked_add!( + this_party_fund_base_weight, + inputs_weight, + change_weight, + 36 + )?; + let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; + + // Base weight (nLocktime, nVersion, funding input ...) is distributed + // among parties independently of output types + let this_party_cet_base_weight = CET_BASE_WEIGHT; + + // size of the payout script pubkey scaled by 4 from vBytes to weight units + let output_spk_weight = + self.payout_script_pubkey + .len() + .checked_mul(4) + .ok_or(Error::InvalidArgument(format!( + "[get_change_output_and_fees] error: failed to calculate payout script pubkey weight" + )))?; + let total_cet_weight = checked_add!(this_party_cet_base_weight, output_spk_weight)?; + let cet_or_refund_fee = util::weight_to_fee(total_cet_weight, fee_rate_per_vb)?; + + let required_input_funds = + checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)?; + + if self.input_amount < required_input_funds { + return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); + } + + let change_output = TxOut { + value: self.input_amount - required_input_funds, + script_pubkey: self.change_script_pubkey.clone(), + }; + + (change_output, fund_fee, cet_or_refund_fee) + } }; Ok((change_output, fund_fee, cet_or_refund_fee)) From bb57fa03452d5ec96610ecc81def4fe9afc9b178 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 14:49:28 +0200 Subject: [PATCH 2/8] feat: simplfied logic --- dlc/src/lib.rs | 148 ++++++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 77 deletions(-) diff --git a/dlc/src/lib.rs b/dlc/src/lib.rs index a39d81a5..c8817d83 100644 --- a/dlc/src/lib.rs +++ b/dlc/src/lib.rs @@ -275,84 +275,76 @@ impl PartyParams { fee_rate_per_vb: u64, extra_fee: u64, ) -> Result<(TxOut, u64, u64), Error> { - let (change_output, fund_fee, cet_or_refund_fee) = match self.collateral { - 0 => ( - TxOut { - value: self.input_amount, - script_pubkey: self.change_script_pubkey.clone(), - }, - 0, - 0, - ), - _ => { - let mut inputs_weight: usize = 0; - - for w in &self.inputs { - let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) - .len() - .checked_mul(4) - .ok_or(Error::InvalidArgument(format!("[get_change_output_and_fees] error: failed to transform a redeem script for a p2sh-p2w* output to a script signature")))?; - inputs_weight = checked_add!( - inputs_weight, - TX_INPUT_BASE_WEIGHT, - script_weight, - w.max_witness_len - )?; - } - - // Value size + script length var_int + ouput script pubkey size - let change_size = self.change_script_pubkey.len(); - // Change size is scaled by 4 from vBytes to weight units - let change_weight = - change_size - .checked_mul(4) - .ok_or(Error::InvalidArgument(format!( - "[get_change_output_and_fees] error: failed to calculate change weight" - )))?; - - // Base weight (nLocktime, nVersion, ...) is distributed among parties - let this_party_fund_base_weight = FUND_TX_BASE_WEIGHT; - - let total_fund_weight = checked_add!( - this_party_fund_base_weight, - inputs_weight, - change_weight, - 36 - )?; - let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; - - // Base weight (nLocktime, nVersion, funding input ...) is distributed - // among parties independently of output types - let this_party_cet_base_weight = CET_BASE_WEIGHT; - - // size of the payout script pubkey scaled by 4 from vBytes to weight units - let output_spk_weight = - self.payout_script_pubkey - .len() - .checked_mul(4) - .ok_or(Error::InvalidArgument(format!( - "[get_change_output_and_fees] error: failed to calculate payout script pubkey weight" - )))?; - let total_cet_weight = checked_add!(this_party_cet_base_weight, output_spk_weight)?; - let cet_or_refund_fee = util::weight_to_fee(total_cet_weight, fee_rate_per_vb)?; - - let required_input_funds = - checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)?; - - if self.input_amount < required_input_funds { - return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); - } - - let change_output = TxOut { - value: self.input_amount - required_input_funds, - script_pubkey: self.change_script_pubkey.clone(), - }; - - (change_output, fund_fee, cet_or_refund_fee) - } + let mut inputs_weight: usize = 0; + + for w in &self.inputs { + let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) + .len() + .checked_mul(4) + .ok_or(Error::InvalidArgument(format!("[get_change_output_and_fees] error: failed to transform a redeem script for a p2sh-p2w* output to a script signature")))?; + inputs_weight = checked_add!( + inputs_weight, + TX_INPUT_BASE_WEIGHT, + script_weight, + w.max_witness_len + )?; + } + + // Value size + script length var_int + ouput script pubkey size + let change_size = self.change_script_pubkey.len(); + // Change size is scaled by 4 from vBytes to weight units + let change_weight = change_size + .checked_mul(4) + .ok_or(Error::InvalidArgument(format!( + "[get_change_output_and_fees] error: failed to calculate change weight" + )))?; + + // Base weight (nLocktime, nVersion, ...) is distributed among parties + // independently of inputs contributed + let this_party_fund_base_weight = FUND_TX_BASE_WEIGHT; + + let total_fund_weight = checked_add!( + this_party_fund_base_weight, + inputs_weight, + change_weight, + 36 + )?; + let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; + + // Base weight (nLocktime, nVersion, funding input ...) is distributed + // among parties independently of output types + let this_party_cet_base_weight = CET_BASE_WEIGHT; + + // size of the payout script pubkey scaled by 4 from vBytes to weight units + let output_spk_weight = + self.payout_script_pubkey + .len() + .checked_mul(4) + .ok_or(Error::InvalidArgument(format!( + "[get_change_output_and_fees] error: failed to calculate payout script pubkey weight" + )))?; + let total_cet_weight = checked_add!(this_party_cet_base_weight, output_spk_weight)?; + let cet_or_refund_fee = util::weight_to_fee(total_cet_weight, fee_rate_per_vb)?; + let required_input_funds: u64 = if self.collateral == 0 { + 0 + } else { + checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)? + }; + + if self.input_amount < required_input_funds { + return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); + } + + let change_output = TxOut { + value: self.input_amount - required_input_funds, + script_pubkey: self.change_script_pubkey.clone(), }; - Ok((change_output, fund_fee, cet_or_refund_fee)) + if fee_rate_per_vb == 0 { + Ok((change_output, 0, 0)) + } else { + Ok((change_output, fund_fee, cet_or_refund_fee)) + } } fn get_unsigned_tx_inputs_and_serial_ids(&self, sequence: Sequence) -> (Vec, Vec) { @@ -428,7 +420,7 @@ pub(crate) fn create_fund_transaction_with_fees( let total_collateral = checked_add!(offer_params.collateral, accept_params.collateral)?; let (offer_change_output, offer_fund_fee, offer_cet_fee) = - offer_params.get_change_output_and_fees(fee_rate_per_vb, extra_fee)?; + offer_params.get_change_output_and_fees(0, extra_fee)?; let (accept_change_output, accept_fund_fee, accept_cet_fee) = accept_params.get_change_output_and_fees(fee_rate_per_vb, extra_fee)?; @@ -439,6 +431,8 @@ pub(crate) fn create_fund_transaction_with_fees( - accept_fund_fee - extra_fee; + log_message("fund_output_value:", fund_output_value); + assert_eq!( total_collateral + offer_cet_fee + accept_cet_fee + extra_fee, fund_output_value From 1f6c27d420287c8916f86325da7767f1165d180f Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 14:58:01 +0200 Subject: [PATCH 3/8] feat: added logs --- dlc/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dlc/src/lib.rs b/dlc/src/lib.rs index c8817d83..b1babcc8 100644 --- a/dlc/src/lib.rs +++ b/dlc/src/lib.rs @@ -277,6 +277,8 @@ impl PartyParams { ) -> Result<(TxOut, u64, u64), Error> { let mut inputs_weight: usize = 0; + println!("fee_rate_per_vb: {}", fee_rate_per_vb); + for w in &self.inputs { let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) .len() @@ -311,6 +313,8 @@ impl PartyParams { )?; let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; + println!("fund_fee: {}", fund_fee); + // Base weight (nLocktime, nVersion, funding input ...) is distributed // among parties independently of output types let this_party_cet_base_weight = CET_BASE_WEIGHT; @@ -331,6 +335,9 @@ impl PartyParams { checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)? }; + println!("required_input_funds: {}", required_input_funds); + println!("input_amount: {}", self.input_amount); + if self.input_amount < required_input_funds { return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); } @@ -341,6 +348,7 @@ impl PartyParams { }; if fee_rate_per_vb == 0 { + println!("fee_rate_per_vb is 0, returning 0 fees"); Ok((change_output, 0, 0)) } else { Ok((change_output, fund_fee, cet_or_refund_fee)) From 9f7ee0fafb6c1af039934738344173961f94c74f Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 15:14:46 +0200 Subject: [PATCH 4/8] feat: modified logging for wasm --- dlc/src/lib.rs | 15 ++++++++------- dlc/src/macros.rs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 dlc/src/macros.rs diff --git a/dlc/src/lib.rs b/dlc/src/lib.rs index b1babcc8..daf34a9a 100644 --- a/dlc/src/lib.rs +++ b/dlc/src/lib.rs @@ -38,6 +38,9 @@ use secp256k1_zkp::{ use serde::{Deserialize, Serialize}; use std::fmt; +#[macro_use] +mod macros; + pub mod channel; pub mod secp_utils; pub mod util; @@ -277,7 +280,7 @@ impl PartyParams { ) -> Result<(TxOut, u64, u64), Error> { let mut inputs_weight: usize = 0; - println!("fee_rate_per_vb: {}", fee_rate_per_vb); + log_to_console!("fee_rate_per_vb: {}", fee_rate_per_vb); for w in &self.inputs { let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) @@ -313,7 +316,7 @@ impl PartyParams { )?; let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; - println!("fund_fee: {}", fund_fee); + log_to_console!("fund_fee: {}", fund_fee); // Base weight (nLocktime, nVersion, funding input ...) is distributed // among parties independently of output types @@ -335,8 +338,8 @@ impl PartyParams { checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)? }; - println!("required_input_funds: {}", required_input_funds); - println!("input_amount: {}", self.input_amount); + log_to_console!("required_input_funds: {}", required_input_funds); + log_to_console!("input_amount: {}", self.input_amount); if self.input_amount < required_input_funds { return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); @@ -348,7 +351,7 @@ impl PartyParams { }; if fee_rate_per_vb == 0 { - println!("fee_rate_per_vb is 0, returning 0 fees"); + log_to_console!("fee_rate_per_vb is 0, returning 0 fees"); Ok((change_output, 0, 0)) } else { Ok((change_output, fund_fee, cet_or_refund_fee)) @@ -439,8 +442,6 @@ pub(crate) fn create_fund_transaction_with_fees( - accept_fund_fee - extra_fee; - log_message("fund_output_value:", fund_output_value); - assert_eq!( total_collateral + offer_cet_fee + accept_cet_fee + extra_fee, fund_output_value diff --git a/dlc/src/macros.rs b/dlc/src/macros.rs new file mode 100644 index 00000000..e9287491 --- /dev/null +++ b/dlc/src/macros.rs @@ -0,0 +1,16 @@ +#[macro_export] +macro_rules! clog { + ( $( $t:tt )* ) => { + web_sys::console::log_1(&format!( $( $t )* ).into()); + } +} + +#[macro_export] +macro_rules! log_to_console { + ( $( $t:tt )* ) => { + #[cfg(target_arch = "wasm32")] + $crate::clog!( $( $t )* ); + #[cfg(not(target_arch = "wasm32"))] + println!( $( $t )* ); + } +} From 594d97f2f66f985fd4400f0b61a1370ca44519da Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 15:15:59 +0200 Subject: [PATCH 5/8] feat: added documentation to macros --- dlc/src/macros.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dlc/src/macros.rs b/dlc/src/macros.rs index e9287491..e26acc0d 100644 --- a/dlc/src/macros.rs +++ b/dlc/src/macros.rs @@ -1,3 +1,6 @@ +/// This macro logs a message to the browser console using the `web_sys::console::log_1` function. +/// It takes any number of arguments and formats them into a string using the `format!` macro. +/// The resulting string is then passed to `web_sys::console::log_1` as an argument. #[macro_export] macro_rules! clog { ( $( $t:tt )* ) => { @@ -5,6 +8,12 @@ macro_rules! clog { } } +/// This macro is a wrapper around the `clog!` and `println!` macros. +/// It logs a message to the console using `clog!` if the target architecture is `wasm32`, +/// or prints the message to the standard output using `println!` otherwise. +/// It takes any number of arguments and formats them into a string using the `format!` macro. +/// The resulting string is then passed to either `clog!` or `println!` as an argument, +/// depending on the target architecture. #[macro_export] macro_rules! log_to_console { ( $( $t:tt )* ) => { From 818abccc851b3c685f348e2a6e2caac4ead7bca6 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 17:27:40 +0200 Subject: [PATCH 6/8] feat: added web-sys dependency --- dlc/Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dlc/Cargo.toml b/dlc/Cargo.toml index a6c5cb15..6b4bf790 100644 --- a/dlc/Cargo.toml +++ b/dlc/Cargo.toml @@ -27,3 +27,7 @@ bitcoincore-rpc-json = {version = "0.16.0" } dlc-trie = {path = "../dlc-trie"} rayon = "1.5" secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std", "serde", "global-context"]} + +[dependencies.web-sys] +version = "0.3" +features = ["console"] From a9fac429403b9911d0627641c7f30192ceaa9562 Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Sep 2023 17:31:23 +0200 Subject: [PATCH 7/8] feat: removed logs --- dlc/Cargo.toml | 4 ---- dlc/src/lib.rs | 11 ----------- dlc/src/macros.rs | 25 ------------------------- 3 files changed, 40 deletions(-) delete mode 100644 dlc/src/macros.rs diff --git a/dlc/Cargo.toml b/dlc/Cargo.toml index 6b4bf790..a6c5cb15 100644 --- a/dlc/Cargo.toml +++ b/dlc/Cargo.toml @@ -27,7 +27,3 @@ bitcoincore-rpc-json = {version = "0.16.0" } dlc-trie = {path = "../dlc-trie"} rayon = "1.5" secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std", "serde", "global-context"]} - -[dependencies.web-sys] -version = "0.3" -features = ["console"] diff --git a/dlc/src/lib.rs b/dlc/src/lib.rs index daf34a9a..9130307c 100644 --- a/dlc/src/lib.rs +++ b/dlc/src/lib.rs @@ -38,9 +38,6 @@ use secp256k1_zkp::{ use serde::{Deserialize, Serialize}; use std::fmt; -#[macro_use] -mod macros; - pub mod channel; pub mod secp_utils; pub mod util; @@ -280,8 +277,6 @@ impl PartyParams { ) -> Result<(TxOut, u64, u64), Error> { let mut inputs_weight: usize = 0; - log_to_console!("fee_rate_per_vb: {}", fee_rate_per_vb); - for w in &self.inputs { let script_weight = util::redeem_script_to_script_sig(&w.redeem_script) .len() @@ -316,8 +311,6 @@ impl PartyParams { )?; let fund_fee = util::weight_to_fee(total_fund_weight, fee_rate_per_vb)?; - log_to_console!("fund_fee: {}", fund_fee); - // Base weight (nLocktime, nVersion, funding input ...) is distributed // among parties independently of output types let this_party_cet_base_weight = CET_BASE_WEIGHT; @@ -338,9 +331,6 @@ impl PartyParams { checked_add!(self.collateral, fund_fee, cet_or_refund_fee, extra_fee)? }; - log_to_console!("required_input_funds: {}", required_input_funds); - log_to_console!("input_amount: {}", self.input_amount); - if self.input_amount < required_input_funds { return Err(Error::InvalidArgument(format!("[get_change_output_and_fees] error: input amount is lower than the sum of the collateral plus the required fees => input_amount: {}, collateral: {}, fund fee: {}, cet_or_refund_fee: {}, extra_fee: {}", self.input_amount, self.collateral, fund_fee, cet_or_refund_fee, extra_fee))); } @@ -351,7 +341,6 @@ impl PartyParams { }; if fee_rate_per_vb == 0 { - log_to_console!("fee_rate_per_vb is 0, returning 0 fees"); Ok((change_output, 0, 0)) } else { Ok((change_output, fund_fee, cet_or_refund_fee)) diff --git a/dlc/src/macros.rs b/dlc/src/macros.rs deleted file mode 100644 index e26acc0d..00000000 --- a/dlc/src/macros.rs +++ /dev/null @@ -1,25 +0,0 @@ -/// This macro logs a message to the browser console using the `web_sys::console::log_1` function. -/// It takes any number of arguments and formats them into a string using the `format!` macro. -/// The resulting string is then passed to `web_sys::console::log_1` as an argument. -#[macro_export] -macro_rules! clog { - ( $( $t:tt )* ) => { - web_sys::console::log_1(&format!( $( $t )* ).into()); - } -} - -/// This macro is a wrapper around the `clog!` and `println!` macros. -/// It logs a message to the console using `clog!` if the target architecture is `wasm32`, -/// or prints the message to the standard output using `println!` otherwise. -/// It takes any number of arguments and formats them into a string using the `format!` macro. -/// The resulting string is then passed to either `clog!` or `println!` as an argument, -/// depending on the target architecture. -#[macro_export] -macro_rules! log_to_console { - ( $( $t:tt )* ) => { - #[cfg(target_arch = "wasm32")] - $crate::clog!( $( $t )* ); - #[cfg(not(target_arch = "wasm32"))] - println!( $( $t )* ); - } -} From 539ce6752bc66ff8b8a4c6866c681a49bf28556e Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Mon, 18 Sep 2023 16:12:33 +0200 Subject: [PATCH 8/8] feat: changed get_change_output calls --- dlc-manager/src/utils.rs | 22 +++++++++++++--------- dlc/src/lib.rs | 9 +++++++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/dlc-manager/src/utils.rs b/dlc-manager/src/utils.rs index 6066a764..8db7e56c 100644 --- a/dlc-manager/src/utils.rs +++ b/dlc-manager/src/utils.rs @@ -2,7 +2,7 @@ use std::ops::Deref; use bitcoin::{consensus::Encodable, Txid}; -use dlc::{PartyParams, TxInputInfo}; +use dlc::{util::get_common_fee, PartyParams, TxInputInfo}; use dlc_messages::{ oracle_msgs::{OracleAnnouncement, OracleAttestation}, FundingInput, @@ -81,9 +81,18 @@ where let change_serial_id = get_new_serial_id(); // Add base cost of fund tx + CET / 2 and a CET output to the collateral. - let appr_required_amount = - own_collateral + get_half_common_fee(fee_rate)? + dlc::util::weight_to_fee(124, fee_rate)?; - let utxos = wallet.get_utxos_for_amount(appr_required_amount, Some(fee_rate), true)?; + + let utxos = match own_collateral { + 0 => Vec::new(), + _ => { + let common_fee = get_common_fee(fee_rate)?; + let total_fee = common_fee + own_collateral; + let appr_required_amount = + own_collateral + total_fee + dlc::util::weight_to_fee(124, fee_rate)?; + let utxos = wallet.get_utxos_for_amount(appr_required_amount, Some(fee_rate), true)?; + utxos + } + }; let mut funding_inputs_info: Vec = Vec::new(); let mut funding_tx_info: Vec = Vec::new(); @@ -141,11 +150,6 @@ where }) } -pub(crate) fn get_half_common_fee(fee_rate: u64) -> Result { - let common_fee = dlc::util::get_common_fee(fee_rate)?; - Ok((common_fee as f64 / 2_f64).ceil() as u64) -} - pub(crate) fn get_range_info_and_oracle_sigs( contract_info: &ContractInfo, adaptor_info: &AdaptorInfo, diff --git a/dlc/src/lib.rs b/dlc/src/lib.rs index 9130307c..073af1e9 100644 --- a/dlc/src/lib.rs +++ b/dlc/src/lib.rs @@ -419,8 +419,13 @@ pub(crate) fn create_fund_transaction_with_fees( ) -> Result<(Transaction, Script), Error> { let total_collateral = checked_add!(offer_params.collateral, accept_params.collateral)?; - let (offer_change_output, offer_fund_fee, offer_cet_fee) = - offer_params.get_change_output_and_fees(0, extra_fee)?; + let offer_change_output = TxOut { + value: 0, + script_pubkey: offer_params.change_script_pubkey.clone(), + }; + let offer_fund_fee = 0; + let offer_cet_fee = 0; + let (accept_change_output, accept_fund_fee, accept_cet_fee) = accept_params.get_change_output_and_fees(fee_rate_per_vb, extra_fee)?;