use serde_json::json;
fn main() -> anyhow::Result<()> {
println!("blvm-node Batch & Script Analysis RPC Examples");
println!("==================================================");
println!();
println!("These methods handle transaction batching, congestion monitoring,");
println!("and script/descriptor/PSBT analysis.");
println!();
let rpc_url = "http://127.0.0.1:18332";
println!("RPC Endpoint: {rpc_url}");
println!();
println!("=== Congestion Monitoring ===");
println!();
println!("1. getcongestion - Get current mempool congestion metrics");
let request = json!({
"jsonrpc": "2.0",
"method": "getcongestion",
"params": [],
"id": 1
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Monitor mempool fee rates and congestion level to decide batch timing");
println!(" Returns: {{mempool_size, avg_fee_rate, median_fee_rate, estimated_blocks}}");
println!();
println!("2. getcongestionmetrics - Alias for getcongestion with identical output");
let request = json!({
"jsonrpc": "2.0",
"method": "getcongestionmetrics",
"params": [],
"id": 2
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Same as getcongestion; provided for API symmetry with batch methods");
println!();
println!("=== Transaction Batching (requires ctv feature) ===");
println!();
println!("3. createbatch - Create a new transaction batch");
let batch_id = "batch_001";
let request = json!({
"jsonrpc": "2.0",
"method": "createbatch",
"params": {
"batch_id": batch_id,
"target_fee_rate": 5 },
"id": 3
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Initialize a batch to collect multiple payment outputs into one transaction");
println!(
" Note: target_fee_rate controls when broadcastbatch fires; omit for default policy"
);
println!();
println!("4. addtobatch - Add a transaction to an existing batch");
let request = json!({
"jsonrpc": "2.0",
"method": "addtobatch",
"params": {
"batch_id": batch_id,
"tx_id": "pay_xyz789",
"outputs": [
{"amount": 100_000, "script_pubkey": "76a914aaa...88ac"},
{"amount": 200_000, "script_pubkey": "76a914bbb...88ac"}
],
"priority": "normal", "deadline": null },
"id": 4
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Accumulate payment outputs; batching reduces per-payment fee overhead");
println!(
" Note: priority affects ordering within the batch; deadline triggers early broadcast"
);
println!();
println!("5. broadcastbatch - Broadcast a batch when congestion conditions are optimal");
let request = json!({
"jsonrpc": "2.0",
"method": "broadcastbatch",
"params": {
"batch_id": batch_id
},
"id": 5
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Trigger batch broadcast; returns covenant_proof for the aggregated tx");
println!(" Note: The congestion manager may delay broadcast until fee rate meets target");
println!();
println!("=== Script Analysis (requires miniscript feature) ===");
println!();
println!("6. getdescriptorinfo - Analyze an output descriptor");
let descriptor = "wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)";
let request = json!({
"jsonrpc": "2.0",
"method": "getdescriptorinfo",
"params": [descriptor],
"id": 6
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Parse and validate a descriptor; get checksum, script type, and is_range");
println!(" Note: Descriptors encode spending conditions (wpkh, wsh, tr, multi, etc.)");
println!();
println!("7. analyzepsbt - Analyze a Partially Signed Bitcoin Transaction");
let psbt_base64 = "cHNidP8BAHUCAAAAASaBcTce3/KF6Tet7qSze3gADAVmy7OtZGQXE8pn/...";
let request = json!({
"jsonrpc": "2.0",
"method": "analyzepsbt",
"params": [psbt_base64],
"id": 7
});
println!(" Request: {}", serde_json::to_string_pretty(&request)?);
println!(" Use: Inspect PSBT roles, missing signatures, and estimated fee before finalizing");
println!(" Note: Returns per-input analysis showing which signers have signed");
println!();
println!("Method Summary:");
println!(" getcongestion - Mempool fee rate and congestion metrics");
println!(" getcongestionmetrics - Alias for getcongestion");
println!(" createbatch - Initialize a transaction batch (ctv feature)");
println!(" addtobatch - Add outputs to a batch (ctv feature)");
println!(" broadcastbatch - Broadcast batch when conditions are met (ctv feature)");
println!(
" getdescriptorinfo - Parse and validate an output descriptor (miniscript feature)"
);
println!(" analyzepsbt - Inspect PSBT signing status and fee (miniscript feature)");
println!();
println!("Typical batching workflow:");
println!(" 1. getcongestion → check current fee environment");
println!(" 2. createbatch → open a new batch");
println!(" 3. addtobatch (N times) → accumulate payment outputs");
println!(" 4. broadcastbatch → submit when fee rate is acceptable");
println!();
println!("To test with a running node:");
println!(" 1. Start node: blvm-node --network testnet");
println!(" 2. Send requests with curl or any HTTP client");
Ok(())
}