chia-query
Rust crate for querying the Chia blockchain. Maintains a pool of decentralized peer connections as the primary data source and falls back to the coinset.org HTTP API when peers fail.
All hash parameters and return values use 0x-prefixed hex strings. All methods are async and return Result<T, ChiaQueryError>.
Installation
[]
= "0.1"
= { = "1", = ["full"] }
Initialization
use ;
use Duration;
// Default: mainnet, 5 peers, coinset.org fallback enabled
let client = new.await?;
// Custom configuration
let client = new.await?;
TLS certificates auto-generate if the files don't exist. Set TRUSTED_FULLNODE env var to an IP address to prioritize a specific peer. Localhost (127.0.0.1) is always tried before DNS discovery.
Types
Coin
CoinRecord
CoinSpend
SpendBundle
BlockRecord
Other types
FullBlock--serde_json::Value(deeply nested block structure)FeeEstimate-- estimates, target_times, current_fee_rate, mempool_size, etc.NetworkInfo-- network_name, network_prefix, genesis_challengeBlockchainState-- peak, sync state, difficulty, mempool statsTxStatus-- status string, success boolAdditionsAndRemovals-- additions:Vec<CoinRecord>, removals:Vec<CoinRecord>Condition-- opcode:serde_json::Value(hex string), vars:Vec<String>CoinSpendWithConditions-- coin_spend + parsed conditionsMempoolItem--serde_json::Value
API Reference
Coin queries
get_coin_record_by_name
Look up a single coin by its ID.
let record = client.get_coin_record_by_name.await?;
// record.coin.amount, record.spent, record.confirmed_block_index
get_coin_records_by_puzzle_hash
Find all coins matching a puzzle hash.
let records = client.get_coin_records_by_puzzle_hash.await?;
get_coin_records_by_puzzle_hashes
Same as above but batched for multiple puzzle hashes.
let hashes = vec!;
let records = client.get_coin_records_by_puzzle_hashes.await?;
get_coin_records_by_names
Look up multiple coins by their IDs.
let names = vec!;
let records = client.get_coin_records_by_names.await?;
get_coin_records_by_parent_ids
Find child coins of given parent coin IDs.
let parents = vec!;
let records = client.get_coin_records_by_parent_ids.await?;
get_coin_records_by_hint / get_coin_records_by_hints
Find coins by hint (used in CAT and NFT protocols).
let records = client.get_coin_records_by_hint.await?;
get_memos_by_coin_name
Get memos associated with a coin. Returns serde_json::Value.
let memos = client.get_memos_by_coin_name.await?;
Puzzle and solution
get_puzzle_and_solution
Get the puzzle reveal and solution for a spent coin. Height is optional -- if omitted, the spent height is auto-resolved from the peer.
let spend = client.get_puzzle_and_solution.await?;
// spend.puzzle_reveal, spend.solution
get_puzzle_and_solution_with_conditions
Same as above but also runs the puzzle against the solution to extract parsed CLVM conditions.
let result = client.get_puzzle_and_solution_with_conditions.await?;
for cond in &result.conditions
Transaction broadcast
push_tx
Broadcast a spend bundle to the network.
let status = client.push_tx.await?;
// status.status = "SUCCESS" | "PENDING" | "FAILED"
// status.success = true/false
Block queries
get_block / get_block_by_height
Get a full block. Returns serde_json::Value.
let block = client.get_block.await?;
let block = client.get_block_by_height.await?;
// block["reward_chain_block"]["height"].as_u64()
get_block_record / get_block_record_by_height
Get a block record (lighter than full block).
let record = client.get_block_record_by_height.await?;
// record.height, record.weight, record.timestamp, record.farmer_puzzle_hash
get_block_records
Get a range of block records. Range is [start, end).
let records = client.get_block_records.await?;
get_blocks
Get a range of full blocks. Range is [start, end).
let blocks = client.get_blocks.await?;
get_additions_and_removals
Get all coins created (additions) and spent (removals) in a block.
let ar = client.get_additions_and_removals.await?;
// ar.additions: Vec<CoinRecord> -- coins created
// ar.removals: Vec<CoinRecord> -- coins spent
get_block_spends
Get all coin spends in a block with puzzle_reveal and solution.
let spends = client.get_block_spends.await?;
for spend in &spends
get_block_spends_with_conditions
Same as above but also extracts parsed CLVM conditions per spend.
let spends = client.get_block_spends_with_conditions.await?;
for s in &spends
get_block_count_metrics
let metrics = client.get_block_count_metrics.await?;
// metrics.compact_blocks, metrics.uncompact_blocks, metrics.hint_count
get_unfinished_block_headers
let headers = client.get_unfinished_block_headers.await?;
Fee estimation
get_fee_estimate
Estimate fees for target confirmation times (in seconds).
let estimate = client.get_fee_estimate.await?;
// estimate.estimates = [0.0, 0.0, 0.0] -- fee rate per target time
// estimate.current_fee_rate, estimate.mempool_size
Network and state
get_network_info
let info = client.get_network_info.await?;
// info.network_name = "mainnet"
// info.network_prefix = "xch"
// info.genesis_challenge = "0xccd5bb71..."
get_aggsig_additional_data
let data = client.get_aggsig_additional_data.await?;
// "0xccd5bb71..." (same as genesis_challenge on mainnet)
get_blockchain_state
let state = client.get_blockchain_state.await?;
// state.peak.unwrap().height
// state.sync.unwrap().synced
// state.difficulty, state.mempool_size
get_network_space
let space = client.get_network_space.await?;
Mempool
get_all_mempool_items
let items = client.get_all_mempool_items.await?;
// HashMap<String, serde_json::Value> keyed by transaction ID
get_all_mempool_tx_ids
let tx_ids = client.get_all_mempool_tx_ids.await?;
get_mempool_item_by_tx_id
let item = client.get_mempool_item_by_tx_id.await?;
get_mempool_items_by_coin_name
let items = client.get_mempool_items_by_coin_name.await?;
Convenience
wait_for_confirmation
Poll until a coin is confirmed on-chain.
use Duration;
let record = client.wait_for_confirmation.await?;
// Returns CoinRecord once confirmed_block_index > 0
// Errors with timeout if not confirmed within the deadline
Routing behavior
Every request tries decentralized peers first (two attempts on different peers), then falls back to coinset.org. Peer connections are maintained in a pool of up to max_peers (default 5). Failed peers are ejected and replaced in the background.
| Source | Endpoints |
|---|---|
| Peer first, coinset fallback | All coin queries, puzzle_and_solution, fee_estimate, push_tx, block_record_by_height, block_records, additions_and_removals, block_spends, blocks |
| Constants (no network call) | get_network_info, get_aggsig_additional_data |
| Coinset only | get_block_count_metrics, get_block_record (by hash), get_unfinished_block_headers, get_memos_by_coin_name, get_network_space, all mempool endpoints |
Error handling
use ChiaQueryError;
match client.get_coin_record_by_name.await
License
MIT