@@ -605,13 +605,27 @@ def get_balance(self) -> float:
605605 "id" : 1 ,
606606 }
607607
608- # Use public Base RPC
609- response = httpx .post ("https://mainnet.base.org" , json = payload , timeout = 10 )
610- result = response .json ().get ("result" , "0x0" )
611-
612- # Convert from hex and normalize (USDC has 6 decimals)
613- balance_raw = int (result , 16 )
614- return balance_raw / 1_000_000
608+ # Try multiple RPCs for reliability
609+ rpcs = [
610+ "https://base.publicnode.com" ,
611+ "https://mainnet.base.org" ,
612+ "https://base.meowrpc.com" ,
613+ ]
614+
615+ last_error = None
616+ for rpc in rpcs :
617+ try :
618+ response = httpx .post (rpc , json = payload , timeout = 10 )
619+ result = response .json ().get ("result" , "0x0" )
620+ # Convert from hex and normalize (USDC has 6 decimals)
621+ balance_raw = int (result , 16 )
622+ return balance_raw / 1_000_000
623+ except Exception as e :
624+ last_error = e
625+ continue
626+
627+ # If all RPCs failed, raise the last error
628+ raise last_error or Exception ("All RPCs failed" )
615629
616630 def close (self ):
617631 """Close the HTTP client."""
@@ -932,14 +946,28 @@ async def get_balance(self) -> float:
932946 "id" : 1 ,
933947 }
934948
935- # Use public Base RPC
936- async with httpx .AsyncClient (timeout = 10 ) as http_client :
937- response = await http_client .post ("https://mainnet.base.org" , json = payload )
938- result = response .json ().get ("result" , "0x0" )
949+ # Try multiple RPCs for reliability
950+ rpcs = [
951+ "https://base.publicnode.com" ,
952+ "https://mainnet.base.org" ,
953+ "https://base.meowrpc.com" ,
954+ ]
939955
940- # Convert from hex and normalize (USDC has 6 decimals)
941- balance_raw = int (result , 16 )
942- return balance_raw / 1_000_000
956+ last_error = None
957+ async with httpx .AsyncClient (timeout = 10 ) as http_client :
958+ for rpc in rpcs :
959+ try :
960+ response = await http_client .post (rpc , json = payload )
961+ result = response .json ().get ("result" , "0x0" )
962+ # Convert from hex and normalize (USDC has 6 decimals)
963+ balance_raw = int (result , 16 )
964+ return balance_raw / 1_000_000
965+ except Exception as e :
966+ last_error = e
967+ continue
968+
969+ # If all RPCs failed, raise the last error
970+ raise last_error or Exception ("All RPCs failed" )
943971
944972 async def close (self ):
945973 """Close the async HTTP client."""
0 commit comments