diff --git a/mm2src/coins/lp_coins.rs b/mm2src/coins/lp_coins.rs index b9a2fa935e..816e9ca98a 100644 --- a/mm2src/coins/lp_coins.rs +++ b/mm2src/coins/lp_coins.rs @@ -1681,10 +1681,10 @@ pub trait MakerCoinSwapOpsV2: ParseCoinAssocTypes + Send + Sync + 'static { async fn spend_maker_payment_v2(&self, args: SpendMakerPaymentArgs<'_, Self>) -> Result; /// Get the fee to be paid for the processing of the maker payment transaction. - async fn get_maker_payment_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult; + async fn get_maker_payment_fee(&self, value: TradePreimageValue) -> TradePreimageResult; /// Get the fee to be paid for the processing of the maker payment spend transaction aka the claiming transaction. - async fn get_maker_payment_spend_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult; + async fn get_maker_payment_spend_fee(&self) -> TradePreimageResult; } #[async_trait] @@ -1890,13 +1890,13 @@ pub trait TakerCoinSwapOpsV2: ParseCoinAssocTypes + Send + Sync + 'static { /// Txs in success case (no refund) go as follows: /// Chain A: funding -> taker payment -> taker payment spend (aka preimage) (to the maker & dex) /// Chain B: maker payment -> maker payment spend (aka claiming) (to the taker) - async fn get_funding_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult; + async fn get_funding_fee(&self, value: TradePreimageValue) -> TradePreimageResult; /// Get the fee to be paid for the processing of the taker payment transaction. - async fn get_taker_payment_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult; + async fn get_taker_payment_fee(&self) -> TradePreimageResult; /// Get the fee to be paid for the processing of the taker payment spend transaction aka the preimage transaction. - async fn get_taker_payment_spend_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult; + async fn get_taker_payment_spend_fee(&self) -> TradePreimageResult; } /// Operations that coins have independently from the MarketMaker. diff --git a/mm2src/coins/test_coin.rs b/mm2src/coins/test_coin.rs index 56250ae24e..9e1058129d 100644 --- a/mm2src/coins/test_coin.rs +++ b/mm2src/coins/test_coin.rs @@ -558,9 +558,9 @@ impl TakerCoinSwapOpsV2 for TestCoin { fn derive_htlc_pubkey_v2(&self, swap_unique_data: &[u8]) -> Self::Pubkey { todo!() } - async fn get_funding_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { todo!() } + async fn get_funding_fee(&self, value: TradePreimageValue) -> TradePreimageResult { todo!() } - async fn get_taker_payment_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { todo!() } + async fn get_taker_payment_fee(&self) -> TradePreimageResult { todo!() } - async fn get_taker_payment_spend_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { todo!() } + async fn get_taker_payment_spend_fee(&self) -> TradePreimageResult { todo!() } } diff --git a/mm2src/coins/utxo/utxo_common.rs b/mm2src/coins/utxo/utxo_common.rs index c095cf446d..96bf248830 100644 --- a/mm2src/coins/utxo/utxo_common.rs +++ b/mm2src/coins/utxo/utxo_common.rs @@ -884,26 +884,8 @@ enum FundingSpendFeeSetting { pub mod tx_sizes { use super::*; - /// Returns the taker funding transaction size in vbytes. - pub async fn get_funding_tx_size(_coin: &impl UtxoCommonOps) -> usize { - // This depends on what coins being spent so might be a bit clumsy. - // Since this is the first tx anyway, we won't need a utxo locking mechanism - // to prevent certain utxos from being spent in other swaps happening in parallel. - // We just need to make sure that we deliver the correct trading volume and not less. - - // FIXME: Inspect `get_sender_trade_fee`, but I don't think it nails it correctly. - todo!() - } - - /// Returns the maker payment transaction size in vbytes. - pub async fn get_maker_payment_tx_size(coin: &impl UtxoCommonOps) -> usize { - // Maker payment is similar to funding tx in that it spends coins directly from the user's - // wallet and sends them to P2SH address. - get_funding_tx_size(coin).await - } - /// Returns the taker payment transaction size in vbytes. - pub async fn get_taker_payment_tx_size(coin: &impl UtxoCommonOps) -> usize { + pub fn get_taker_payment_tx_size(coin: &impl UtxoCommonOps) -> usize { let preimage = TransactionInputSigner { lock_time: 0, version: coin.as_ref().conf.tx_version, @@ -961,7 +943,7 @@ pub mod tx_sizes { } /// Returns the taker payment spend transaction size in vbytes. - pub async fn get_taker_payment_spend_tx_size(coin: &impl UtxoCommonOps) -> usize { + pub fn get_taker_payment_spend_tx_size(coin: &impl UtxoCommonOps) -> usize { let preimage = TransactionInputSigner { lock_time: 0, version: coin.as_ref().conf.tx_version, @@ -1022,7 +1004,7 @@ pub mod tx_sizes { } /// Returns the maker payment spend transaction size in vbytes. - pub async fn get_maker_payment_spend_tx_size(coin: &impl UtxoCommonOps) -> usize { + pub fn get_maker_payment_spend_tx_size(coin: &impl UtxoCommonOps) -> usize { let preimage = TransactionInputSigner { lock_time: 0, version: coin.as_ref().conf.tx_version, @@ -1213,12 +1195,7 @@ where let fee = match fee { FundingSpendFeeSetting::GetFromCoin => { - let calculated_fee = coin - .get_taker_payment_fee(&FeeApproxStage::WithoutApprox) - .await - .unwrap() - .amount - .to_decimal(); + let calculated_fee = coin.get_taker_payment_fee().await.unwrap().amount.to_decimal(); let calculated_fee = sat_from_big_decimal(&calculated_fee, coin.as_ref().decimals).mm_err(|e| e.into())?; let taker_instructed_fee = sat_from_big_decimal(&args.taker_payment_fee, coin.as_ref().decimals).mm_err(|e| e.into())?; diff --git a/mm2src/coins/utxo/utxo_standard.rs b/mm2src/coins/utxo/utxo_standard.rs index 6086728af4..f058df72ca 100644 --- a/mm2src/coins/utxo/utxo_standard.rs +++ b/mm2src/coins/utxo/utxo_standard.rs @@ -651,21 +651,16 @@ impl MakerCoinSwapOpsV2 for UtxoStandardCoin { utxo_common::spend_maker_payment_v2(self, args).await } - async fn get_maker_payment_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { - let maker_payment_tx_size = utxo_common::tx_sizes::get_maker_payment_tx_size(self).await; - let fee_sat = self.get_htlc_spend_fee(maker_payment_tx_size as u64, stage).await?; - let amount = big_decimal_from_sat_unsigned(fee_sat, self.as_ref().decimals).into(); - Ok(TradeFee { - coin: self.as_ref().conf.ticker.clone(), - amount, - paid_from_trading_vol: true, - }) + async fn get_maker_payment_fee(&self, value: TradePreimageValue) -> TradePreimageResult { + let mut fee = utxo_common::get_sender_trade_fee(self, value, FeeApproxStage::StartSwap).await?; + fee.paid_from_trading_vol = true; + Ok(fee) } - async fn get_maker_payment_spend_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { - let maker_payment_spend_tx_size = utxo_common::tx_sizes::get_maker_payment_spend_tx_size(self).await; + async fn get_maker_payment_spend_fee(&self) -> TradePreimageResult { + let maker_payment_spend_tx_size = utxo_common::tx_sizes::get_maker_payment_spend_tx_size(self); let fee_sat = self - .get_htlc_spend_fee(maker_payment_spend_tx_size as u64, stage) + .get_htlc_spend_fee(maker_payment_spend_tx_size as u64, &FeeApproxStage::TradePreimage) .await?; let amount = big_decimal_from_sat_unsigned(fee_sat, self.as_ref().decimals).into(); Ok(TradeFee { @@ -852,20 +847,17 @@ impl TakerCoinSwapOpsV2 for UtxoStandardCoin { *self.derive_htlc_key_pair(swap_unique_data).public() } - async fn get_funding_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { - let funding_tx_size = utxo_common::tx_sizes::get_funding_tx_size(self).await; - let fee_sat = self.get_htlc_spend_fee(funding_tx_size as u64, stage).await?; - let amount = big_decimal_from_sat_unsigned(fee_sat, self.as_ref().decimals).into(); - Ok(TradeFee { - coin: self.as_ref().conf.ticker.clone(), - amount, - paid_from_trading_vol: true, - }) + async fn get_funding_fee(&self, value: TradePreimageValue) -> TradePreimageResult { + let mut fee = utxo_common::get_sender_trade_fee(self, value, FeeApproxStage::StartSwap).await?; + fee.paid_from_trading_vol = true; + Ok(fee) } - async fn get_taker_payment_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { - let taker_payment_tx_size = utxo_common::tx_sizes::get_taker_payment_tx_size(self).await; - let fee_sat = self.get_htlc_spend_fee(taker_payment_tx_size as u64, stage).await?; + async fn get_taker_payment_fee(&self) -> TradePreimageResult { + let taker_payment_tx_size = utxo_common::tx_sizes::get_taker_payment_tx_size(self); + let fee_sat = self + .get_htlc_spend_fee(taker_payment_tx_size as u64, &FeeApproxStage::TradePreimage) + .await?; let amount = big_decimal_from_sat_unsigned(fee_sat, self.as_ref().decimals).into(); Ok(TradeFee { coin: self.as_ref().conf.ticker.clone(), @@ -874,10 +866,10 @@ impl TakerCoinSwapOpsV2 for UtxoStandardCoin { }) } - async fn get_taker_payment_spend_fee(&self, stage: &FeeApproxStage) -> TradePreimageResult { - let taker_payment_spend_tx_size = utxo_common::tx_sizes::get_taker_payment_spend_tx_size(self).await; + async fn get_taker_payment_spend_fee(&self) -> TradePreimageResult { + let taker_payment_spend_tx_size = utxo_common::tx_sizes::get_taker_payment_spend_tx_size(self); let fee_sat = self - .get_htlc_spend_fee(taker_payment_spend_tx_size as u64, stage) + .get_htlc_spend_fee(taker_payment_spend_tx_size as u64, &FeeApproxStage::TradePreimage) .await?; let amount = big_decimal_from_sat_unsigned(fee_sat, self.as_ref().decimals).into(); Ok(TradeFee { diff --git a/mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs b/mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs index cbeacb9ce3..235500966f 100644 --- a/mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs +++ b/mm2src/mm2_main/src/lp_swap/maker_swap_v2.rs @@ -3,9 +3,9 @@ use super::{swap_v2_topic, LockedAmount, LockedAmountInfo, SavedTradeFee, SwapsC NEGOTIATION_TIMEOUT_SEC}; use crate::mm2::lp_swap::maker_swap::MakerSwapPreparedParams; use crate::mm2::lp_swap::swap_lock::SwapLock; +use crate::mm2::lp_swap::swap_v2_pb::*; use crate::mm2::lp_swap::{broadcast_swap_v2_msg_every, check_balance_for_maker_swap, recv_swap_v2_msg, SecretHashAlgo, SwapConfirmationsSettings, TransactionIdentifier, MAKER_SWAP_V2_TYPE, MAX_STARTED_AT_DIFF}; -use crate::mm2::lp_swap::{swap_v2_pb::*, NO_REFUND_FEE}; use async_trait::async_trait; use bitcrypto::{dhash160, sha256}; use coins::utxo::utxo_common::big_decimal_from_sat_unsigned; @@ -799,12 +799,7 @@ impl fee, Err(e) => { let reason = AbortReason::FailedToGetMakerPaymentFee(e.to_string()); @@ -812,10 +807,10 @@ impl fee, Err(e) => { - let reason = AbortReason::FailedToGetTakerPaymentSpendFee(e.to_string()); + let reason = AbortReason::FailedToGetTakerPaymentFee(e.to_string()); return Self::change_state(Aborted::new(reason), state_machine).await; }, }; @@ -832,7 +827,7 @@ impl { diff --git a/mm2src/mm2_main/src/lp_swap/taker_swap_v2.rs b/mm2src/mm2_main/src/lp_swap/taker_swap_v2.rs index 6d491e74fd..3fe33ae28b 100644 --- a/mm2src/mm2_main/src/lp_swap/taker_swap_v2.rs +++ b/mm2src/mm2_main/src/lp_swap/taker_swap_v2.rs @@ -2,10 +2,10 @@ use super::swap_v2_common::*; use super::{LockedAmount, LockedAmountInfo, SavedTradeFee, SwapsContext, TakerSwapPreparedParams, NEGOTIATE_SEND_INTERVAL, NEGOTIATION_TIMEOUT_SEC}; use crate::mm2::lp_swap::swap_lock::SwapLock; +use crate::mm2::lp_swap::swap_v2_pb::*; use crate::mm2::lp_swap::{broadcast_swap_v2_msg_every, check_balance_for_taker_swap, recv_swap_v2_msg, swap_v2_topic, SecretHashAlgo, SwapConfirmationsSettings, TransactionIdentifier, MAX_STARTED_AT_DIFF, TAKER_SWAP_V2_TYPE}; -use crate::mm2::lp_swap::{swap_v2_pb::*, NO_REFUND_FEE}; use async_trait::async_trait; use bitcrypto::{dhash160, sha256}; use coins::utxo::sat_from_big_decimal; @@ -940,11 +940,9 @@ impl fee, Err(e) => { let reason = AbortReason::FailedToGetTakerPaymentSpendFee(e.to_string()); @@ -952,16 +950,12 @@ impl fee, Err(e) => { let reason = AbortReason::FailedToGetTakerPaymentFee(e.to_string()); @@ -969,7 +963,7 @@ impl fee, Err(e) => { let reason = AbortReason::FailedToGetMakerPaymentSpendFee(e.to_string()); @@ -999,7 +993,7 @@ impl