@@ -101,20 +101,36 @@ async def _get_fresh_blockhash(self, max_attempts: int = 3) -> Hash:
101101 commitment = self .commitment
102102 )
103103
104- if blockhash_resp .value and blockhash_resp .value .blockhash :
104+ # Handle response type safely - check if response has .value attribute
105+ if (
106+ hasattr (blockhash_resp , "value" )
107+ and blockhash_resp .value
108+ and hasattr (blockhash_resp .value , "blockhash" )
109+ ):
105110 logger .debug (f"Fresh blockhash obtained from { rpc_to_try } " )
106111 return blockhash_resp .value .blockhash # Return Hash object directly
107112 else :
108- raise ConnectionError ("Blockhash response was empty" )
113+ raise ConnectionError (
114+ f"Blockhash response was invalid: { type (blockhash_resp )} "
115+ )
109116
110117 except Exception as e :
111118 logger .warning (
112119 f"Failed to get blockhash from { rpc_to_try } "
113120 f"(attempt { attempt + 1 } /{ max_attempts } ): { e } "
114121 )
115122
116- # Try next RPC endpoint
123+ # Try next RPC endpoint on any error
117124 self ._current_rpc_index += 1
125+
126+ # Close current client to force reconnection with new RPC
127+ if self ._client :
128+ try :
129+ await self ._client .close ()
130+ except Exception : # noqa: S110
131+ pass # Ignore close errors
132+ self ._client = None
133+
118134 if attempt < max_attempts - 1 :
119135 continue
120136
@@ -147,7 +163,7 @@ async def health_check(self) -> bool:
147163 try :
148164 # Test basic connection by getting epoch info (lightweight check)
149165 epoch_info = await self .client .get_epoch_info (commitment = self .commitment )
150- if not epoch_info .value :
166+ if not hasattr ( epoch_info , "value" ) or not epoch_info .value :
151167 logger .warning ("Failed to fetch epoch info" )
152168 return False
153169
@@ -193,7 +209,7 @@ async def get_account_info(
193209 response = await self .client .get_account_info (
194210 pubkey , encoding = encoding , commitment = self .commitment
195211 )
196- if response .value is None :
212+ if not hasattr ( response , "value" ) or response .value is None :
197213 return None
198214
199215 return {
@@ -333,7 +349,9 @@ async def send_transaction(
333349 ),
334350 )
335351
336- signature = str (response .value )
352+ # Handle response type safely - sometimes response is int
353+ # instead of object with .value attribute
354+ signature = str (getattr (response , "value" , response ))
337355 logger .info (f"Transaction sent successfully: { signature } " )
338356 return signature
339357
@@ -403,12 +421,18 @@ async def simulate_transaction(
403421 tx_copy , commitment = self .commitment
404422 )
405423
406- return {
407- "logs" : response .value .logs ,
408- "err" : response .value .err ,
409- "accounts" : response .value .accounts ,
410- "units_consumed" : response .value .units_consumed ,
411- }
424+ # Handle response type safely
425+ if hasattr (response , "value" ) and response .value :
426+ return {
427+ "logs" : response .value .logs ,
428+ "err" : response .value .err ,
429+ "accounts" : response .value .accounts ,
430+ "units_consumed" : response .value .units_consumed ,
431+ }
432+ else :
433+ raise TransactionError (
434+ f"Invalid simulation response: { type (response )} "
435+ )
412436
413437 except Exception as e :
414438 last_error = e
@@ -450,7 +474,8 @@ async def get_balance(self, pubkey: PublicKey) -> int:
450474 """
451475 try :
452476 response = await self .client .get_balance (pubkey , commitment = self .commitment )
453- return int (response .value )
477+ # Handle response type safely - cast response to int directly
478+ return int (getattr (response , "value" , 0 )) # type: ignore[arg-type]
454479 except Exception as e :
455480 raise ConnectionError (f"Failed to get balance: { e } " )
456481
@@ -473,12 +498,17 @@ async def get_token_account_balance(
473498 response = await self .client .get_token_account_balance (
474499 token_account , commitment = self .commitment
475500 )
476- return {
477- "amount" : response .value .amount ,
478- "decimals" : response .value .decimals ,
479- "ui_amount" : response .value .ui_amount ,
480- "ui_amount_string" : response .value .ui_amount_string ,
481- }
501+ if hasattr (response , "value" ) and response .value :
502+ return {
503+ "amount" : response .value .amount ,
504+ "decimals" : response .value .decimals ,
505+ "ui_amount" : response .value .ui_amount ,
506+ "ui_amount_string" : response .value .ui_amount_string ,
507+ }
508+ else :
509+ raise ConnectionError (
510+ f"Invalid token balance response: { type (response )} "
511+ )
482512 except Exception as e :
483513 raise ConnectionError (f"Failed to get token balance: { e } " )
484514
0 commit comments