Skip to content

Commit 3ab5ae0

Browse files
committed
use Option for premium, as new_protocol::MakerReserved needs BigRational, which doesn't impl std default
1 parent 4f1002c commit 3ab5ae0

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

mm2src/mm2_main/src/lp_ordermatch.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,8 +1722,8 @@ pub struct MakerOrder {
17221722
p2p_privkey: Option<SerializableSecp256k1Keypair>,
17231723
#[serde(default, skip_serializing_if = "SwapVersion::is_legacy")]
17241724
pub swap_version: SwapVersion,
1725-
#[serde(default, skip_serializing_if = "MmNumber::is_zero")]
1726-
premium: MmNumber,
1725+
#[serde(default, skip_serializing_if = "Option::is_none")]
1726+
premium: Option<MmNumber>,
17271727
}
17281728

17291729
pub struct MakerOrderBuilder<'a> {
@@ -1737,7 +1737,7 @@ pub struct MakerOrderBuilder<'a> {
17371737
conf_settings: Option<OrderConfirmationsSettings>,
17381738
save_in_history: bool,
17391739
swap_version: u8,
1740-
premium: MmNumber,
1740+
premium: Option<MmNumber>,
17411741
}
17421742

17431743
pub enum MakerOrderBuildError {
@@ -1933,7 +1933,7 @@ impl<'a> MakerOrderBuilder<'a> {
19331933
/// In the future alls users will be using TPU V2 by default without "use_trading_proto_v2" configuration.
19341934
pub fn set_legacy_swap_v(&mut self) { self.swap_version = legacy_swap_version() }
19351935

1936-
pub fn with_premium(mut self, premium: MmNumber) -> Self {
1936+
pub fn with_premium(mut self, premium: Option<MmNumber>) -> Self {
19371937
self.premium = premium;
19381938
self
19391939
}
@@ -2091,6 +2091,7 @@ impl MakerOrder {
20912091
let ticker_match = (self.base == taker.rel || self.base_orderbook_ticker.as_ref() == Some(&taker.rel))
20922092
&& (self.rel == taker.base || self.rel_orderbook_ticker.as_ref() == Some(&taker.base));
20932093
let taker_price = taker_base_amount / taker_rel_amount;
2094+
let premium = self.premium.clone().unwrap_or_default();
20942095

20952096
// Determine the matched amounts depending on version
20962097
let (matched_base_amount, matched_rel_amount) =
@@ -2100,13 +2101,13 @@ impl MakerOrder {
21002101
} else {
21012102
// For TPU, if the total rel amount from the taker (rel is coin which should be sent by taker during swap)
21022103
// is less than the maker's premium, the trade is not possible
2103-
if taker_base_amount < &self.premium {
2104+
if taker_base_amount < &premium {
21042105
return OrderMatchResult::NotMatched;
21052106
}
21062107
// Calculate the resulting base amount using the maker's price instead of the taker's.
21072108
// The maker wants to "take" an additional portion of rel as a premium,
21082109
// so we reduce the base amount the maker gives by (premium / price).
2109-
let matched_base_amount = &(taker_base_amount - &self.premium) / &self.price;
2110+
let matched_base_amount = &(taker_base_amount - &premium) / &self.price;
21102111
(matched_base_amount, taker_base_amount.clone())
21112112
};
21122113

@@ -2256,8 +2257,8 @@ pub struct MakerReserved {
22562257
pub rel_protocol_info: Option<Vec<u8>>,
22572258
#[serde(default, skip_serializing_if = "SwapVersion::is_legacy")]
22582259
pub swap_version: SwapVersion,
2259-
#[serde(default, skip_serializing_if = "MmNumber::is_zero")]
2260-
premium: MmNumber,
2260+
#[serde(default, skip_serializing_if = "Option::is_none")]
2261+
premium: Option<MmNumber>,
22612262
}
22622263

22632264
impl MakerReserved {
@@ -2286,7 +2287,7 @@ impl MakerReserved {
22862287
base_protocol_info: message.base_protocol_info,
22872288
rel_protocol_info: message.rel_protocol_info,
22882289
swap_version: message.swap_version,
2289-
premium: message.premium,
2290+
premium: message.premium.map(MmNumber::from),
22902291
}
22912292
}
22922293
}
@@ -2304,7 +2305,7 @@ impl From<MakerReserved> for new_protocol::OrdermatchMessage {
23042305
base_protocol_info: maker_reserved.base_protocol_info,
23052306
rel_protocol_info: maker_reserved.rel_protocol_info,
23062307
swap_version: maker_reserved.swap_version,
2307-
premium: maker_reserved.premium,
2308+
premium: maker_reserved.premium.map(|p| p.to_ratio()),
23082309
})
23092310
}
23102311
}
@@ -3154,7 +3155,7 @@ fn lp_connect_start_bob(ctx: MmArc, maker_match: MakerMatch, maker_order: MakerO
31543155
locktime: &lock_time,
31553156
maker_amount: &maker_amount,
31563157
taker_amount: &taker_amount,
3157-
taker_premium: &maker_order.premium,
3158+
taker_premium: &maker_order.premium.clone().unwrap_or_default(),
31583159
};
31593160
let taker_p2p_pubkey = match taker_p2p_pubkey {
31603161
PublicKey::Secp256k1(pubkey) => pubkey.into(),
@@ -3388,7 +3389,7 @@ fn lp_connected_alice(ctx: MmArc, taker_order: TakerOrder, taker_match: TakerMat
33883389
locktime: &locktime,
33893390
maker_amount: &maker_amount,
33903391
taker_amount: &taker_amount,
3391-
taker_premium: &taker_match.reserved.premium,
3392+
taker_premium: &taker_match.reserved.premium.unwrap_or_default(),
33923393
};
33933394
let maker_p2p_pubkey = match maker_p2p_pubkey {
33943395
PublicKey::Secp256k1(pubkey) => pubkey.into(),
@@ -4761,7 +4762,7 @@ pub struct SetPriceReq {
47614762
#[serde(default = "get_true")]
47624763
save_in_history: bool,
47634764
#[serde(default)]
4764-
premium: MmNumber,
4765+
premium: Option<MmNumber>,
47654766
}
47664767

47674768
#[derive(Deserialize)]

mm2src/mm2_main/src/lp_ordermatch/new_protocol.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ pub struct MakerReserved {
285285
pub rel_protocol_info: Option<Vec<u8>>,
286286
#[serde(default, skip_serializing_if = "SwapVersion::is_legacy")]
287287
pub swap_version: SwapVersion,
288-
#[serde(default, skip_serializing_if = "MmNumber::is_zero")]
289-
pub premium: MmNumber,
288+
/// Note: `std::default::Default` is not implemented for `num_rational::Ratio<mm2_number::BigInt>`, that's why it's preferable to use Optional
289+
#[serde(default, skip_serializing_if = "Option::is_none")]
290+
pub premium: Option<BigRational>,
290291
}
291292

292293
#[derive(Clone, Debug, Deserialize, Serialize)]

mm2src/mm2_main/src/lp_ordermatch/simple_market_maker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ async fn create_single_order(
601601
rel_confs: cfg.rel_confs,
602602
rel_nota: cfg.rel_nota,
603603
save_in_history: true,
604-
premium: MmNumber::default(),
604+
premium: None,
605605
};
606606

607607
let resp = create_maker_order(&ctx, req)

0 commit comments

Comments
 (0)