Skip to content

Commit cbd844e

Browse files
committed
adjust config and add liquidation functions
1 parent 7ff248c commit cbd844e

File tree

2 files changed

+101
-39
lines changed

2 files changed

+101
-39
lines changed

src/synthetix/perps/perps.py

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,8 @@ def get_markets(self):
13141314
"market_name": market_digests[ind][1].decode("utf-8").strip("\x00"),
13151315
"symbol": market_digests[ind][1].decode("utf-8").strip("\x00")[:-4],
13161316
"feed_id": encode_hex(market_configs[ind][1]),
1317-
"config": unpack_bfp_configuration(market_config),
1317+
"system_config": unpack_bfp_configuration(market_config),
1318+
"market_config": unpack_bfp_configuration_by_id(market_configs[ind]),
13181319
}
13191320
for ind, market_id in enumerate(market_ids)
13201321
}
@@ -1346,7 +1347,7 @@ def get_markets(self):
13461347
"total_trader_debt_usd": wei_to_ether(market_digests[ind][10]),
13471348
"total_collateral_value_usd": wei_to_ether(market_digests[ind][11]),
13481349
"debt_correction": wei_to_ether(market_digests[ind][12]),
1349-
# "market_configuration": unpack_bfp_configuration_by_id(market_configs[ind]),
1350+
"market_config": unpack_bfp_configuration_by_id(market_configs[ind]),
13501351
}
13511352
for ind, market_id in enumerate(market_ids)
13521353
}
@@ -1541,6 +1542,39 @@ def get_quote(
15411542
"side": "long" if size > 0 else "short",
15421543
}
15431544

1545+
def get_can_liquidate(
1546+
self, account_id: int = None, market_id: int = None, market_name: str = None
1547+
):
1548+
"""
1549+
Check if an ``account_id`` is eligible for liquidation on a specified market
1550+
1551+
:param int | None account_id: The id of the account to check. If not provided, the default account is used.
1552+
:return: A boolean indicating if the account is eligible for liquidation.
1553+
:rtype: bool
1554+
"""
1555+
market_id, market_name = self._resolve_market(market_id, market_name)
1556+
if not account_id:
1557+
account_id = self.default_account_id
1558+
1559+
# call the liquidation functions
1560+
is_position_liquidatable = call_erc7412(
1561+
self.snx,
1562+
self.market_proxy,
1563+
"isPositionLiquidatable",
1564+
(account_id, market_id),
1565+
)
1566+
is_margin_liquidatable = call_erc7412(
1567+
self.snx,
1568+
self.market_proxy,
1569+
"isMarginLiquidatable",
1570+
(account_id, market_id),
1571+
)
1572+
1573+
return {
1574+
"is_position_liquidatable": is_position_liquidatable,
1575+
"is_margin_liquidatable": is_margin_liquidatable,
1576+
}
1577+
15441578
def modify_collateral(
15451579
self,
15461580
amount: float,
@@ -1644,10 +1678,11 @@ def commit_order(
16441678
hooks = []
16451679

16461680
# Prepare the transaction
1647-
tx_params = self.snx._get_tx_params(to=self.market_proxy.address)
1648-
tx_params["data"] = self.market_proxy.encodeABI(
1649-
fn_name="commitOrder",
1650-
args=[
1681+
tx_params = write_erc7412(
1682+
self.snx,
1683+
self.market_proxy,
1684+
"commitOrder",
1685+
[
16511686
account_id,
16521687
market_id,
16531688
size_wei,
@@ -1703,10 +1738,10 @@ def settle_order(
17031738
order = self.get_order(account_id, market_id=market_id)
17041739
self.logger.info(f"Order: {order}")
17051740

1706-
min_publish_delay = self.market_meta[market_id]["config"][
1741+
min_publish_delay = self.market_meta[market_id]["system_config"][
17071742
"pyth_publish_time_min"
17081743
]
1709-
max_publish_delay = self.market_meta[market_id]["config"][
1744+
max_publish_delay = self.market_meta[market_id]["system_config"][
17101745
"pyth_publish_time_max"
17111746
]
17121747

@@ -1720,12 +1755,6 @@ def settle_order(
17201755

17211756
for attempt in range(max_attempts):
17221757
try:
1723-
# log the block info
1724-
block = self.snx.web3.eth.get_block("latest")
1725-
self.logger.info(
1726-
f"Block number: {block['number']} | timestamp {block['timestamp']}"
1727-
)
1728-
17291758
# Fetch the latest Pyth price update data
17301759
pyth_data = self.snx.pyth.get_price_from_ids(
17311760
[pyth_price_feed_id], publish_time=publish_time
@@ -1818,3 +1847,34 @@ def pay_debt(
18181847
return tx_hash
18191848
else:
18201849
return tx_params
1850+
1851+
def liquidate(
1852+
self,
1853+
account_id: int = None,
1854+
market_id: int = None,
1855+
market_name: str = None,
1856+
margin_only: bool = False,
1857+
submit: bool = False,
1858+
):
1859+
""" """
1860+
market_id, market_name = self._resolve_market(market_id, market_name)
1861+
if account_id is None:
1862+
account_id = self.default_account_id
1863+
1864+
fn_name = "liquidatePosition" if not margin_only else "liquidateMarginOnly"
1865+
tx_params = write_erc7412(
1866+
self.snx,
1867+
self.market_proxy,
1868+
fn_name,
1869+
[account_id, market_id],
1870+
)
1871+
if submit:
1872+
tx_hash = self.snx.execute_transaction(tx_params)
1873+
self.logger.info(
1874+
f"Liquidating account {account_id} in market {market_name}"
1875+
)
1876+
1877+
self.logger.info(f"{fn_name} tx: {tx_hash}")
1878+
return tx_hash
1879+
else:
1880+
return tx_params

src/synthetix/perps/perps_utils.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from eth_utils import encode_hex
2+
from ..utils import wei_to_ether
3+
24

35
def unpack_bfp_configuration(config_data):
46
"""
@@ -15,22 +17,22 @@ def unpack_bfp_configuration(config_data):
1517
"pyth_publish_time_max": config_data[4],
1618
"min_order_age": config_data[5],
1719
"max_order_age": config_data[6],
18-
"min_keeper_fee_usd": config_data[7],
19-
"max_keeper_fee_usd": config_data[8],
20-
"keeper_profit_margin_usd": config_data[9],
21-
"keeper_profit_margin_percent": config_data[10],
20+
"min_keeper_fee_usd": wei_to_ether(config_data[7]),
21+
"max_keeper_fee_usd": wei_to_ether(config_data[8]),
22+
"keeper_profit_margin_usd": wei_to_ether(config_data[9]),
23+
"keeper_profit_margin_percent": wei_to_ether(config_data[10]),
2224
"keeper_settlement_gas_units": config_data[11],
2325
"keeper_cancellation_gas_units": config_data[12],
2426
"keeper_liquidation_gas_units": config_data[13],
2527
"keeper_flag_gas_units": config_data[14],
2628
"keeper_liquidate_margin_gas_units": config_data[15],
2729
"keeper_liquidation_endorsed": config_data[16],
2830
"collateral_discount_scalar": config_data[17],
29-
"min_collateral_discount": config_data[18],
30-
"max_collateral_discount": config_data[19],
31-
"utilization_breakpoint_percent": config_data[20],
32-
"low_utilization_slope_percent": config_data[21],
33-
"high_utilization_slope_percent": config_data[22],
31+
"min_collateral_discount": wei_to_ether(config_data[18]),
32+
"max_collateral_discount": wei_to_ether(config_data[19]),
33+
"utilization_breakpoint_percent": wei_to_ether(config_data[20]),
34+
"low_utilization_slope_percent": wei_to_ether(config_data[21]),
35+
"high_utilization_slope_percent": wei_to_ether(config_data[22]),
3436
}
3537

3638

@@ -65,20 +67,20 @@ def unpack_bfp_configuration_by_id(config_data):
6567
return {
6668
"oracle_node_id": encode_hex(oracle_node_id),
6769
"pyth_price_feed_id": encode_hex(pyth_price_feed_id),
68-
"maker_fee": maker_fee,
69-
"taker_fee": taker_fee,
70-
"max_market_size": max_market_size,
71-
"max_funding_velocity": max_funding_velocity,
72-
"skew_scale": skew_scale,
73-
"funding_velocity_clamp": funding_velocity_clamp,
74-
"min_credit_percent": min_credit_percent,
75-
"min_margin_usd": min_margin_usd,
76-
"min_margin_ratio": min_margin_ratio,
77-
"incremental_margin_scalar": incremental_margin_scalar,
78-
"maintenance_margin_scalar": maintenance_margin_scalar,
79-
"max_initial_margin_ratio": max_initial_margin_ratio,
80-
"liquidation_reward_percent": liquidation_reward_percent,
81-
"liquidation_limit_scalar": liquidation_limit_scalar,
82-
"liquidation_window_duration": liquidation_window_duration,
83-
"liquidation_max_pd": liquidation_max_pd,
70+
"maker_fee": wei_to_ether(maker_fee),
71+
"taker_fee": wei_to_ether(taker_fee),
72+
"max_market_size": wei_to_ether(max_market_size),
73+
"max_funding_velocity": wei_to_ether(max_funding_velocity),
74+
"skew_scale": wei_to_ether(skew_scale),
75+
"funding_velocity_clamp": wei_to_ether(funding_velocity_clamp),
76+
"min_credit_percent": wei_to_ether(min_credit_percent),
77+
"min_margin_usd": wei_to_ether(min_margin_usd),
78+
"min_margin_ratio": wei_to_ether(min_margin_ratio),
79+
"incremental_margin_scalar": wei_to_ether(incremental_margin_scalar),
80+
"maintenance_margin_scalar": wei_to_ether(maintenance_margin_scalar),
81+
"max_initial_margin_ratio": wei_to_ether(max_initial_margin_ratio),
82+
"liquidation_reward_percent": wei_to_ether(liquidation_reward_percent),
83+
"liquidation_limit_scalar": wei_to_ether(liquidation_limit_scalar),
84+
"liquidation_window_duration": wei_to_ether(liquidation_window_duration),
85+
"liquidation_max_pd": wei_to_ether(liquidation_max_pd),
8486
}

0 commit comments

Comments
 (0)