Skip to content

Commit

Permalink
add swap feature deactivation by version
Browse files Browse the repository at this point in the history
  • Loading branch information
dimxy committed Nov 7, 2024
1 parent 601519d commit 05feb72
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
25 changes: 18 additions & 7 deletions mm2src/coins/swap_features.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
/// Framework to activate new swap protocol features at certain protocol version

#[derive(PartialEq)]
pub enum SwapFeature {
pub enum LegacySwapFeature {
// Sending part of dex fee to a dedicated account to exchange it on KMD coins and burn them
SendToPreBurnAccount,
}

impl SwapFeature {
// add new features to activate
const SWAP_FEATURE_ACTIVATION: &[(u16, SwapFeature)] = &[(1, SwapFeature::SendToPreBurnAccount)];
impl LegacySwapFeature {
/// Features activated/deactivated by protocol version. Tuple elements are:
/// element.0 is version when the feature is activated,
/// element.1 is version when the feature is discontinued,
/// element.2 is a feature itself, registered in the LegacySwapFeature enum
const SWAP_FEATURE_ACTIVATION: &[(u16, Option<u16>, LegacySwapFeature)] = &[
(1, None, LegacySwapFeature::SendToPreBurnAccount),
// add more features to activate...
];

pub fn is_active(feature: SwapFeature, version: u16) -> bool {
if let Some(found) = Self::SWAP_FEATURE_ACTIVATION.iter().find(|fv| fv.1 == feature) {
return version >= found.0;
/// Returns true if feature is active for the protocol version param
pub fn is_active(feature: LegacySwapFeature, version: u16) -> bool {
if let Some(found) = Self::SWAP_FEATURE_ACTIVATION.iter().find(|fv| fv.2 == feature) {
return version >= found.0
&& found
.1
.map(|ver_discontinued| version < ver_discontinued)
.unwrap_or(true);
}
false
}
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::lp_swap::{broadcast_swap_message, swap_ext_topic, taker_payment_spend
MAX_STARTED_AT_DIFF};
use crate::lp_swap::{NegotiationDataMsgVersion, SwapMsgExt};
use coins::lp_price::fetch_swap_coins_price;
use coins::swap_features::SwapFeature;
use coins::swap_features::LegacySwapFeature;
use coins::SWAP_PROTOCOL_VERSION;
#[cfg(feature = "run-docker-tests")]
use coins::TEST_DEX_FEE_ADDR_RAW_PUBKEY;
Expand Down Expand Up @@ -803,7 +803,7 @@ impl MakerSwap {
.data
.taker_version
.ok_or("No swap protocol version".to_owned())?;
let is_burn_active = SwapFeature::is_active(SwapFeature::SendToPreBurnAccount, remote_version);
let is_burn_active = LegacySwapFeature::is_active(LegacySwapFeature::SendToPreBurnAccount, remote_version);
let dex_fee = dex_fee_from_taker_coin(
self.taker_coin.deref(),
&self.r().data.maker_coin,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::lp_swap::{broadcast_p2p_tx_msg, broadcast_swap_msg_every_delayed, swa
wait_for_maker_payment_conf_duration, SwapMsgWrapper, TakerSwapWatcherData, MAX_STARTED_AT_DIFF};
use crate::lp_swap::{NegotiationDataMsgVersion, SwapMsgExt};
use coins::lp_price::fetch_swap_coins_price;
use coins::swap_features::SwapFeature;
use coins::swap_features::LegacySwapFeature;
use coins::SWAP_PROTOCOL_VERSION;
#[cfg(feature = "run-docker-tests")]
use coins::TEST_DEX_FEE_ADDR_RAW_PUBKEY;
Expand Down Expand Up @@ -1375,7 +1375,7 @@ impl TakerSwap {
.data
.maker_version
.ok_or("No swap protocol version".to_owned())?;
let is_burn_active = SwapFeature::is_active(SwapFeature::SendToPreBurnAccount, remote_version);
let is_burn_active = LegacySwapFeature::is_active(LegacySwapFeature::SendToPreBurnAccount, remote_version);
let dex_fee = dex_fee_from_taker_coin(
self.taker_coin.deref(),
&self.r().data.maker_coin,
Expand Down

0 comments on commit 05feb72

Please sign in to comment.