Skip to content

Commit

Permalink
chore(iota-sdk): harmonize client name in examples (#2074)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Aug 28, 2024
1 parent e14a5f4 commit 2693e23
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 74 deletions.
20 changes: 11 additions & 9 deletions crates/iota-sdk/examples/coin_read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use utils::setup_for_read;

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

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

Expand All @@ -28,7 +28,7 @@ async fn main() -> Result<(), anyhow::Error> {
// use `None` for the default `Coin<IOTA>` which is represented as
// "0x2::iota::IOTA"
let coin_type = Some("0x2::iota::IOTA".to_string());
let coins = iota
let coins = client
.coin_read_api()
.get_coins(active_address, coin_type.clone(), None, Some(5)) // get the first five coins
.await?;
Expand All @@ -40,7 +40,7 @@ async fn main() -> Result<(), anyhow::Error> {
// This function works very similar to the get_coins function, except it does
// not take a coin_type filter argument and it returns all coin types
// associated with this address
let all_coins = iota
let all_coins = client
.coin_read_api()
.get_all_coins(active_address, None, Some(5)) // get the first five coins
.await?;
Expand All @@ -50,7 +50,9 @@ async fn main() -> Result<(), anyhow::Error> {

// Get coins as a stream
// Similar to the previous functions, except it returns the coins as a stream.
let coins_stream = iota.coin_read_api().get_coins_stream(active_address, None);
let coins_stream = client
.coin_read_api()
.get_coins_stream(active_address, None);

println!(" *** Coins Stream ***");
coins_stream
Expand All @@ -63,7 +65,7 @@ async fn main() -> Result<(), anyhow::Error> {

// Select coins based on the provided coin type (IOTA in this example). Use
// `None` for the default Iota coin
let select_coins = iota
let select_coins = client
.coin_read_api()
.select_coins(active_address, coin_type, 1, vec![])
.await?;
Expand All @@ -75,14 +77,14 @@ async fn main() -> Result<(), anyhow::Error> {
// Balance
// Returns the balance for the specified coin type for this address,
// or if None is passed, it will use Coin<IOTA> as the coin type
let balance = iota
let balance = client
.coin_read_api()
.get_balance(active_address, None)
.await?;

// Total balance
// Returns the balance for each coin owned by this address
let total_balance = iota
let total_balance = client
.coin_read_api()
.get_all_balances(active_address)
.await?;
Expand All @@ -92,7 +94,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!(" *** Balance + Total Balance ***\n ");

// Return the coin metadata for the Coin<IOTA>
let coin_metadata = iota
let coin_metadata = client
.coin_read_api()
.get_coin_metadata("0x2::iota::IOTA".to_string())
.await?;
Expand All @@ -102,7 +104,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!(" *** Coin Metadata ***\n ");

// Total Supply
let total_supply = iota
let total_supply = client
.coin_read_api()
.get_total_supply("0x2::iota::IOTA".to_string())
.await?;
Expand Down
8 changes: 4 additions & 4 deletions crates/iota-sdk/examples/event_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ use utils::{setup_for_write, split_coin_digest};

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

println!(" *** Get events *** ");
// for demonstration purposes, we set to make a transaction
let digest = split_coin_digest(&iota, &active_address).await?;
let events = iota.event_api().get_events(digest).await?;
let digest = split_coin_digest(&client, &active_address).await?;
let events = client.event_api().get_events(digest).await?;
println!("{:?}", events);
println!(" *** Get events ***\n ");

let descending = true;
let query_events = iota
let query_events = client
.event_api()
.query_events(EventFilter::All(vec![]), None, Some(5), descending) // query first 5 events in descending order
.await?;
Expand Down
13 changes: 8 additions & 5 deletions crates/iota-sdk/examples/governance_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,29 @@ use utils::setup_for_read;

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

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

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

println!(" *** Stakes ***");
println!("{:?}", stakes);
println!(" *** Stakes ***\n");

// Committee Info
let committee = iota.governance_api().get_committee_info(None).await?; // None defaults to the latest epoch
let committee = client.governance_api().get_committee_info(None).await?; // None defaults to the latest epoch

println!(" *** Committee Info ***");
println!("{:?}", committee);
println!(" *** Committee Info ***\n");

// Latest Iota System State
let iota_system_state = iota.governance_api().get_latest_iota_system_state().await?;
let iota_system_state = client
.governance_api()
.get_latest_iota_system_state()
.await?;

println!(" *** Iota System State ***");
println!("{:?}", iota_system_state);
Expand All @@ -55,7 +58,7 @@ async fn main() -> Result<(), anyhow::Error> {

println!(" *** List active validators ***\n");
// Reference Gas Price
let reference_gas_price = iota.governance_api().get_reference_gas_price().await?;
let reference_gas_price = client.governance_api().get_reference_gas_price().await?;

println!(" *** Reference Gas Price ***");
println!("{:?}", reference_gas_price);
Expand Down
20 changes: 10 additions & 10 deletions crates/iota-sdk/examples/iota_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ use iota_sdk::IotaClientBuilder;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let iota = IotaClientBuilder::default()
let client = IotaClientBuilder::default()
.build("http://127.0.0.1:9000") // local network address
.await?;
println!("Iota local network version: {}", iota.api_version());
println!("Iota local network version: {}", client.api_version());

// local Iota network, like the above one but using the dedicated function
let iota_local = IotaClientBuilder::default().build_localnet().await?;
println!("Iota local network version: {}", iota_local.api_version());
let local_client = IotaClientBuilder::default().build_localnet().await?;
println!("Iota local network version: {}", local_client.api_version());

// Iota devnet -- https://fullnode.devnet.iota.io:443
let iota_devnet = IotaClientBuilder::default().build_devnet().await?;
println!("Iota devnet version: {}", iota_devnet.api_version());
let devnet_client = IotaClientBuilder::default().build_devnet().await?;
println!("Iota devnet version: {}", devnet_client.api_version());

// Iota testnet -- https://fullnode.testnet.iota.io:443
let iota_testnet = IotaClientBuilder::default().build_testnet().await?;
println!("Iota testnet version: {}", iota_testnet.api_version());
let testnet_client = IotaClientBuilder::default().build_testnet().await?;
println!("Iota testnet version: {}", testnet_client.api_version());

println!("{:?}", iota_local.available_rpc_methods());
println!("{:?}", iota_local.available_subscriptions());
println!("{:?}", local_client.available_rpc_methods());
println!("{:?}", local_client.available_subscriptions());

Ok(())
}
4 changes: 2 additions & 2 deletions crates/iota-sdk/examples/json_rpc_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use utils::setup_for_read;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let (iota, active_address) = setup_for_read().await?;
let (client, active_address) = setup_for_read().await?;
let coin_type = Some("0x42".to_string());
let coins = iota
let coins = client
.coin_read_api()
.get_coins(active_address, coin_type.clone(), None, Some(5))
.await;
Expand Down
8 changes: 4 additions & 4 deletions crates/iota-sdk/examples/pay_iota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use utils::setup_for_write;
async fn main() -> Result<(), anyhow::Error> {
// 1) Get the Iota client, the sender and recipient that we will use
// for the transaction
let (iota, sender, recipient) = setup_for_write().await?;
let (client, sender, recipient) = setup_for_write().await?;

// 2) Get the coin we will use as gas and for the payment
let coins = iota
let coins = client
.coin_read_api()
.get_coins(sender, None, None, None)
.await?;
Expand All @@ -33,7 +33,7 @@ async fn main() -> Result<(), anyhow::Error> {

// 3) Build the transaction data, to transfer 1_000 NANOS from the gas coin to
// the recipient address
let tx_data = iota
let tx_data = client
.transaction_builder()
.pay_iota(
sender,
Expand All @@ -50,7 +50,7 @@ async fn main() -> Result<(), anyhow::Error> {

// 5) Execute the transaction
println!("Executing the transaction...");
let transaction_response = iota
let transaction_response = client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
Expand Down
10 changes: 5 additions & 5 deletions crates/iota-sdk/examples/programmable_transactions_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ use utils::setup_for_write;
async fn main() -> Result<(), anyhow::Error> {
// 1) Get the Iota client, the sender and recipient that we will use
// for the transaction
let (iota, sender, recipient) = setup_for_write().await?;
let (client, sender, recipient) = setup_for_write().await?;

// We need to find the coin we will use as gas
let coins = iota
let coins = client
.coin_read_api()
.get_coins(sender, None, None, None)
.await?;
Expand Down Expand Up @@ -68,7 +68,7 @@ async fn main() -> Result<(), anyhow::Error> {
let transaction = ptb.finish();

let gas_budget = 5_000_000;
let gas_price = iota.read_api().get_reference_gas_price().await?;
let gas_price = client.read_api().get_reference_gas_price().await?;
// Create the transaction data that will be sent to the network
let tx_data = TransactionData::new_programmable(
sender,
Expand All @@ -84,7 +84,7 @@ async fn main() -> Result<(), anyhow::Error> {

// 5) Execute the transaction
print!("Executing the transaction...");
let transaction_response = iota
let transaction_response = client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
Expand All @@ -95,7 +95,7 @@ async fn main() -> Result<(), anyhow::Error> {
print!("done\n Transaction information: ");
println!("{:?}", transaction_response);

let coins = iota
let coins = client
.coin_read_api()
.get_coins(recipient, None, None, None)
.await?;
Expand Down
24 changes: 12 additions & 12 deletions crates/iota-sdk/examples/read_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ use utils::{setup_for_write, split_coin_digest};

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

// ************ READ API ************ //
println!("// ************ READ API ************ //\n");
// Owned Objects
let owned_objects = iota
let owned_objects = client
.read_api()
.get_owned_objects(active_address, None, None, Some(5))
.await?;
Expand All @@ -37,7 +37,7 @@ async fn main() -> Result<(), anyhow::Error> {

// Dynamic Fields
let parent_object_id = ObjectID::from_address(active_address.into());
let dynamic_fields = iota
let dynamic_fields = client
.read_api()
.get_dynamic_fields(parent_object_id, None, None)
.await?;
Expand All @@ -46,7 +46,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!(" *** Dynamic Fields ***\n");
if let Some(dynamic_field_info) = dynamic_fields.data.into_iter().next() {
println!(" *** First Dynamic Field ***");
let dynamic_field = iota
let dynamic_field = client
.read_api()
.get_dynamic_field_object(parent_object_id, dynamic_field_info.name)
.await?;
Expand Down Expand Up @@ -75,7 +75,7 @@ async fn main() -> Result<(), anyhow::Error> {
show_storage_rebate: true,
};

let past_object = iota
let past_object = client
.read_api()
.try_get_parsed_past_object(object_id, version, iota_data_options.clone())
.await?;
Expand All @@ -84,7 +84,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!(" *** Past Object ***\n");

let iota_get_past_object_request = past_object.clone().into_object()?;
let multi_past_object = iota
let multi_past_object = client
.read_api()
.try_multi_get_parsed_past_object(
vec![IotaGetPastObjectRequest {
Expand All @@ -99,7 +99,7 @@ async fn main() -> Result<(), anyhow::Error> {
println!(" *** Multi Past Object ***\n");

// Object with options
let object_with_options = iota
let object_with_options = client
.read_api()
.get_object_with_options(iota_get_past_object_request.object_id, iota_data_options)
.await?;
Expand All @@ -109,17 +109,17 @@ async fn main() -> Result<(), anyhow::Error> {
println!(" *** Object with Options ***\n");

println!(" *** Chain identifier *** ");
println!("{:?}", iota.read_api().get_chain_identifier().await?);
println!("{:?}", client.read_api().get_chain_identifier().await?);
println!(" *** Chain identifier ***\n ");

println!(" *** Protocol Config *** ");
println!("{:?}", iota.read_api().get_protocol_config(None).await?);
println!("{:?}", client.read_api().get_protocol_config(None).await?);
println!(" *** Protocol Config ***\n ");

// we make a dummy transaction which returns a transaction digest
let tx_digest = split_coin_digest(&iota, &active_address).await?;
let tx_digest = split_coin_digest(&client, &active_address).await?;
println!(" *** Transaction data *** ");
let tx_response = iota
let tx_response = client
.read_api()
.get_transaction_with_options(
tx_digest,
Expand All @@ -138,7 +138,7 @@ async fn main() -> Result<(), anyhow::Error> {

println!("Transaction data: {tx_response:?}");

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

Expand Down
10 changes: 5 additions & 5 deletions crates/iota-sdk/examples/sign_tx_guide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use utils::request_tokens_from_faucet;
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// set up iota client for the desired network.
let iota_client = IotaClientBuilder::default().build_testnet().await?;
let client = IotaClientBuilder::default().build_testnet().await?;

// deterministically generate a keypair, testing only, do not use for mainnet,
// use the next section to randomly generate a keypair instead.
Expand Down Expand Up @@ -102,8 +102,8 @@ async fn main() -> Result<(), anyhow::Error> {
println!("Sender: {sender:?}");

// make sure the sender has a gas coin as an example.
request_tokens_from_faucet(sender, &iota_client).await?;
let gas_coin = iota_client
request_tokens_from_faucet(sender, &client).await?;
let gas_coin = client
.coin_read_api()
.get_coins(sender, None, None, None)
.await?
Expand All @@ -120,7 +120,7 @@ async fn main() -> Result<(), anyhow::Error> {
};

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

// create the transaction data that will be sent to the network.
let tx_data = TransactionData::new_programmable(
Expand Down Expand Up @@ -153,7 +153,7 @@ async fn main() -> Result<(), anyhow::Error> {
assert!(res.is_ok());

// execute the transaction.
let transaction_response = iota_client
let transaction_response = client
.quorum_driver_api()
.execute_transaction_block(
iota_types::transaction::Transaction::from_generic_sig_data(
Expand Down
Loading

0 comments on commit 2693e23

Please sign in to comment.