合约客户端
ContractClient 提供与 Savitri Network 上的智能合约、预言机和治理系统交互的高级辅助工具。
初始化
use savitri_sdk::{ContractClient, Wallet, RpcClient};
let wallet = Wallet::from_private_key_hex("your_private_key")?;
let rpc = RpcClient::from_url("http://localhost:8545")?;
let contract = ContractClient::new(rpc, wallet);
// 或直接从 URL 创建
let contract = ContractClient::from_url_and_wallet(
"http://localhost:8545",
wallet,
)?;
合约调用
通用合约调用
let tx_hash = contract.call_contract(
&"contract_address_64hex".to_string(), // contract address
b"function_name", // function selector
b"encoded_arguments", // ABI-encoded args
Some(0), // value to send (0 for read-like)
).await?;
println!("TX hash: {}", tx_hash);
call_contract 方法执行以下步骤:
- 构建调用数据(
function_selector || args) - 通过
TransactionBuilder创建交易 - 使用钱包签名
- 通过
savitri_sendRawTransaction提交 - 返回交易哈希
预言机客户端
访问预言机系统以获取外部数据源:
let oracle = contract.oracle();
// 向预言机请求数据
let tx_hash = oracle.request_data(
&"oracle_address".to_string(),
"temperature", // data type
b"sensor_id_001", // parameters
).await?;
// 提交响应(作为预言机提供者)
let tx_hash = oracle.submit_response(
&"oracle_address".to_string(),
request_id, // u64
b"25.5", // response data
).await?;
// 验证数据
let is_valid = oracle.verify_data(
&"oracle_address".to_string(),
b"data_to_verify",
).await?;
治理客户端
与链上治理系统交互:
let gov = contract.governance();
// 创建提案
let tx_hash = gov.create_proposal(
&"governance_address".to_string(),
"Upgrade consensus parameters", // title
"Increase block size to 2MB", // description
604800, // voting period (seconds)
).await?;
// 对提案投票
let tx_hash = gov.vote(
&"governance_address".to_string(),
proposal_id, // u64
true, // true = support, false = oppose
).await?;
// 执行已批准的提案
let tx_hash = gov.execute(
&"governance_address".to_string(),
proposal_id,
).await?;
// 查询提案状态
let status = gov.get_proposal_status(
&"governance_address".to_string(),
proposal_id,
).await?;
println!("Proposal #{}: {}", status.id, status.status);
println!("Votes: {} for, {} against", status.votes_for, status.votes_against);
println!("Executed: {}", status.executed);
ProposalStatus 字段
| 字段 | 类型 | 描述 |
|---|---|---|
id | u64 | 提案标识符 |
title | String | 提案标题 |
votes_for | u64 | 赞成票数 |
votes_against | u64 | 反对票数 |
status | String | "active"(活跃)、"passed"(通过)、"rejected"(拒绝)、"executed"(已执行) |
executed | bool | 提案是否已执行 |