Skip to content

Commit d75c04a

Browse files
fix: only craete test entity if client account has funding
1 parent 0e74eed commit d75c04a

File tree

3 files changed

+1616
-1586
lines changed

3 files changed

+1616
-1586
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Starter template for Arkiv Python SDK"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
dependencies = [
8-
"arkiv-sdk[dev]>=1.0.0a9",
8+
"arkiv-sdk[dev]>=1.0.0a10",
99
]
1010

1111
[build-system]

src/arkiv_starter/01_clients.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,28 @@
1111
Run this example: uv run python -m arkiv_starter.01_clients
1212
"""
1313

14-
from typing import cast
14+
import socket
15+
from typing import Optional, cast
1516
from web3.providers.base import BaseProvider
1617
from arkiv import Arkiv, NamedAccount
1718
from arkiv.provider import ProviderBuilder
18-
19+
from urllib.parse import urlparse
20+
21+
EXTERNAL_RPC_URL: str = "https://mendoza.hoodi.arkiv.network/rpc"
22+
23+
def is_rpc_reachable(rpc_url: str, timeout: float = 2.0) -> bool:
24+
"""Check if RPC endpoint is reachable using a simple socket connection."""
25+
try:
26+
parsed = urlparse(rpc_url)
27+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
28+
sock.settimeout(timeout)
29+
host = parsed.hostname or "127.0.0.1"
30+
port = parsed.port or (443 if parsed.scheme == "https" else 8545)
31+
result = sock.connect_ex((host, port))
32+
sock.close()
33+
return result == 0
34+
except Exception:
35+
return False
1936

2037
print("=" * 70)
2138
print("PATTERN 1: Default Constructor (Simplest)")
@@ -46,12 +63,18 @@
4663
print("=" * 70)
4764
print("\n🚀 Creating client with custom provider...")
4865
print(" - Connect to specific RPC URL")
49-
print(" - Useful for remote nodes or specific configurations\n")
66+
print(" - Useful for remote nodes or specific configurations")
67+
print(" - Fallback to local node if external node not reachable\n")
5068

51-
# Use the node from the default client
5269
local_node = client.node
53-
assert local_node is not None, "Default client should have started a node"
54-
rpc_url = local_node.http_url
70+
if is_rpc_reachable(EXTERNAL_RPC_URL):
71+
print(f"🌐 External RPC URL is reachable: {EXTERNAL_RPC_URL}\n")
72+
rpc_url = EXTERNAL_RPC_URL
73+
else:
74+
print(f"⚠️ External RPC URL is NOT reachable: {EXTERNAL_RPC_URL}")
75+
assert local_node is not None, "Default client should have started a node"
76+
rpc_url = local_node.http_url
77+
print(f" Falling back to local node RPC URL: {rpc_url}\n")
5578

5679
provider = cast(BaseProvider, ProviderBuilder().custom(url=rpc_url).build())
5780
# Note: When only providing provider, client doesn't auto-create account
@@ -62,7 +85,6 @@
6285
print(f" Provider: {provider}")
6386
print(f" Note: No default account - you must provide one for transactions\n")
6487

65-
6688
print("=" * 70)
6789
print("PATTERN 3: Custom Account (Specific Private Key)")
6890
print("=" * 70)
@@ -71,22 +93,26 @@
7193
print(" - Useful for production with specific keys\n")
7294

7395
account = NamedAccount.create("custom-account")
74-
local_node.fund_account(account) # Fund the new account
96+
if local_node:
97+
local_node.fund_account(account) # Fund the new account
7598

7699
custom_account_client = Arkiv(provider, account=account)
100+
custom_account_balance = custom_account_client.eth.get_balance(account.address)
77101

78102
print(f"✅ Client with custom account created!")
79103
print(f" Account: {account.address}")
80104
print(f" Account name: {account.name}")
81-
print(f" Balance: {custom_account_client.eth.get_balance(account.address)/10**18} ETH\n")
105+
print(f" Balance: {custom_account_balance/10**18} ETH\n")
82106

83107
# Quick test
84-
entity_key2, receipt2 = custom_account_client.arkiv.create_entity(
85-
payload=b"Test from custom account",
86-
content_type="text/plain",
87-
expires_in=3600
88-
)
89-
print(f"✅ Test entity created: {entity_key2}\n")
108+
if custom_account_balance > 0:
109+
print("💸 Creating test entity with custom account...")
110+
entity_key2, receipt2 = custom_account_client.arkiv.create_entity(
111+
payload=b"Test from custom account",
112+
content_type="text/plain",
113+
expires_in=3600
114+
)
115+
print(f"✅ Test entity created: {entity_key2}\n")
90116

91117

92118
print("=" * 70)

0 commit comments

Comments
 (0)