88from hyperliquid .utils .constants import TESTNET_API_URL , MAINNET_API_URL
99
1010from kms_signer import KMSSigner
11+ from metrics import Metrics
1112from price_state import PriceState
1213
1314
@@ -17,7 +18,7 @@ class Publisher:
1718
1819 See https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/hip-3-deployer-actions
1920 """
20- def __init__ (self , config : dict , price_state : PriceState ):
21+ def __init__ (self , config : dict , price_state : PriceState , metrics : Metrics ):
2122 self .publish_interval = float (config ["hyperliquid" ]["publish_interval" ])
2223 self .kms_signer = None
2324 self .enable_kms = False
@@ -43,40 +44,54 @@ def __init__(self, config: dict, price_state: PriceState):
4344 self .enable_publish = config ["hyperliquid" ].get ("enable_publish" , False )
4445
4546 self .price_state = price_state
47+ self .metrics = metrics
4648
4749 async def run (self ):
4850 while True :
4951 await asyncio .sleep (self .publish_interval )
52+ try :
53+ self .publish ()
54+ except Exception as e :
55+ logger .exception ("Publisher.publish() exception: {}" , e )
5056
51- oracle_pxs = {}
52- oracle_px = self .price_state .get_current_oracle_price ()
53- if not oracle_px :
54- logger .error ("No valid oracle price available!" )
55- return
56- else :
57- logger .debug ("Current oracle price: {}" , oracle_px )
58- oracle_pxs [self .market_symbol ] = oracle_px
57+ def publish (self ):
58+ oracle_pxs = {}
59+ oracle_px = self .price_state .get_current_oracle_price ()
60+ if not oracle_px :
61+ logger .error ("No valid oracle price available" )
62+ self .metrics .no_oracle_price_counter .add (1 )
63+ return
64+ else :
65+ logger .debug ("Current oracle price: {}" , oracle_px )
66+ oracle_pxs [self .market_symbol ] = oracle_px
5967
60- mark_pxs = []
61- #if self.price_state.hl_mark_price:
62- # mark_pxs.append({self.market_symbol: self.price_state.hl_mark_price})
68+ mark_pxs = []
69+ #if self.price_state.hl_mark_price:
70+ # mark_pxs.append({self.market_symbol: self.price_state.hl_mark_price})
6371
64- external_perp_pxs = {}
72+ external_perp_pxs = {}
73+
74+ if self .enable_publish :
75+ if self .enable_kms :
76+ push_response = self .kms_signer .set_oracle (
77+ dex = self .market_name ,
78+ oracle_pxs = oracle_pxs ,
79+ all_mark_pxs = mark_pxs ,
80+ external_perp_pxs = external_perp_pxs ,
81+ )
82+ else :
83+ push_response = self .oracle_publisher_exchange .perp_deploy_set_oracle (
84+ dex = self .market_name ,
85+ oracle_pxs = oracle_pxs ,
86+ all_mark_pxs = mark_pxs ,
87+ external_perp_pxs = external_perp_pxs ,
88+ )
6589
66- if self .enable_publish :
67- if self .enable_kms :
68- push_response = self .kms_signer .set_oracle (
69- dex = self .market_name ,
70- oracle_pxs = oracle_pxs ,
71- all_mark_pxs = mark_pxs ,
72- external_perp_pxs = external_perp_pxs ,
73- )
74- else :
75- push_response = self .oracle_publisher_exchange .perp_deploy_set_oracle (
76- dex = self .market_name ,
77- oracle_pxs = oracle_pxs ,
78- all_mark_pxs = mark_pxs ,
79- external_perp_pxs = external_perp_pxs ,
80- )
81- # TODO: Look at specific error responses and log/alert accordingly
82- logger .info ("publish: push response: {}" , push_response )
90+ # TODO: Look at specific error responses and log/alert accordingly
91+ logger .debug ("publish: push response: {} {}" , push_response , type (push_response ))
92+ status = push_response .get ("status" , "" )
93+ if status == "ok" :
94+ self .metrics .successful_push_counter .add (1 )
95+ elif status == "err" :
96+ self .metrics .failed_push_counter .add (1 )
97+ logger .error ("publish: publish error: {}" , push_response )
0 commit comments