Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get_change_output_and_fees match collateral value #5

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions dlc-manager/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<FundingInputInfo> = Vec::new();
let mut funding_tx_info: Vec<TxInputInfo> = Vec::new();
Expand Down Expand Up @@ -141,11 +150,6 @@ where
})
}

pub(crate) fn get_half_common_fee(fee_rate: u64) -> Result<u64, Error> {
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,
Expand Down
27 changes: 20 additions & 7 deletions dlc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ impl PartyParams {

// Base weight (nLocktime, nVersion, ...) is distributed among parties
// independently of inputs contributed
let this_party_fund_base_weight = FUND_TX_BASE_WEIGHT / 2;
let this_party_fund_base_weight = FUND_TX_BASE_WEIGHT;

let total_fund_weight = checked_add!(
this_party_fund_base_weight,
Expand All @@ -313,7 +313,7 @@ impl PartyParams {

// Base weight (nLocktime, nVersion, funding input ...) is distributed
// among parties independently of output types
let this_party_cet_base_weight = CET_BASE_WEIGHT / 2;
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 =
Expand All @@ -325,8 +325,12 @@ impl PartyParams {
)))?;
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)?;
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)));
}
Expand All @@ -336,7 +340,11 @@ impl PartyParams {
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<TxIn>, Vec<u64>) {
Expand Down Expand Up @@ -411,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(fee_rate_per_vb, 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)?;

Expand Down