forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 110
feat(tpu): provide premium for tpu #2405
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
Draft
laruh
wants to merge
23
commits into
dev
Choose a base branch
from
premium-tpu
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
aee2d17
add premium field in SetPriceReq and MakerOrder
laruh 1736774
wip
laruh 810addf
comeback to prod ver
laruh 2d996d1
Update Buy and Sell actions, provide more notes
laruh 9eb0663
improve match_with_request Sell; add premium to start state machines
laruh 419d91a
premium in MakerReserved
laruh 4f1002c
use old comment for resulting base amount in Sell legacy
laruh cd53b6d
use Option for premium, as new_protocol::MakerReserved needs BigRatio…
laruh 1344166
Option premium field in start_swaps test function
laruh 99c7d41
provide taker_method: TakerMethod field to start_swaps fn
laruh c3217ae
buy and sell work
laruh af2c3c5
Merge remote-tracking branch 'origin/dev' into premium-tpu
laruh 18dc713
fix utxo buy test; add burnkey_as_alice test when taker sells
laruh 4aaf0bd
fix burnkey_as_alice test for sell
laruh 96594e1
provide test_v2_swap_utxo_utxo_impl_common function for tests
laruh 7d2f22c
Add a clarification doc comment for the premium field
laruh 011798a
review: create is_legacy variable
laruh e19e1c5
review: move base amount calc to a variable
laruh 385b382
Don't force the taker to send all amount they offered in the Buy action
laruh 285b9ef
review: create premium variable before taker action matching
laruh 74c126d
add safe check for taker real price
laruh 1332c21
review: doc com for premium
laruh 0207b02
merge with origin/dev
shamardy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1666,7 +1666,22 @@ impl TakerOrder { | |
| || self.base_orderbook_ticker.as_ref() == Some(&reserved.rel)) | ||
| && (self.request.rel == reserved.base | ||
| || self.rel_orderbook_ticker.as_ref() == Some(&reserved.base)); | ||
| if match_ticker && my_base_amount == other_rel_amount && my_rel_amount <= other_base_amount { | ||
|
|
||
| // Reject if any common conditions are unmet | ||
| if !match_ticker || my_base_amount != other_rel_amount { | ||
| return MatchReservedResult::NotMatched; | ||
| } | ||
|
|
||
| let other_base_amount = if self.request.swap_version.is_legacy() || reserved.swap_version.is_legacy() { | ||
| other_base_amount.clone() | ||
| } else { | ||
| let premium = &reserved.premium.clone().unwrap_or_default(); | ||
| let other_price = &(my_base_amount - premium) / other_base_amount; | ||
| // In match_with_request function, we allowed maker to send fewer coins for taker sell action | ||
| other_base_amount + &(premium / &other_price) | ||
| }; | ||
|
|
||
| if my_rel_amount <= &other_base_amount { | ||
| MatchReservedResult::Matched | ||
| } else { | ||
| MatchReservedResult::NotMatched | ||
|
|
@@ -1754,6 +1769,9 @@ pub struct MakerOrder { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: OrderMetadata, | ||
| timeout_in_minutes: Option<u16>, | ||
| /// Fixed extra amount of maker rel coin, requested by the maker and paid by the taker | ||
| #[serde(default, skip_serializing_if = "Option::is_none")] | ||
| premium: Option<MmNumber>, | ||
| } | ||
|
|
||
| pub struct MakerOrderBuilder<'a> { | ||
|
|
@@ -1770,6 +1788,7 @@ pub struct MakerOrderBuilder<'a> { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: OrderMetadata, | ||
| timeout_in_minutes: Option<u16>, | ||
| premium: Option<MmNumber>, | ||
borngraced marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// Contains extra and/or optional metadata (e.g., protocol-specific information) that can | ||
|
|
@@ -1933,6 +1952,7 @@ impl<'a> MakerOrderBuilder<'a> { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: OrderMetadata::default(), | ||
| timeout_in_minutes: None, | ||
| premium: Default::default(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1979,6 +1999,11 @@ impl<'a> MakerOrderBuilder<'a> { | |
| /// In the future alls users will be using TPU V2 by default without "use_trading_proto_v2" configuration. | ||
| pub fn set_legacy_swap_v(&mut self) { self.swap_version = legacy_swap_version() } | ||
|
|
||
| pub fn with_premium(mut self, premium: Option<MmNumber>) -> Self { | ||
| self.premium = premium; | ||
| self | ||
| } | ||
|
|
||
| /// Build MakerOrder | ||
| #[allow(clippy::result_large_err)] | ||
| pub fn build(self) -> Result<MakerOrder, MakerOrderBuildError> { | ||
|
|
@@ -2039,6 +2064,7 @@ impl<'a> MakerOrderBuilder<'a> { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: self.order_metadata, | ||
| timeout_in_minutes: self.timeout_in_minutes, | ||
| premium: self.premium, | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -2067,6 +2093,7 @@ impl<'a> MakerOrderBuilder<'a> { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: self.order_metadata, | ||
| timeout_in_minutes: self.timeout_in_minutes, | ||
| premium: Default::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2105,31 +2132,71 @@ impl MakerOrder { | |
| return OrderMatchResult::NotMatched; | ||
| } | ||
|
|
||
| let is_legacy = self.swap_version.is_legacy() || taker.swap_version.is_legacy(); | ||
| let premium = self.premium.clone().unwrap_or_default(); | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we may get premium as reference here, like: and use it within |
||
| match taker.action { | ||
| TakerAction::Buy => { | ||
| let ticker_match = (self.base == taker.base | ||
| || self.base_orderbook_ticker.as_ref() == Some(&taker.base)) | ||
| && (self.rel == taker.rel || self.rel_orderbook_ticker.as_ref() == Some(&taker.rel)); | ||
| // taker_base_amount: the amount taker desires to buy (input.volume from SellBuyRequest) | ||
| // taker_rel_amount: the amount taker is willing to pay (input.volume * input.price, where input is SellBuyRequest) | ||
| // taker_price: the effective price offered by the taker | ||
| let taker_price = taker_rel_amount / taker_base_amount; | ||
| if ticker_match | ||
| && taker_base_amount <= &self.available_amount() | ||
| && taker_base_amount >= &self.min_base_vol | ||
| && taker_price >= self.price | ||
| { | ||
| OrderMatchResult::Matched((taker_base_amount.clone(), taker_base_amount * &self.price)) | ||
|
|
||
| // Reject if any basic conditions are not satisfied | ||
| let base_amount_exceeds = taker_base_amount > &self.available_amount(); | ||
| let below_min_volume = taker_base_amount < &self.min_base_vol; | ||
| let price_too_low = taker_price < self.price; | ||
|
|
||
| if !ticker_match || base_amount_exceeds || below_min_volume || price_too_low { | ||
| return OrderMatchResult::NotMatched; | ||
| } | ||
|
|
||
| let result_rel_amount = taker_base_amount * &self.price; | ||
|
|
||
| if is_legacy { | ||
| // Legacy mode: use maker's price to calculate rel amount | ||
| OrderMatchResult::Matched((taker_base_amount.clone(), result_rel_amount)) | ||
| } else { | ||
| OrderMatchResult::NotMatched | ||
| // taker_rel_amount must cover the premium requested by maker | ||
| let required_rel_amount = result_rel_amount + premium; | ||
| if taker_rel_amount >= &required_rel_amount { | ||
| // TPU mode: treat buy as a limit order using taker's base amount and required_rel_amount | ||
| OrderMatchResult::Matched((taker_base_amount.clone(), required_rel_amount)) | ||
| } else { | ||
| OrderMatchResult::NotMatched | ||
| } | ||
| } | ||
| }, | ||
| TakerAction::Sell => { | ||
| let ticker_match = (self.base == taker.rel || self.base_orderbook_ticker.as_ref() == Some(&taker.rel)) | ||
| && (self.rel == taker.base || self.rel_orderbook_ticker.as_ref() == Some(&taker.base)); | ||
| let taker_price = taker_base_amount / taker_rel_amount; | ||
|
|
||
| // Calculate the resulting base amount using the Maker's price instead of the Taker's. | ||
| let matched_base_amount = taker_base_amount / &self.price; | ||
| let matched_rel_amount = taker_base_amount.clone(); | ||
| // Determine the matched amounts depending on version | ||
| let (matched_base_amount, matched_rel_amount) = if is_legacy { | ||
| // Legacy: calculate the resulting base amount using the Maker's price instead of the Taker's. | ||
| (taker_base_amount / &self.price, taker_base_amount.clone()) | ||
| } else { | ||
| // this check prevents division by zero | ||
| if taker_base_amount <= &premium { | ||
| return OrderMatchResult::NotMatched; | ||
| } | ||
| // Calculate the resulting base amount using the maker's price instead of the taker's. | ||
| // For TPU, in the taker sell action the maker wants to "take" an additional portion of rel as a premium, | ||
| // so we reduce the base amount the maker gives by (premium / price). | ||
| let result_base_amount = &(taker_base_amount - &premium) / &self.price; | ||
| let real_price_for_taker = taker_base_amount / &result_base_amount; | ||
| // Ensure the taker doesn't end up paying a higher price (including premium) | ||
| if real_price_for_taker > taker_price { | ||
| return OrderMatchResult::NotMatched; | ||
| } | ||
| (result_base_amount, taker_base_amount.clone()) | ||
| }; | ||
|
|
||
| // Match if all common conditions are met | ||
| if ticker_match | ||
| && matched_base_amount <= self.available_amount() | ||
dimxy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| && matched_base_amount >= self.min_base_vol | ||
|
|
@@ -2202,6 +2269,7 @@ impl From<TakerOrder> for MakerOrder { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: taker_order.request.order_metadata, | ||
| timeout_in_minutes: None, | ||
| premium: Default::default(), | ||
| }, | ||
| // The "buy" taker order is recreated with reversed pair as Maker order is always considered as "sell" | ||
| TakerAction::Buy => { | ||
|
|
@@ -2229,6 +2297,7 @@ impl From<TakerOrder> for MakerOrder { | |
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: taker_order.request.order_metadata, | ||
| timeout_in_minutes: None, | ||
| premium: Default::default(), | ||
| } | ||
| }, | ||
| } | ||
|
|
@@ -2283,6 +2352,11 @@ pub struct MakerReserved { | |
| pub swap_version: SwapVersion, | ||
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: OrderMetadata, | ||
| /// Note: `std::default::Default` is not implemented for `num_rational::Ratio<mm2_number::BigInt>` | ||
| /// in the [new_protocol::MakerReserved] structure. As a result, we use `Option<BigRational>` there. | ||
| /// It is preferable to follow this same approach in the current structure for consistency. | ||
| #[serde(default, skip_serializing_if = "Option::is_none")] | ||
| premium: Option<MmNumber>, | ||
borngraced marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| impl MakerReserved { | ||
|
|
@@ -2314,6 +2388,7 @@ impl MakerReserved { | |
| // TODO: Support the new protocol types. | ||
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: OrderMetadata::default(), | ||
| premium: message.premium.map(MmNumber::from), | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -2331,6 +2406,7 @@ impl From<MakerReserved> for new_protocol::OrdermatchMessage { | |
| base_protocol_info: maker_reserved.base_protocol_info, | ||
| rel_protocol_info: maker_reserved.rel_protocol_info, | ||
| swap_version: maker_reserved.swap_version, | ||
| premium: maker_reserved.premium.map(|p| p.to_ratio()), | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -3071,6 +3147,7 @@ struct StateMachineParams<'a> { | |
| locktime: &'a u64, | ||
| maker_amount: &'a MmNumber, | ||
| taker_amount: &'a MmNumber, | ||
| taker_premium: &'a MmNumber, | ||
| } | ||
|
|
||
| #[allow(unreachable_code, unused_variables)] // TODO: remove with `ibc-routing-for-swaps` feature removal. | ||
|
|
@@ -3196,6 +3273,7 @@ fn lp_connect_start_bob(ctx: MmArc, maker_match: MakerMatch, maker_order: MakerO | |
| locktime: &lock_time, | ||
| maker_amount: &maker_amount, | ||
| taker_amount: &taker_amount, | ||
| taker_premium: &maker_order.premium.clone().unwrap_or_default(), | ||
| }; | ||
| let taker_p2p_pubkey = match taker_p2p_pubkey { | ||
| PublicKey::Secp256k1(pubkey) => pubkey.into(), | ||
|
|
@@ -3298,7 +3376,7 @@ async fn start_maker_swap_state_machine< | |
| secret: *secret, | ||
| taker_coin: taker_coin.clone(), | ||
| taker_volume: params.taker_amount.clone(), | ||
| taker_premium: Default::default(), | ||
| taker_premium: params.taker_premium.clone(), | ||
| conf_settings: *params.my_conf_settings, | ||
| p2p_topic: swap_v2_topic(params.uuid), | ||
| uuid: *params.uuid, | ||
|
|
@@ -3442,6 +3520,7 @@ fn lp_connected_alice(ctx: MmArc, taker_order: TakerOrder, taker_match: TakerMat | |
| locktime: &locktime, | ||
| maker_amount: &maker_amount, | ||
| taker_amount: &taker_amount, | ||
| taker_premium: &taker_match.reserved.premium.unwrap_or_default(), | ||
| }; | ||
| let maker_p2p_pubkey = match maker_p2p_pubkey { | ||
| PublicKey::Secp256k1(pubkey) => pubkey.into(), | ||
|
|
@@ -3551,7 +3630,7 @@ async fn start_taker_swap_state_machine< | |
| maker_volume: params.maker_amount.clone(), | ||
| taker_coin: taker_coin.clone(), | ||
| taker_volume: params.taker_amount.clone(), | ||
| taker_premium: Default::default(), | ||
| taker_premium: params.taker_premium.clone(), | ||
| secret_hash_algo: *params.secret_hash_algo, | ||
| conf_settings: *params.my_conf_settings, | ||
| p2p_topic: swap_v2_topic(params.uuid), | ||
|
|
@@ -4098,6 +4177,7 @@ async fn process_taker_request(ctx: MmArc, from_pubkey: H256Json, taker_request: | |
| swap_version: order.swap_version, | ||
| #[cfg(feature = "ibc-routing-for-swaps")] | ||
| order_metadata: order.order_metadata.clone(), | ||
| premium: order.premium.clone(), | ||
| }; | ||
| let topic = order.orderbook_topic(); | ||
| log::debug!("Request matched sending reserved {:?}", reserved); | ||
|
|
@@ -4834,6 +4914,8 @@ pub struct SetPriceReq { | |
| #[serde(default = "get_true")] | ||
| save_in_history: bool, | ||
| timeout_in_minutes: Option<u16>, | ||
| #[serde(default)] | ||
| premium: Option<MmNumber>, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
|
|
@@ -5097,7 +5179,8 @@ pub async fn create_maker_order(ctx: &MmArc, req: SetPriceReq) -> Result<MakerOr | |
| .with_conf_settings(conf_settings) | ||
| .with_save_in_history(req.save_in_history) | ||
| .with_base_orderbook_ticker(ordermatch_ctx.orderbook_ticker(base_coin.ticker())) | ||
| .with_rel_orderbook_ticker(ordermatch_ctx.orderbook_ticker(rel_coin.ticker())); | ||
| .with_rel_orderbook_ticker(ordermatch_ctx.orderbook_ticker(rel_coin.ticker())) | ||
| .with_premium(req.premium); | ||
dimxy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if let Some(t) = req.timeout_in_minutes { | ||
| builder.set_timeout(t); | ||
|
|
@@ -5329,7 +5412,7 @@ pub async fn update_maker_order_rpc(ctx: MmArc, req: Json) -> Result<Response<Ve | |
| /// Result of match_order_and_request function | ||
| #[derive(Debug, PartialEq)] | ||
| enum OrderMatchResult { | ||
| /// Order and request matched, contains base and rel resulting amounts | ||
| /// Order and request matched, contains base and rel resulting amounts (represent maker and taker payment amounts for swap) | ||
| Matched((MmNumber, MmNumber)), | ||
| /// Orders didn't match | ||
| NotMatched, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add a doc comment for this field?