RPC 클라이언트
RpcClient는 JSON-RPC 2.0을 통해 Savitri 노드와 통신하는 주요 전송 계층입니다.
클라이언트 생성
use savitri_sdk::RpcClient;
// From URL (localhost HTTP allowed)
let client = RpcClient::from_url("http://localhost:8545")?;
// From URL (HTTPS required for remote)
let client = RpcClient::from_url("https://rpc.savitrinetwork.com")?;
// With custom configuration
use savitri_sdk::client::rpc_client::RpcClientConfig;
let client = RpcClient::new(RpcClientConfig {
url: "http://localhost:8545".to_string(),
timeout: Some(60), // 60 second timeout
allow_insecure: false, // enforce HTTPS for remote
})?;
체인 조회
// Block height
let height: u64 = client.get_block_number().await?;
// Block by height
let block = client.get_block_by_height(42).await?;
println!("Block {} hash: {}", block.height, block.hash);
println!("Proposer: {}", block.proposer);
println!("TX count: {}", block.transaction_count);
// Block hash
let hash: String = client.get_block_hash(42).await?;
계정 조회
// Full account info
let account = client.get_account("aabbcc...64chars").await?;
println!("Balance: {}", account.balance); // u128 as decimal string
println!("Nonce: {}", account.nonce);
// Balance only
let balance: String = client.get_balance("aabbcc...").await?;
// Nonce only
let nonce: u64 = client.get_nonce("aabbcc...").await?;
트랜잭션 작업
// Get transaction by hash
let tx = client.get_transaction("tx_hash_hex").await?;
println!("From: {} To: {} Amount: {}", tx.from, tx.to, tx.amount);
// Submit signed transaction
let result = client.send_raw_transaction("signed_tx_hex").await?;
println!("TX hash: {}", result.tx_hash);
// Claim testnet faucet
let claim = client.faucet_claim("your_address_64hex").await?;
println!("Received: {} (tx: {})", claim.amount, claim.tx_hash);
합의 조회
// Local PoU state
let pou = client.pou_local().await?;
println!("My score: {:?}", pou.local_score);
println!("Leader: {:?} (score: {:?})", pou.leader, pou.leader_score);
println!("Am I leader? {}", pou.local_is_leader);
// All peer scores
let peers = client.pou_peers().await?;
for (peer_id, score) in &peers.peers {
println!("{}: {}", peer_id, score);
}
// Groups (masternode only)
let groups = client.pou_groups().await?;
for group in &groups.groups {
println!("Group {}: {} members", group.group_id, group.members.len());
}
// Masternodes (masternode only)
let mns = client.pou_masternodes().await?;
for mn in &mns.masternodes {
println!("{}: PoU={} Health={}", mn.node_id, mn.pou_score, mn.health_score);
}
배치 요청
단일 HTTP 요청에서 여러 RPC 호출을 전송합니다 (최대 100개):
let responses = client.batch(vec![
("savitri_blockNumber", serde_json::json!([])),
("savitri_health", serde_json::json!([])),
("savitri_getAccount", serde_json::json!(["aabbcc..."])),
]).await?;
for resp in &responses {
println!("{}", resp);
}
직접 호출
타입이 지정된 헬퍼가 없는 메서드를 위해:
let result: serde_json::Value = client
.call_raw("custom_method", serde_json::json!(["param1", 42]))
.await?;
상태 확인
// Typed health check
let health = client.health().await?;
assert_eq!(health.status, "ok");
// Simple ping (returns bool)
let is_alive = client.ping().await?;
오류 처리
use savitri_sdk::SdkError;
match client.get_account("invalid").await {
Ok(account) => println!("Balance: {}", account.balance),
Err(SdkError::RpcError { code, message, .. }) => {
println!("RPC error {}: {}", code, message);
}
Err(SdkError::HttpError(e)) => {
println!("Network error: {}", e);
}
Err(e) => println!("Other error: {}", e),
}
오류 유형:
| 오류 | 설명 |
|---|---|
SdkError::RpcError | 서버의 JSON-RPC 오류 (코드, 메시지, 데이터) |
SdkError::HttpError | 네트워크/전송 오류 |
SdkError::JsonError | JSON 직렬화 오류 |
SdkError::InvalidResponse | 예상치 못한 응답 형식 |
SdkError::NoRpcClient | RPC 연결 없이 wallet 메서드 호출 |