Skip to content

Commit e030b6e

Browse files
committed
ensure atomic swap is sell
1 parent 1f4e0e6 commit e030b6e

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

mm2src/mm2_main/src/lr_swap/lr_swap_state_machine.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,11 @@ impl AggTakerSwapStateMachine {
10241024
}
10251025

10261026
/// Start nested atomic swap by calling lp_auto_buy. The actual volume is determined on previous states
1027+
/// The volume is always in the taker sell coins.
1028+
/// TODO: add slippage support
10271029
async fn start_lp_auto_buy(&self, volume: MmNumber, _slippage: f32) -> MmResult<SellBuyResponse, LrSwapError> {
1030+
const TAKER_ACTION: TakerAction = TakerAction::Sell;
1031+
10281032
let base_coin = lp_coinfind_or_err(&self.ctx, &self.atomic_swap.base)
10291033
.await
10301034
.map_mm_err()?;
@@ -1047,11 +1051,11 @@ impl AggTakerSwapStateMachine {
10471051
"{LOG_PREFIX} volume={} self.atomic_swap.price={} action={:?} base={} rel={}",
10481052
volume.to_decimal(),
10491053
self.atomic_swap.price.to_decimal(),
1050-
self.atomic_swap.action,
1054+
TAKER_ACTION,
10511055
self.atomic_swap.base,
10521056
self.atomic_swap.rel
10531057
);
1054-
let (taker_amount, sell_base, sell_rel) = match self.atomic_swap.action {
1058+
let (taker_amount, sell_base, sell_rel) = match TAKER_ACTION {
10551059
TakerAction::Buy => (&volume * &self.atomic_swap.price, rel_coin.clone(), base_coin.clone()),
10561060
TakerAction::Sell => (volume.clone(), base_coin.clone(), rel_coin.clone()),
10571061
};
@@ -1077,15 +1081,15 @@ impl AggTakerSwapStateMachine {
10771081
"{LOG_PREFIX} starting atomic swap for {}/{}, action {:?}, amount {}",
10781082
self.atomic_swap.base,
10791083
self.atomic_swap.rel,
1080-
self.atomic_swap.action,
1084+
TAKER_ACTION,
10811085
volume.to_decimal()
10821086
);
10831087
let sell_buy_req = lr_helpers::make_atomic_swap_request(
10841088
self.atomic_swap.base.clone(),
10851089
self.atomic_swap.rel.clone(),
10861090
self.atomic_swap.price.clone(),
10871091
volume,
1088-
self.atomic_swap.action.clone(),
1092+
TAKER_ACTION,
10891093
self.atomic_swap.match_by.clone(),
10901094
self.atomic_swap.order_type.clone(),
10911095
);
@@ -1196,8 +1200,8 @@ impl AggTakerSwapStateMachine {
11961200
let dex_fee_rate = DexFee::dex_fee_rate(&taker_ticker, &maker_ticker);
11971201
// Recalculate swap_volume with the tarde fees removed:
11981202
let taker_volume = volume_with_fees.clone() / (MmNumber::from("1") + dex_fee_rate);
1199-
println!(
1200-
"deduct_fees volume_with_fees={volume_with_fees} taker_volume={taker_volume} dex_fee_diff={}",
1203+
debug!(
1204+
"{maker_ticker}/{taker_ticker} deduct_fees volume_with_fees={volume_with_fees} taker_volume={taker_volume} dex_fee_diff={}",
12011205
&volume_with_fees - &taker_volume
12021206
);
12031207

mm2src/mm2_main/src/rpc/lp_commands/lr_swap_api/lr_api_types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl AskOrBidOrder {
6565
}
6666
}
6767
/// Convert to maker buy price
68+
#[allow(unused)]
6869
pub fn buy_price(&self) -> MmNumber {
6970
match self {
7071
AskOrBidOrder::Ask { order, .. } => &MmNumber::from(1) / &order.price.rational.clone().into(),

mm2src/mm2_main/tests/mm2_tests/lr_swap_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn test_aggregated_swap_mainnet_polygon_utxo() {
224224
alice_bal_diff
225225
);
226226
assert!(
227-
alice_bal_diff < &swap_amount * &"1.20".parse::<BigDecimal>().unwrap(),
227+
alice_bal_diff < &swap_amount * "1.20".parse::<BigDecimal>().unwrap(),
228228
"too low received {}",
229229
alice_bal_diff
230230
);
@@ -412,7 +412,7 @@ fn test_aggregated_swap_mainnet_polygon_arbitrum_impl(
412412
.flatten()
413413
.cloned()
414414
.collect::<Vec<_>>();
415-
json_asks.push(json!({ "base": &grt_ticker, "orders": &best_asks_1 }));
415+
json_asks.push(json!({ "base": &arb_ticker, "orders": &best_asks_1 }));
416416
} else {
417417
// using "sell" we get all aave orders as bids:
418418
let best_orders_res = block_on(best_orders_v2_by_number(&mm_alice, &aave_ticker, "sell", 10, true));

0 commit comments

Comments
 (0)