-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
113 lines (87 loc) · 3.58 KB
/
client.py
File metadata and controls
113 lines (87 loc) · 3.58 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
"""
Rigoblock Agent SDK — Python Example
Demonstrates calling the Rigoblock Agent Trader API with x402 payments.
Requires a wallet with USDC on Base for payment (~$0.01 per request).
Setup:
pip install requests eth-account
Usage:
export PRIVATE_KEY=0x... # wallet with USDC on Base
python client.py quote
python client.py chat 0xYourVault "swap 0.01 ETH for USDC on Base"
NOTE: This is a simplified example showing the raw x402 handshake.
For production, use a proper x402 Python client when available.
The x402 payment construction below shows the protocol flow — you'll need
to implement EIP-712 signing for the actual payment permit.
"""
import json
import os
import sys
import requests
BASE_URL = "https://trader.rigoblock.com"
def get_quote(sell: str = "ETH", buy: str = "USDC", amount: str = "1", chain: str = "base"):
"""Get a DEX price quote ($0.002 USDC per request)."""
url = f"{BASE_URL}/api/quote"
params = {"sell": sell, "buy": buy, "amount": amount, "chain": chain}
print(f"Fetching quote: {amount} {sell} → {buy} on {chain}...\n")
# First request returns 402 with payment instructions
res = requests.get(url, params=params)
if res.status_code == 402:
payment_info = res.json()
print("x402 Payment Required:")
print(json.dumps(payment_info, indent=2))
print("\n--- To complete the flow ---")
print("1. Parse the 'accepts' array for payment details")
print("2. Sign an EIP-712 permit for the USDC amount")
print("3. Retry with the X-Payment header")
print("\nSee the TypeScript example for a complete x402 implementation.")
return payment_info
data = res.json()
print("Quote response:")
print(json.dumps(data, indent=2))
return data
def chat(vault_address: str, message: str, chain_id: int = 8453):
"""Send a chat message to the AI agent ($0.01 USDC per request)."""
url = f"{BASE_URL}/api/chat"
payload = {
"messages": [{"role": "user", "content": message}],
"vaultAddress": vault_address,
"chainId": chain_id,
}
print(f'Chat request: "{message}"\n')
res = requests.post(url, json=payload)
if res.status_code == 402:
payment_info = res.json()
print("x402 Payment Required:")
print(json.dumps(payment_info, indent=2))
print("\n--- To complete the flow ---")
print("1. Parse the 'accepts' array for payment details")
print("2. Sign an EIP-712 permit for the USDC amount")
print("3. Retry with the X-Payment header")
return payment_info
data = res.json()
print("Chat response:")
print(json.dumps(data, indent=2))
return data
def main():
if len(sys.argv) < 2:
print("Usage:")
print(" python client.py quote [sell] [buy] [amount] [chain]")
print(" python client.py chat <vaultAddress> <message>")
sys.exit(1)
command = sys.argv[1]
if command == "quote":
sell = sys.argv[2] if len(sys.argv) > 2 else "ETH"
buy = sys.argv[3] if len(sys.argv) > 3 else "USDC"
amount = sys.argv[4] if len(sys.argv) > 4 else "1"
chain = sys.argv[5] if len(sys.argv) > 5 else "base"
get_quote(sell, buy, amount, chain)
elif command == "chat":
vault = sys.argv[2] if len(sys.argv) > 2 else "0xYourVaultAddress"
message = sys.argv[3] if len(sys.argv) > 3 else "swap 0.01 ETH for USDC on Base"
chat(vault, message)
else:
print(f"Unknown command: {command}")
print("Use 'quote' or 'chat'")
sys.exit(1)
if __name__ == "__main__":
main()