Skip to content

Commit ebaaf3b

Browse files
author
1bcMax
committed
v0.3.7: Add RPC fallback for reliable balance checks
- get_balance() now tries 3 RPCs before failing - Order: publicnode.com, mainnet.base.org, meowrpc.com - Fixes false $0 balance issue with unreliable RPCs
1 parent 4e05c5a commit ebaaf3b

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

blockrun_llm/client.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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."""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "blockrun-llm"
7-
version = "0.3.6"
7+
version = "0.3.7"
88
description = "BlockRun SDK - Pay-per-request AI (LLM & Image) via x402 on Base"
99
readme = "README.md"
1010
license = "MIT"

0 commit comments

Comments
 (0)