-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcommands.rs
More file actions
391 lines (361 loc) · 13.3 KB
/
Copy pathcommands.rs
File metadata and controls
391 lines (361 loc) · 13.3 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use crate::agent_deposit::{
agent_deposit_address, encode_uint256_call, fetch_chain_id, format_balance_response,
format_unsigned_tx, read_balance, read_registered, selectors, Action,
};
use crate::output::{emit, Mode};
use crate::rpc::{hex_to_u64, rpc_call, wei_hex_to_eth};
use clap::Command;
use colored::Colorize;
use eyre::{eyre, Result};
use serde_json::{json, Value};
struct SupportedChain {
chain_id: u64,
name: &'static str,
rpc_default: &'static str,
explorer: &'static str,
usdc: &'static str,
uniswap_v3_quoter: Option<&'static str>,
}
const SUPPORTED_CHAINS: &[SupportedChain] = &[
SupportedChain {
chain_id: 42161,
name: "Arbitrum One",
rpc_default: "https://arb1.arbitrum.io/rpc",
explorer: "https://arbiscan.io",
usdc: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
uniswap_v3_quoter: Some("0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6"),
},
SupportedChain {
chain_id: 421614,
name: "Arbitrum Sepolia",
rpc_default: "https://sepolia-rollup.arbitrum.io/rpc",
explorer: "https://sepolia.arbiscan.io",
usdc: "0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d",
uniswap_v3_quoter: None,
},
];
// ── block ──
pub async fn block(rpc: &str, block: &str, mode: Mode) -> Result<()> {
let block_param = if block == "latest" || block == "earliest" || block == "pending" {
json!(block)
} else {
let n: u64 = block.parse().map_err(|_| eyre!("invalid block number"))?;
json!(format!("0x{n:x}"))
};
let result = rpc_call(rpc, "eth_getBlockByNumber", json!([block_param, false])).await?;
// Annotate with decoded fields for humans
let mut annotated = result.clone();
if let Some(obj) = annotated.as_object_mut() {
if let Some(num) = obj.get("number").and_then(|v| v.as_str()) {
if let Ok(n) = hex_to_u64(num) {
obj.insert("number_decimal".to_string(), json!(n));
}
}
if let Some(ts) = obj.get("timestamp").and_then(|v| v.as_str()) {
if let Ok(t) = hex_to_u64(ts) {
obj.insert("timestamp_decimal".to_string(), json!(t));
}
}
}
emit(mode, "Block", &annotated);
Ok(())
}
// ── tx ──
pub async fn tx(rpc: &str, hash: &str, mode: Mode) -> Result<()> {
let result = rpc_call(rpc, "eth_getTransactionByHash", json!([hash])).await?;
if result.is_null() {
return Err(eyre!("Transaction not found: {hash}"));
}
emit(mode, "Transaction", &result);
Ok(())
}
// ── balance ──
pub async fn balance(rpc: &str, address: &str, mode: Mode) -> Result<()> {
let result = rpc_call(rpc, "eth_getBalance", json!([address, "latest"])).await?;
let wei_hex = result.as_str().unwrap_or("0x0");
let eth = wei_hex_to_eth(wei_hex)?;
let out = json!({
"address": address,
"balance_wei": wei_hex,
"balance_eth": eth,
});
emit(mode, "Balance", &out);
Ok(())
}
// ── token balance ──
pub async fn token_balance(rpc: &str, token: &str, address: &str, mode: Mode) -> Result<()> {
// balanceOf(address) selector = 0x70a08231
let address = address.trim_start_matches("0x");
let padded = format!("{address:0>64}");
let data = format!("0x70a08231{padded}");
let result = rpc_call(
rpc,
"eth_call",
json!([{"to": token, "data": data}, "latest"]),
)
.await?;
let raw = result.as_str().unwrap_or("0x0");
// Also fetch decimals
let decimals_result = rpc_call(
rpc,
"eth_call",
json!([{"to": token, "data": "0x313ce567"}, "latest"]),
)
.await
.ok();
let decimals = decimals_result
.as_ref()
.and_then(|v| v.as_str())
.and_then(|s| hex_to_u64(s).ok())
.unwrap_or(18);
let balance_raw = u128::from_str_radix(raw.trim_start_matches("0x"), 16).unwrap_or(0);
let balance_human = balance_raw as f64 / 10f64.powi(decimals as i32);
let out = json!({
"token": token,
"address": address,
"decimals": decimals,
"balance_raw": raw,
"balance": balance_human,
});
emit(mode, "Token Balance", &out);
Ok(())
}
// ── call ──
pub async fn call(rpc: &str, to: &str, data: &str, mode: Mode) -> Result<()> {
let result = rpc_call(rpc, "eth_call", json!([{"to": to, "data": data}, "latest"])).await?;
let out = json!({
"to": to,
"data": data,
"result": result,
});
emit(mode, "Contract Call", &out);
Ok(())
}
// ── gas ──
pub async fn gas(rpc: &str, mode: Mode) -> Result<()> {
let gas_price = rpc_call(rpc, "eth_gasPrice", json!([])).await?;
let block_num = rpc_call(rpc, "eth_blockNumber", json!([])).await?;
let gas_hex = gas_price.as_str().unwrap_or("0x0");
let gwei = u128::from_str_radix(gas_hex.trim_start_matches("0x"), 16).unwrap_or(0) as f64 / 1e9;
let out = json!({
"gas_price_wei": gas_hex,
"gas_price_gwei": gwei,
"block_number": block_num,
});
emit(mode, "Gas", &out);
Ok(())
}
// ── watch ──
pub async fn watch(rpc: &str, target: &str, mode: Mode) -> Result<()> {
use std::time::Duration;
use tokio::time::sleep;
if target == "blocks" {
let mut last = 0u64;
loop {
let result = rpc_call(rpc, "eth_blockNumber", json!([])).await?;
if let Some(hex) = result.as_str() {
let n = hex_to_u64(hex).unwrap_or(0);
if n != last {
let out = json!({ "block": n, "hex": hex });
emit(mode, "New Block", &out);
last = n;
}
}
sleep(Duration::from_secs(2)).await;
}
} else {
Err(eyre!(
"Unsupported watch target: {target}. Use 'blocks' for now."
))
}
}
// ── exec (generic RPC passthrough — agent-friendly) ──
pub async fn exec(rpc: &str, method: &str, params: &str, mode: Mode) -> Result<()> {
let params_val: Value =
serde_json::from_str(params).map_err(|e| eyre!("Invalid params JSON: {e}"))?;
let result = rpc_call(rpc, method, params_val).await?;
let out = json!({
"method": method,
"result": result,
});
emit(mode, "Exec", &out);
Ok(())
}
fn chain_inventory() -> Vec<Value> {
SUPPORTED_CHAINS
.iter()
.map(|chain| {
json!({
"chain_id": chain.chain_id,
"name": chain.name,
"rpc_default": chain.rpc_default,
"explorer": chain.explorer,
"contracts": {
"usdc": chain.usdc,
"agent_deposit": agent_deposit_address(chain.chain_id),
"uniswap_v3_quoter": chain.uniswap_v3_quoter,
},
})
})
.collect()
}
fn subcommand_inventory(command: Command) -> Vec<Value> {
command
.get_subcommands()
.map(|subcommand| {
let args: Vec<Value> = subcommand
.get_arguments()
.map(|arg| {
json!({
"name": arg.get_id().as_str(),
"required": arg.is_required_set(),
"help": arg.get_help().map(|help| help.to_string()),
})
})
.collect();
json!({
"name": subcommand.get_name(),
"description": subcommand.get_about().map(|about| about.to_string()),
"args": args,
})
})
.collect()
}
pub(crate) fn info_inventory(command: Command) -> Value {
json!({
"name": "arbitrum-cli",
"version": env!("CARGO_PKG_VERSION"),
"brand": "kcolbchain",
"chains": chain_inventory(),
"subcommands": subcommand_inventory(command),
})
}
fn print_info_human(inventory: &Value) {
let check = "✓".green().bold();
let title = "arbitrum-cli info".bold();
println!("\n {check} {title}");
let divider = "─".repeat(72).dimmed();
println!(" {divider}");
let version = inventory["version"].as_str().unwrap_or_default();
let version_label = "version:".cyan();
println!(" {version_label} {version}");
let chains = "Chains".cyan().bold();
println!("\n {chains}");
println!(
" {:<18} {:<8} RPC Explorer",
"Name", "Chain"
);
for chain in inventory["chains"].as_array().into_iter().flatten() {
let name = chain["name"].as_str().unwrap_or_default();
let chain_id = chain["chain_id"].as_u64().unwrap_or_default();
let rpc_default = chain["rpc_default"].as_str().unwrap_or_default();
let explorer = chain["explorer"].as_str().unwrap_or_default();
println!(" {name:<18} {chain_id:<8} {rpc_default:<38} {explorer}");
let contracts = &chain["contracts"];
let usdc = contracts["usdc"].as_str().unwrap_or("not configured");
println!(" USDC: {usdc}");
let agent_deposit = contracts["agent_deposit"]
.as_str()
.unwrap_or("not configured");
println!(" AgentDeposit: {agent_deposit}");
let uniswap_v3_quoter = contracts["uniswap_v3_quoter"]
.as_str()
.unwrap_or("not configured");
println!(" Uniswap V3 Quoter: {uniswap_v3_quoter}");
}
let subcommands = "Subcommands".cyan().bold();
println!("\n {subcommands}");
println!(" Name Description");
for subcommand in inventory["subcommands"].as_array().into_iter().flatten() {
let name = subcommand["name"].as_str().unwrap_or_default();
let description = subcommand["description"].as_str().unwrap_or_default();
println!(" {name:<16} {description}");
}
println!();
}
// ── info ──
pub fn info(mode: Mode, command: Command) -> Result<()> {
let inventory = info_inventory(command);
match mode {
Mode::Json => emit(mode, "arbitrum-cli info", &inventory),
Mode::Human => print_info_human(&inventory),
}
Ok(())
}
// ── agent-deposit (Create Protocol) ──
//
// Phase 1 of Create Protocol is an agent-economy on Arbitrum: agents register,
// deposit USDC, execute tasks, earn fees. This command gives the CLI a
// first-class verb for the AgentDeposit contract so an LLM can do the full
// loop (check balance → prepare deposit/withdraw tx) from one binary.
//
// Reads go through `eth_call` and return decoded JSON. Writes return unsigned
// calldata — the CLI is strictly key-less; agents sign with switchboard or
// any wallet and broadcast via `exec eth_sendRawTransaction`.
pub async fn agent_deposit(
rpc: &str,
address: &str,
action_str: &str,
amount: Option<u128>,
contract_override: Option<&str>,
mode: Mode,
) -> Result<()> {
let action = Action::parse(action_str)?;
// Resolve contract: explicit override wins, else look up by chain id. We
// call eth_chainId either way so the emitted JSON records which chain
// the caller is actually talking to — avoids silent testnet/mainnet
// confusion when an agent is driving.
let chain_id = fetch_chain_id(rpc).await?;
let contract = match contract_override {
Some(c) => c.to_string(),
None => agent_deposit_address(chain_id)
.ok_or_else(|| {
eyre!(
"No AgentDeposit deployment registered for chain id {chain_id}. \
Pass --contract <address> or use an Arbitrum RPC.",
)
})?
.to_string(),
};
match action {
Action::Balance => {
let raw = read_balance(rpc, &contract, address).await?;
let out = format_balance_response(address, &contract, chain_id, raw);
emit(mode, "Agent Deposit Balance", &out);
}
Action::Registered => {
let is_reg = read_registered(rpc, &contract, address).await?;
let out = json!({
"agent": address,
"contract": contract,
"chain_id": chain_id,
"action": "registered",
"registered": is_reg,
});
emit(mode, "Agent Registration", &out);
}
Action::Deposit | Action::Withdraw => {
let amt = amount.ok_or_else(|| {
eyre!("--amount is required for deposit/withdraw (raw units; USDC = 6 decimals)")
})?;
let (selector, label, action_tag) = match action {
Action::Deposit => (selectors::DEPOSIT, "Agent Deposit (unsigned)", "deposit"),
Action::Withdraw => (selectors::WITHDRAW, "Agent Withdraw (unsigned)", "withdraw"),
_ => unreachable!(),
};
let data = encode_uint256_call(selector, amt)?;
let out = format_unsigned_tx(action_tag, address, &contract, chain_id, amt, &data);
emit(mode, label, &out);
}
}
Ok(())
}
// ── mcp ──
//
// Run a real Model Context Protocol server over stdio. MCP hosts (Claude
// Desktop, Cursor, …) spawn this binary and speak JSON-RPC 2.0 on
// stdin/stdout. The protocol surface (initialize / tools/list / tools/call)
// and tool execution live in `crate::mcp`; this is just the entry point.
pub async fn mcp(rpc: &str) -> Result<()> {
crate::mcp::serve_stdio(rpc).await
}