Skip to content

Commit 08b3cad

Browse files
committed
fix async min_amount_received
1 parent 3d7bdbe commit 08b3cad

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/synthetix/spot/spot.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ def commit_order(
656656
side: Literal["buy", "sell"],
657657
size: int,
658658
slippage_tolerance: float = 0,
659+
min_amount_received: int = None,
659660
settlement_strategy_id: int = 0,
660661
market_id: int = None,
661662
market_name: str = None,
@@ -673,6 +674,7 @@ def commit_order(
673674
:param int size: The order size in ether. If ``side`` is "buy", this is the amount
674675
of the synth to buy. If ``side`` is "sell", this is the amount of the synth to sell.
675676
:param float slippage_tolerance: The slippage tolerance for the order as a percentage (0.01 = 1%). Default is 0.
677+
:param int min_amount_received: The minimum amount to receive in ether units. This will override the slippage_tolerance.
676678
:param int settlement_strategy_id: The settlement strategy ID. Default 2.
677679
:param int market_id: The ID of the market.
678680
:param str market_name: The name of the market.
@@ -683,17 +685,27 @@ def commit_order(
683685
market_id, market_name = self._resolve_market(market_id, market_name)
684686

685687
# get a price
686-
feed_id = self.markets_by_id[market_id]["settlement_strategy"]["feed_id"]
687-
settlement_reward = self.markets_by_id[market_id]["settlement_strategy"][
688-
"settlement_reward"
689-
]
690-
pyth_data = self.snx.pyth.get_price_from_ids([feed_id])
691-
price = pyth_data["meta"][feed_id]["price"]
688+
if min_amount_received is None:
689+
feed_id = self.markets_by_id[market_id]["settlement_strategy"]["feed_id"]
690+
settlement_reward = self.markets_by_id[market_id]["settlement_strategy"][
691+
"settlement_reward"
692+
]
693+
pyth_data = self.snx.pyth.get_price_from_ids([feed_id])
694+
price = pyth_data["meta"][feed_id]["price"]
692695

693-
min_amount_received = (
694-
size * price * (1 - slippage_tolerance) - settlement_reward
695-
)
696-
min_amount_received_wei = ether_to_wei(min_amount_received)
696+
# adjust size for trade side
697+
if side == "buy":
698+
trade_size = size / price
699+
else:
700+
trade_size = size * price
701+
702+
# calculate the amount after slippage
703+
min_amount_received = (
704+
trade_size * (1 - slippage_tolerance) - settlement_reward
705+
)
706+
min_amount_received_wei = ether_to_wei(min_amount_received)
707+
else:
708+
min_amount_received_wei = ether_to_wei(min_amount_received)
697709

698710
size_wei = ether_to_wei(size)
699711
order_type = 3 if side == "buy" else 4

0 commit comments

Comments
 (0)