@@ -656,6 +656,7 @@ def commit_order(
656
656
side : Literal ["buy" , "sell" ],
657
657
size : int ,
658
658
slippage_tolerance : float = 0 ,
659
+ min_amount_received : int = None ,
659
660
settlement_strategy_id : int = 0 ,
660
661
market_id : int = None ,
661
662
market_name : str = None ,
@@ -673,6 +674,7 @@ def commit_order(
673
674
:param int size: The order size in ether. If ``side`` is "buy", this is the amount
674
675
of the synth to buy. If ``side`` is "sell", this is the amount of the synth to sell.
675
676
: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.
676
678
:param int settlement_strategy_id: The settlement strategy ID. Default 2.
677
679
:param int market_id: The ID of the market.
678
680
:param str market_name: The name of the market.
@@ -683,17 +685,27 @@ def commit_order(
683
685
market_id , market_name = self ._resolve_market (market_id , market_name )
684
686
685
687
# 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" ]
692
695
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 )
697
709
698
710
size_wei = ether_to_wei (size )
699
711
order_type = 3 if side == "buy" else 4
0 commit comments