-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(iota-sdk): add more read api examples (#2195)
* 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
Showing
8 changed files
with
157 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |