-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickstart.py
More file actions
executable file
·171 lines (139 loc) · 5.68 KB
/
quickstart.py
File metadata and controls
executable file
·171 lines (139 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
BlackRoad SDK Quickstart Example
=================================
This example demonstrates basic usage of the BlackRoad SDK,
including authentication, blockchain operations, and agent execution.
"""
import os
import sys
# Add parent directory to path to import blackroad
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from blackroad import (
AuthenticationError,
BlackRoadClient,
NotFoundError,
ValidationError,
)
def main() -> None:
"""Run the quickstart example."""
# Initialize the client
print("Initializing BlackRoad client...")
client = BlackRoadClient(
base_url=os.getenv("BLACKROAD_BASE_URL", "http://localhost:8000"),
timeout=30,
max_retries=3,
)
# Example 1: User Registration and Authentication
print("\n=== User Registration and Authentication ===")
try:
# Register a new user
print("Registering new user...")
user = client.auth.register(
username="demo_user",
email="demo@example.com",
password="SecurePassword123!",
full_name="Demo User",
)
print(f"User registered successfully: {user.username}")
print(f"Wallet address: {user.wallet_address}")
print(f"Starting balance: {user.balance} RoadCoin")
except AuthenticationError as e:
print(f"User already exists, logging in instead...")
try:
# Login
print("\nLogging in...")
token = client.auth.login(username="demo_user", password="SecurePassword123!")
print(f"Login successful! Token type: {token.token_type}")
# Set the authentication token
client.set_token(token.access_token)
# Get current user info
current_user = client.auth.me()
print(f"\nCurrent user: {current_user.username}")
print(f"Email: {current_user.email}")
print(f"Balance: {current_user.balance} RoadCoin")
except AuthenticationError as e:
print(f"Authentication error: {e}")
return
# Example 2: Blockchain Operations
print("\n=== Blockchain Operations ===")
try:
# Get wallet information
print("\nGetting wallet information...")
wallet = client.blockchain.get_wallet()
print(f"Wallet address: {wallet.address}")
print(f"Balance: {wallet.balance} RoadCoin")
# Get blockchain statistics
print("\nGetting blockchain statistics...")
stats = client.blockchain.get_stats()
print(f"Latest block: #{stats.latest_block_index}")
print(f"Total blocks: {stats.total_blocks}")
print(f"Total transactions: {stats.total_transactions}")
print(f"Pending transactions: {stats.pending_transactions}")
print(f"Mining difficulty: {stats.difficulty}")
print(f"Mining reward: {stats.mining_reward} RoadCoin")
# Get recent blocks
print("\nGetting recent blocks...")
blocks = client.blockchain.get_blocks(limit=5)
print(f"Retrieved {len(blocks)} blocks:")
for block in blocks:
print(f" Block #{block.index}: {block.hash[:16]}... ({block.transaction_count} txs)")
# Get transaction history
print("\nGetting transaction history...")
transactions = client.blockchain.get_transactions(limit=5)
print(f"Retrieved {len(transactions)} transactions:")
for tx in transactions:
print(
f" {tx.transaction_hash[:16]}...: "
f"{tx.amount} RoadCoin "
f"({'confirmed' if tx.is_confirmed else 'pending'})"
)
except Exception as e:
print(f"Blockchain error: {e}")
# Example 3: Creating a Transaction (if we have a recipient)
print("\n=== Creating a Transaction ===")
# Note: This will fail if there's no other user to send to
# You would need to create another user first
print("Skipping transaction creation (requires another user)")
# Example 4: Mining a Block
print("\n=== Mining a Block ===")
try:
print("Mining a new block (this may take a moment)...")
block = client.blockchain.mine_block()
print(f"Block mined successfully!")
print(f"Block #{block.index}")
print(f"Hash: {block.hash}")
print(f"Nonce: {block.nonce}")
print(f"Reward: {block.reward} RoadCoin")
print(f"Transactions: {block.transaction_count}")
# Check updated balance
wallet = client.blockchain.get_wallet()
print(f"\nUpdated balance: {wallet.balance} RoadCoin")
except Exception as e:
print(f"Mining error: {e}")
# Example 5: Agent Operations (if agent endpoints are available)
print("\n=== Agent Operations ===")
try:
# List available agents
print("Listing available agents...")
agents = client.agents.list_agents(category="devops")
if agents:
print(f"Found {len(agents)} DevOps agents:")
for agent in agents[:5]: # Show first 5
print(f" - {agent.name} (v{agent.version}): {agent.description}")
else:
print("No agents found (agent endpoints may not be implemented yet)")
except NotFoundError:
print("Agent endpoints not available")
except Exception as e:
print(f"Agent error: {e}")
# Cleanup
print("\n=== Cleanup ===")
client.close()
print("Client closed successfully")
print("\n=== Quickstart Complete ===")
print("Check out the other examples for more advanced usage:")
print(" - examples/agents_example.py - Agent management and execution")
print(" - examples/blockchain_example.py - Advanced blockchain operations")
if __name__ == "__main__":
main()