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(swap): implement 0 dexfee for kmd trading pairs #2323

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from

Conversation

borngraced
Copy link
Member

@borngraced borngraced commented Jan 16, 2025

@borngraced borngraced self-assigned this Jan 16, 2025
@borngraced borngraced changed the title feat:(swap) implement 0 dexfee for kmd trading pairs feat(swap) implement 0 dexfee for kmd trading pairs Jan 16, 2025
@borngraced borngraced changed the title feat(swap) implement 0 dexfee for kmd trading pairs feat(swap): implement 0 dexfee for kmd trading pairs Jan 16, 2025
Comment on lines 5894 to 5898
let fee_coin = match &self.coin_type {
EthCoinType::Eth => self.ticker.to_owned(),
EthCoinType::Erc20 { platform, .. } => platform.to_owned(),
EthCoinType::Nft { .. } => return MmError::err(TradePreimageError::NftProtocolNotSupported),
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use platform_ticker instead. I am not sure if you intentionally drop the nft support, but if we want to do that we need to add a if check before using platform_ticker.

Comment on lines 3684 to 3685
/// Check and return true if DexFee is not required to trade otherwise return false.
pub fn no_fee(&self) -> bool { matches!(self, Self::Zero) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Check and return true if DexFee is not required to trade otherwise return false.
pub fn no_fee(&self) -> bool { matches!(self, Self::Zero) }
/// Check and return true if DexFee is not required to trade otherwise return false.
pub fn zero_fee(&self) -> bool { matches!(self, Self::Zero) }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous notes are also still valid

@@ -1844,6 +1844,14 @@ impl MmCoin for SlpToken {
dex_fee_amount: DexFee,
stage: FeeApproxStage,
) -> TradePreimageResult<TradeFee> {
if dex_fee_amount.no_fee() {
return Ok(TradeFee {
coin: self.platform_coin.ticker().into(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
coin: self.platform_coin.ticker().into(),
coin: self.platform_coin.ticker().to_owned(),

@@ -739,6 +739,15 @@ impl MakerSwap {
};
swap_events.push(MakerSwapEvent::MakerPaymentInstructionsReceived(instructions));

let taker_amount = MmNumber::from(self.taker_amount.clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MmNumber::from only takes a reference inside the function. You can implement:

impl From<&BigDecimal> for MmNumber {
    fn from(n: &BigDecimal) -> MmNumber { from_dec_to_ratio(n).into() }
}

to avoid unnecessary cloning in cases like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we have such issue
#2338

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borngraced could you please introduce From<&BigDecimal> for MmNumber and start using it for your changes. after pr merge someone can take refactoring as a separate task.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@laruh
Copy link
Member

laruh commented Feb 19, 2025

@borngraced could you tell why status is blocked? or was it misclick?

onur-ozkan
onur-ozkan previously approved these changes Feb 19, 2025
Copy link
Member

@onur-ozkan onur-ozkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple of non-blocker tiny nits, thanks for the fixes!

LGTM

@@ -3676,6 +3680,9 @@ impl DexFee {
Ok(None)
}
}

/// Check and return true if DexFee is not required to trade otherwise return false.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit:

Suggested change
/// Check and return true if DexFee is not required to trade otherwise return false.
/// Whether this is DexFee::Zero.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 619 to 621
let is_kmd = "KMD" == recreate_ctx.maker_coin.ticker() || "KMD" == recreate_ctx.taker_coin.ticker();
let dex_fee = if is_kmd {
DexFee::Zero
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let is_kmd = "KMD" == recreate_ctx.maker_coin.ticker() || "KMD" == recreate_ctx.taker_coin.ticker();
let dex_fee = if is_kmd {
DexFee::Zero
let dex_fee = if "KMD" == recreate_ctx.maker_coin.ticker() || "KMD" == recreate_ctx.taker_coin.ticker() {
DexFee::Zero

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 735 to 737
let is_kmd = "KMD" == recreate_ctx.maker_coin.ticker() || "KMD" == recreate_ctx.taker_coin.ticker();
let dex_fee = if is_kmd {
DexFee::Zero
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let is_kmd = "KMD" == recreate_ctx.maker_coin.ticker() || "KMD" == recreate_ctx.taker_coin.ticker();
let dex_fee = if is_kmd {
DexFee::Zero
let dex_fee = if "KMD" == recreate_ctx.maker_coin.ticker() || "KMD" == recreate_ctx.taker_coin.ticker() {
DexFee::Zero

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@borngraced
Copy link
Member Author

borngraced commented Feb 19, 2025

@borngraced could you tell why status is blocked? or was it misclick?

this #2112 seems related and not sure which one will be merge first as both have some similar changes

laruh
laruh previously approved these changes Feb 19, 2025
Copy link
Member

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@smk762 smk762 self-requested a review February 21, 2025 09:15
Copy link

@smk762 smk762 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed in swap json.

Before:

       "fee_to_send_taker_fee": {
            "coin": "KMD",
            "amount": "0.00001",
            "paid_from_trading_vol": false
        },
 ...

        {
            "timestamp": 1732532884206,
            "event": {
                "type": "TakerFeeSent",
                "data": {
                    "tx_hex": "040...000",
                    "tx_hash": "fa...b5"
                }
            }
        },

After:

        "fee_to_send_taker_fee": {
            "coin": "KMD",
            "amount": "0",
            "paid_from_trading_vol": false
        },
...       
        {
            "timestamp": 1740128668082,
            "event": {
                "type": "TakerFeeSent",
                "data": {
                    "tx_hex": "",
                    "tx_hash": ""
                }
            }
        }
```        

Copy link

@smk762 smk762 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subsequent to prior comment, all swaps attempted selling KMD with this binary have failed, so approval rescinded for now until further testing can confirm cause.

In all cases, the error was taker_swap:1401] Error waiting for 'maker-payment' data: lp_swap:416] Timeout (691 > 690)

Example failed swap error info: https://dexapi.cipig.net/public/error.php?uuid=a4a0445e-fa7e-473e-980f-21f8015b9c7b

I can DM full swap json if needed

@borngraced
Copy link
Member Author

borngraced commented Feb 21, 2025

Subsequent to prior comment, all swaps attempted selling KMD with this binary have failed, so approval rescinded for now until further testing can confirm cause.

In all cases, the error was taker_swap:1401] Error waiting for 'maker-payment' data: lp_swap:416] Timeout (691 > 690)

This is a breaking change no BC...so I guess both nodes involved in this swap uses a different versions of kdf

@shamardy
Copy link
Collaborator

@borngraced the 0 dex fee should be for TPU only, for legacy protocol the dex fee is needed as an anti-ddos measure. Did you implement it for TPU only?

@borngraced
Copy link
Member Author

@borngraced the 0 dex fee should be for TPU only, for legacy protocol the dex fee is needed as an anti-ddos measure. Did you implement it for TPU only?

ah thanks for the further clarification...I missed that!!

@laruh
Copy link
Member

laruh commented Feb 23, 2025

@borngraced the 0 dex fee should be for TPU only, for legacy protocol the dex fee is needed as an anti-ddos measure. Did you implement it for TPU only?

I see. @borngraced please merge dev to this branch to keep it up to date, when you fix legacy.
I will join to this feature testing to help with TPU (I think smk didnt touch it yet, only me and cipi did it)

Comment on lines -2763 to +2771
let threshold_coef = &(&MmNumber::from(1) + &dex_fee_rate) / &dex_fee_rate;
let max_vol = if available > min_tx_amount * &threshold_coef {
available / (MmNumber::from(1) + dex_fee_rate)
} else {
let max_vol = if base == "KMD" || rel == "KMD" {
&available - min_tx_amount
} else {
let dex_fee_rate = dex_fee_rate(base, rel);
let threshold_coef = &(&MmNumber::from(1) + &dex_fee_rate) / &dex_fee_rate;
if available > min_tx_amount * &threshold_coef {
available / (MmNumber::from(1) + dex_fee_rate)
} else {
&available - min_tx_amount
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this should be reverted, as KMD zero dexfee is only for TPU?

@@ -3186,6 +3190,7 @@ mod taker_swap_tests {
let min_tx_amount = MmNumber::from("0.00001");

// For these `availables` the dex_fee must be greater than min_tx_amount
// For these `availables` the dex_fee must be lesser than min_tx_amount
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for legacy test

Comment on lines +3218 to +3219
// KMD pairs: for these `availables` the dex_fee must be 0
// Non KMD pairs: for these `availables` the dex_fee must be the same as min_tx_amount
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be reverted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants