Skip to content

Commit

Permalink
add LR swap rpc scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
dimxy committed Jan 21, 2025
1 parent 405bcb7 commit 84663a4
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 13 deletions.
6 changes: 3 additions & 3 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ use crate::lp_swap::taker_swap::FailAt;

pub use best_orders::{best_orders_rpc, best_orders_rpc_v2};
pub use orderbook_depth::orderbook_depth_rpc;
pub use orderbook_rpc::{orderbook_rpc, orderbook_rpc_v2};
pub use orderbook_rpc::{orderbook_rpc, orderbook_rpc_v2, AggregatedOrderbookEntryV2};

cfg_wasm32! {
use mm2_db::indexed_db::{ConstructibleDb, DbLocked};
Expand Down Expand Up @@ -5645,7 +5645,7 @@ async fn subscribe_to_orderbook_topic(
Ok(())
}

#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RpcOrderbookEntryV2 {
coin: String,
address: OrderbookAddress,
Expand Down Expand Up @@ -5770,7 +5770,7 @@ fn choose_taker_confs_and_notas(
}
}

#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "address_type", content = "address_data")]
pub enum OrderbookAddress {
Transparent(String),
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_ordermatch/orderbook_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde_json::{self as json, Value as Json};
use super::{addr_format_from_protocol_info, is_my_order, mm2_internal_pubkey_hex, orderbook_address,
subscribe_to_orderbook_topic, OrdermatchContext, RpcOrderbookEntryV2};

#[derive(Debug, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct AggregatedOrderbookEntryV2 {
#[serde(flatten)]
entry: RpcOrderbookEntryV2,
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub use taker_swap::{calc_max_taker_vol, check_balance_for_taker_swap, max_taker
run_taker_swap, taker_swap_trade_preimage, RunTakerSwapInput, TakerSavedSwap, TakerSwap,
TakerSwapData, TakerSwapPreparedParams, TakerTradePreimage, MAKER_PAYMENT_SPENT_BY_WATCHER_LOG,
REFUND_TEST_FAILURE_LOG, WATCHER_MESSAGE_SENT_LOG};
pub use trade_preimage::trade_preimage_rpc;
pub use trade_preimage::{trade_preimage_rpc, TradePreimageResponse};

pub const SWAP_PREFIX: TopicPrefix = "swap";
pub const SWAP_V2_PREFIX: TopicPrefix = "swapv2";
Expand Down
1 change: 1 addition & 0 deletions mm2src/mm2_main/src/lp_swap/trade_preimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ construct_detailed!(DetailedRequiredBalance, required_balance);

pub type TradePreimageRpcResult<T> = Result<T, MmError<TradePreimageRpcError>>;

/// Calculate all fees for the swap and max volume (for maker)
pub async fn trade_preimage_rpc(
ctx: MmArc,
req: TradePreimageRequest,
Expand Down
29 changes: 29 additions & 0 deletions mm2src/mm2_main/src/rpc/lp_commands/lr_swap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//? RPC implementation for swaps with liquidity routing (LR)

use mm2_err_handle::mm_error::{MmResult, MmError};
use mm2_core::mm_ctx::MmArc;
use types::{LrFindBestSwapPathRequest, LrFindBestSwapPathResponse, LrFillOrderRequest, LrFillOrderResponse};
use errors::LrSwapRpcError;

pub mod errors;
pub mod types;

/// Find best swap path with liquidity routing over evm tokens.
/// For the provided list of orderbook entries this RPC searches for the most price effective swap with LR
pub async fn lr_find_best_swap_path_rpc(
ctx: MmArc,
req: LrFindBestSwapPathRequest,
) -> MmResult<LrFindBestSwapPathResponse, LrSwapRpcError> {
MmError::err(LrSwapRpcError::SomeError)
}

/// Run a swap with LR part
pub async fn lr_fill_order_rpc(
ctx: MmArc,
req: LrFillOrderRequest,
) -> MmResult<LrFillOrderResponse, LrSwapRpcError> {
MmError::err(LrSwapRpcError::SomeError)
}

// TODO: Do we need to extend trade_preimage_rpc to include LR-part fee?
// In fact, lr_find_best_swap_path_rpc has same behaviour: returns trade fee
13 changes: 13 additions & 0 deletions mm2src/mm2_main/src/rpc/lp_commands/lr_swap/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//? Errors for LR swaps rpc

use common::{HttpStatusCode, StatusCode};
use enum_derives::EnumFromStringify;
use ser_error_derive::SerializeErrorType;
use serde::Serialize;

#[derive(Debug, Display, Serialize, SerializeErrorType)]
#[serde(tag = "error_type", content = "error_data")]
pub enum LrSwapRpcError {
#[display(fmt = "LR swap error")]
SomeError,
}
61 changes: 61 additions & 0 deletions mm2src/mm2_main/src/rpc/lp_commands/lr_swap/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//? Types for LR swaps rpc
use mm2_number::MmNumber;
use mm2_number::BigDecimal;

use crate::lp_ordermatch::AggregatedOrderbookEntryV2;
use crate::rpc::lp_commands::one_inch::types::ClassicSwapDetails;
use crate::lp_swap::TradePreimageResponse;
use mm2_rpc::data::legacy::{SellBuyRequest, SellBuyResponse};
//use trading_api::one_inch_api::types::ClassicSwapData;
//use coins::TradeFee;

/// Request to find best swap path with LR rpc.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LrFindBestSwapPathRequest {
/// Base (source) coin ticker
pub base: String,
/// Rel (target) coin ticker
pub rel: String,
/// Swap amount in base coins to sell (with fraction)
pub amount: MmNumber,
/// List of maker orders which is searched for best path with LR
pub orderbook_entries: Vec<AggregatedOrderbookEntryV2>,
/// List of tokens allowed to route through with LR
pub route_tokens: Vec<String>,
}

/// Response for find best swap path with LR rpc
#[derive(Serialize)]
pub struct LrFindBestSwapPathResponse {
/// Swap tx data (from 1inch quote)
pub lr_swap_details: ClassicSwapDetails,
/// found best order which can be filled with LR swap
pub best_order: AggregatedOrderbookEntryV2,
/// base/rel price including the price of the LR swap part
pub total_price: BigDecimal,
/// Same retuned
pub trade_fee: TradePreimageResponse,
}

/// Request to sell or buy with LR
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LrFillOrderRequest {
/// Original sell or buy request (but only MatchBy::Orders could be used to fill the maker swap found in )
#[serde(flatten)]
pub fill_req: SellBuyRequest,

/// Tx data to create one inch swap (from 1inch quote)
/// TODO: make this a enum to allow other LR providers
pub lr_swap_details: ClassicSwapDetails,
}

/// Request to sell or buy with LR
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LrFillOrderResponse {
/// Original sell or buy response
#[serde(flatten)]
pub fill_response: SellBuyResponse,
}
1 change: 1 addition & 0 deletions mm2src/mm2_main/src/rpc/lp_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub(crate) mod one_inch;
pub(crate) mod pubkey;
pub(crate) mod tokens;
pub(crate) mod trezor;
pub(crate) mod lr_swap;
22 changes: 17 additions & 5 deletions mm2src/mm2_main/src/rpc/lp_commands/one_inch/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,24 @@ pub struct ClassicSwapCreateRequest {
pub use_permit2: Option<bool>,
}

/// Response for both classic swap quote or create swap calls
#[derive(Serialize, Debug)]
pub struct ClassicSwapResponse {
/// Details to create classic swap calls
#[derive(Serialize, Deserialize, Debug)]
pub struct ClassicSwapDetails {
/// Destination token amount, in coins (with fraction)
pub dst_amount: DetailedAmount,
/// Source (base) token info
#[serde(skip_serializing_if = "Option::is_none")]
pub src_token: Option<TokenInfo>,
/// Source (base) token name as it is defined in the coins file
pub kdf_src_token: Option<String>,
/// Destination (rel) token info
#[serde(skip_serializing_if = "Option::is_none")]
pub dst_token: Option<TokenInfo>,
/// Destination (rel) token name as it is defined in the coins file.
/// This is used to show route tokens in the GUI, like they are in the coin file.
/// However, route tokens can be missed in the coins file and therefore cannot be filled.
/// In this case GUI may use TokenInfo::Address or TokenInfo::Symbol
pub kdf_dst_token: Option<String>,
/// Used liquidity sources
#[serde(skip_serializing_if = "Option::is_none")]
pub protocols: Option<Vec<Vec<Vec<ProtocolInfo>>>>,
Expand All @@ -146,15 +153,20 @@ pub struct ClassicSwapResponse {
pub gas: Option<u128>,
}

impl ClassicSwapResponse {
/// Response for both classic swap quote or create swap calls
pub type ClassicSwapResponse = ClassicSwapDetails;

impl ClassicSwapDetails {
pub(crate) fn from_api_classic_swap_data(
data: one_inch_api::types::ClassicSwapData,
decimals: u8,
) -> MmResult<Self, FromApiValueError> {
Ok(Self {
dst_amount: MmNumber::from(u256_to_big_decimal(U256::from_dec_str(&data.dst_amount)?, decimals)?).into(),
src_token: data.src_token,
kdf_src_token: todo!(),
dst_token: data.dst_token,
kdf_dst_token: todo!(),
protocols: data.protocols,
tx: data
.tx
Expand All @@ -165,7 +177,7 @@ impl ClassicSwapResponse {
}
}

#[derive(Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug)]
pub struct TxFields {
pub from: Address,
pub to: Address,
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_number/src/mm_number_multi_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use crate::from_ratio_to_dec;
use crate::mm_number::MmNumber;
use bigdecimal::BigDecimal;
use num_rational::BigRational;
use serde::Serialize;
use serde::{Deserialize, Serialize};

/// MmNumber representation in all available forms.
#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MmNumberMultiRepr {
pub decimal: BigDecimal,
pub rational: BigRational,
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_rpc/src/data/legacy/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct SellBuyRequest {
pub save_in_history: bool,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Debug, Deserialize)]
pub struct SellBuyResponse {
#[serde(flatten)]
pub request: TakerRequestForRpc,
Expand Down

0 comments on commit 84663a4

Please sign in to comment.