Skip to content

Commit

Permalink
fix async min_amount_received
Browse files Browse the repository at this point in the history
  • Loading branch information
Tburm committed Sep 3, 2024
1 parent 3d7bdbe commit 08b3cad
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/synthetix/spot/spot.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ def commit_order(
side: Literal["buy", "sell"],
size: int,
slippage_tolerance: float = 0,
min_amount_received: int = None,
settlement_strategy_id: int = 0,
market_id: int = None,
market_name: str = None,
Expand All @@ -673,6 +674,7 @@ def commit_order(
:param int size: The order size in ether. If ``side`` is "buy", this is the amount
of the synth to buy. If ``side`` is "sell", this is the amount of the synth to sell.
:param float slippage_tolerance: The slippage tolerance for the order as a percentage (0.01 = 1%). Default is 0.
:param int min_amount_received: The minimum amount to receive in ether units. This will override the slippage_tolerance.
:param int settlement_strategy_id: The settlement strategy ID. Default 2.
:param int market_id: The ID of the market.
:param str market_name: The name of the market.
Expand All @@ -683,17 +685,27 @@ def commit_order(
market_id, market_name = self._resolve_market(market_id, market_name)

# get a price
feed_id = self.markets_by_id[market_id]["settlement_strategy"]["feed_id"]
settlement_reward = self.markets_by_id[market_id]["settlement_strategy"][
"settlement_reward"
]
pyth_data = self.snx.pyth.get_price_from_ids([feed_id])
price = pyth_data["meta"][feed_id]["price"]
if min_amount_received is None:
feed_id = self.markets_by_id[market_id]["settlement_strategy"]["feed_id"]
settlement_reward = self.markets_by_id[market_id]["settlement_strategy"][
"settlement_reward"
]
pyth_data = self.snx.pyth.get_price_from_ids([feed_id])
price = pyth_data["meta"][feed_id]["price"]

min_amount_received = (
size * price * (1 - slippage_tolerance) - settlement_reward
)
min_amount_received_wei = ether_to_wei(min_amount_received)
# adjust size for trade side
if side == "buy":
trade_size = size / price
else:
trade_size = size * price

# calculate the amount after slippage
min_amount_received = (
trade_size * (1 - slippage_tolerance) - settlement_reward
)
min_amount_received_wei = ether_to_wei(min_amount_received)
else:
min_amount_received_wei = ether_to_wei(min_amount_received)

size_wei = ether_to_wei(size)
order_type = 3 if side == "buy" else 4
Expand Down

0 comments on commit 08b3cad

Please sign in to comment.