-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathview_function.rs
More file actions
69 lines (61 loc) · 2.02 KB
/
view_function.rs
File metadata and controls
69 lines (61 loc) · 2.02 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
//! Example: Calling view functions
//!
//! This example demonstrates how to call view functions (read-only)
//! on the Aptos blockchain.
//!
//! Run with: `cargo run --example view_function --features ed25519`
use aptos_sdk::{Aptos, AptosConfig};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create client for testnet
let aptos = Aptos::new(AptosConfig::testnet())?;
println!("Connected to testnet");
// Get ledger info
let ledger_info = aptos.ledger_info().await?;
println!("Ledger version: {}", ledger_info.version()?);
println!("Block height: {}", ledger_info.height()?);
println!("Epoch: {}", ledger_info.epoch_num()?);
println!();
// Call timestamp view function
println!("Calling 0x1::timestamp::now_seconds...");
let result = aptos
.view("0x1::timestamp::now_seconds", vec![], vec![])
.await?;
println!("Current timestamp: {:?}", result);
println!();
// Check if an account exists
let framework_address = "0x1";
println!("Checking if account 0x1 exists...");
let result = aptos
.view(
"0x1::account::exists_at",
vec![],
vec![serde_json::json!(framework_address)],
)
.await?;
println!("Account exists: {:?}", result);
println!();
// Get account sequence number
println!("Getting sequence number for 0x1...");
let result = aptos
.view(
"0x1::account::get_sequence_number",
vec![],
vec![serde_json::json!(framework_address)],
)
.await?;
println!("Sequence number: {:?}", result);
println!();
// Get coin balance for an address
let test_address = "0x1"; // Framework address
println!("Getting APT balance for {}...", test_address);
let result = aptos
.view(
"0x1::coin::balance",
vec!["0x1::aptos_coin::AptosCoin".to_string()],
vec![serde_json::json!(test_address)],
)
.await?;
println!("Balance: {:?} octas", result);
Ok(())
}