Skip to content

Commit

Permalink
feat(iota-sdk): add more read api examples (#2195)
Browse files Browse the repository at this point in the history
* feat(iota-sdk): add more read api examples

* Split read_api2

* Address review comments

* Rename files

* Rename more

* Fix command

* Fix print

* Remove useless sections
  • Loading branch information
Thoralf-M authored Sep 4, 2024
1 parent 0c3d2ea commit f292cbc
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 10 deletions.
3 changes: 0 additions & 3 deletions crates/iota-sdk/examples/coin_read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use utils::setup_for_read;
async fn main() -> Result<(), anyhow::Error> {
let (client, active_address) = setup_for_read().await?;

// ************ COIN READ API ************ //

// Get coins for this address. Coins can be filtered by `coin_type`
// (e.g., 0x168da5bf1f48dafc111b0a488fa454aca95e0b5e::usdc::USDC) or
// use `None` for the default `Coin<IOTA>` which is represented as
Expand Down Expand Up @@ -112,6 +110,5 @@ async fn main() -> Result<(), anyhow::Error> {
println!("{:?}", total_supply);
println!(" *** Total Supply ***\n ");

// ************ END OF COIN READ API ************ //
Ok(())
}
3 changes: 0 additions & 3 deletions crates/iota-sdk/examples/governance_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use utils::setup_for_read;
async fn main() -> Result<(), anyhow::Error> {
let (client, active_address) = setup_for_read().await?;

// ************ GOVERNANCE API ************ //

// Stakes
let stakes = client.governance_api().get_stakes(active_address).await?;

Expand Down Expand Up @@ -64,6 +62,5 @@ async fn main() -> Result<(), anyhow::Error> {
println!("{:?}", reference_gas_price);
println!(" *** Reference Gas Price ***\n");

// ************ END OF GOVERNANCE API ************ //
Ok(())
}
3 changes: 0 additions & 3 deletions crates/iota-sdk/examples/read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use utils::setup_for_write;
async fn main() -> Result<(), anyhow::Error> {
let (client, active_address, _) = setup_for_write().await?;

// ************ READ API ************ //
println!("// ************ READ API ************ //\n");
// Owned Objects
let owned_objects = client
.read_api()
Expand Down Expand Up @@ -117,7 +115,6 @@ async fn main() -> Result<(), anyhow::Error> {

let tx_blocks = client.read_api().get_total_transaction_blocks().await?;
println!("Total transaction blocks {tx_blocks}");
// ************ END OF READ API ************ //

Ok(())
}
41 changes: 41 additions & 0 deletions crates/iota-sdk/examples/read_api_objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! This example gets objects in various ways.
//!
//! cargo run --example read_api_objects

use iota_json_rpc_types::IotaObjectDataOptions;
use iota_sdk::IotaClientBuilder;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let client = IotaClientBuilder::default().build_testnet().await?;

let object = client
.read_api()
.get_object_with_options(
"0x5".parse()?,
IotaObjectDataOptions::new().with_previous_transaction(),
)
.await?;
println!("Object with previous transaction: {object:?}");
let object_id = object.data.as_ref().unwrap().object_id;

let object_bcs = client.read_api().get_move_object_bcs(object_id).await?;
println!("Object bcs: {object_bcs:?}");

let loaded_child_objects = client
.read_api()
.get_loaded_child_objects(object.data.unwrap().previous_transaction.unwrap())
.await?;
println!("Loaded child objects: {loaded_child_objects:?}");

let objects = client
.read_api()
.multi_get_object_with_options(vec![object_id], IotaObjectDataOptions::default().with_bcs())
.await?;
println!("Objects: {objects:?}");

Ok(())
}
21 changes: 21 additions & 0 deletions crates/iota-sdk/examples/read_api_package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! This example gets a move package by its object id.
//!
//! cargo run --example read_api_package

use iota_sdk::IotaClientBuilder;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let client = IotaClientBuilder::default().build_testnet().await?;

let move_package = client
.read_api()
.get_normalized_move_modules_by_package("0x1".parse()?)
.await?;
println!("Move package: {move_package:?}");

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! This example uses the ReadApi to get transaction blocks as paginated
//! response and stream.
//!
//! cargo run --example read_transactions
//! cargo run --example read_api_transactions

use futures::StreamExt;
use iota_json_rpc_types::{IotaTransactionBlockResponseQuery, TransactionFilter};
Expand Down
32 changes: 32 additions & 0 deletions crates/iota-sdk/examples/read_api_tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! This example gets a transaction with options.
//!
//! cargo run --example read_api_tx

use iota_json_rpc_types::{IotaObjectDataOptions, IotaTransactionBlockResponseOptions};
use iota_sdk::IotaClientBuilder;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let client = IotaClientBuilder::default().build_testnet().await?;

// Get a random tx that loaded child objects
let object = client
.read_api()
.get_object_with_options(
"0x5".parse()?,
IotaObjectDataOptions::new().with_previous_transaction(),
)
.await?;
let tx_id = object.data.unwrap().previous_transaction.unwrap();

let tx_response = client
.read_api()
.get_transaction_with_options(tx_id, IotaTransactionBlockResponseOptions::new())
.await?;
println!("Tx: {tx_response:?}");

Ok(())
}
62 changes: 62 additions & 0 deletions crates/iota-sdk/examples/simulate_transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! This example shows how to simulate a transaction, without actually executing
//! it.
//!
//! cargo run --example simulate_transaction

mod utils;

use iota_types::{
programmable_transaction_builder::ProgrammableTransactionBuilder,
transaction::{TransactionData, TransactionKind},
};
use utils::setup_for_write;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let (client, sender, recipient) = setup_for_write().await?;

let coins = client
.coin_read_api()
.get_coins(sender, None, None, None)
.await?;
let gas_coin = coins.data.into_iter().next().unwrap();

let programmable_transaction = {
let mut builder = ProgrammableTransactionBuilder::new();
builder
.pay_iota(vec![recipient], vec![1_000_000_000])
.unwrap();
builder.finish()
};

let gas_budget = 5_000_000;
let gas_price = client.read_api().get_reference_gas_price().await?;

let tx_data = TransactionData::new_programmable(
sender,
vec![gas_coin.object_ref()],
programmable_transaction.clone(),
gas_budget,
gas_price,
);

let dry_run_tx_resp = client.read_api().dry_run_transaction_block(tx_data).await?;
println!("{dry_run_tx_resp:?}");

let dev_inspect_result = client
.read_api()
.dev_inspect_transaction_block(
sender,
TransactionKind::programmable(programmable_transaction),
None,
None,
None,
)
.await?;
println!("{dev_inspect_result:?}");

Ok(())
}

0 comments on commit f292cbc

Please sign in to comment.