Skip to content

Commit

Permalink
refactor(docs/examples): update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miker83z committed Aug 14, 2024
1 parent a0d71ec commit dffd179
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 470 deletions.
92 changes: 5 additions & 87 deletions docs/examples/rust/stardust/address_unlock_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
//! In order to work, it requires a network with test objects
//! generated from iota-genesis-builder/src/stardust/test_outputs.
use std::{fs, path::PathBuf, str::FromStr};
use std::str::FromStr;

use anyhow::anyhow;
use bip32::DerivationPath;
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore};
use docs_examples::{clean_keystore, fund_address, setup_keystore};
use iota_keys::keystore::AccountKeystore;
use iota_sdk::{
rpc_types::{
IotaObjectDataFilter, IotaObjectDataOptions, IotaObjectResponseQuery,
IotaTransactionBlockResponseOptions,
},
types::{
base_types::{IotaAddress, ObjectID},
base_types::ObjectID,
crypto::SignatureScheme::ED25519,
dynamic_field::DynamicFieldName,
gas_coin::GAS,
Expand All @@ -26,14 +27,11 @@ use iota_sdk::{
transaction::{Argument, ObjectArg, Transaction, TransactionData},
TypeTag, IOTA_FRAMEWORK_ADDRESS, STARDUST_ADDRESS,
},
IotaClient, IotaClientBuilder,
IotaClientBuilder,
};
use move_core_types::ident_str;
use shared_crypto::intent::Intent;

/// Got from iota-genesis-builder/src/stardust/test_outputs/stardust_mix.rs
const SPONSOR_ADDRESS_MNEMONIC: &str = "okay pottery arch air egg very cave cash poem gown sorry mind poem crack dawn wet car pink extra crane hen bar boring salt";

/// Got from iota-genesis-builder/src/stardust/test_outputs/alias_ownership.rs
const MAIN_ADDRESS_MNEMONIC: &str = "few hood high omit camp keep burger give happy iron evolve draft few dawn pulp jazz box dash load snake gown bag draft car";

Expand Down Expand Up @@ -271,83 +269,3 @@ async fn main() -> Result<(), anyhow::Error> {
// Finish and clean the temporary keystore file
clean_keystore()
}

fn setup_keystore() -> Result<FileBasedKeystore, anyhow::Error> {
// Create a temporary keystore
let keystore_path = PathBuf::from("iotatempdb");
if !keystore_path.exists() {
let keystore = FileBasedKeystore::new(&keystore_path)?;
keystore.save()?;
}
// Read iota keystore
FileBasedKeystore::new(&keystore_path)
}

fn clean_keystore() -> Result<(), anyhow::Error> {
// Remove files
fs::remove_file("iotatempdb")?;
fs::remove_file("iotatempdb.aliases")?;
Ok(())
}

async fn fund_address(
iota_client: &IotaClient,
keystore: &mut FileBasedKeystore,
recipient: IotaAddress,
) -> Result<(), anyhow::Error> {
// Derive the address of the sponsor.
let sponsor = keystore.import_from_mnemonic(SPONSOR_ADDRESS_MNEMONIC, ED25519, None)?;

println!("Sponsor address: {sponsor:?}");

// Get a gas coin.
let gas_coin = iota_client
.coin_read_api()
.get_coins(sponsor, None, None, None)
.await?
.data
.into_iter()
.next()
.ok_or(anyhow!("No coins found for sponsor"))?;

let pt = {
// Init a programmable transaction builder.
let mut builder = ProgrammableTransactionBuilder::new();
// Pay all iotas from the gas object
builder.pay_all_iota(recipient);
builder.finish()
};

// Setup a gas budget and a gas price.
let gas_budget = 10_000_000;
let gas_price = iota_client.read_api().get_reference_gas_price().await?;

// Create a transaction data that will be sent to the network.
let tx_data = TransactionData::new_programmable(
sponsor,
vec![gas_coin.object_ref()],
pt,
gas_budget,
gas_price,
);

// Sign the transaction.
let signature = keystore.sign_secure(&sponsor, &tx_data, Intent::iota_transaction())?;

// Execute the transaction.
let transaction_response = iota_client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
IotaTransactionBlockResponseOptions::full_content(),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?;

println!(
"Funding transaction digest: {}",
transaction_response.digest
);

Ok(())
}
112 changes: 6 additions & 106 deletions docs/examples/rust/stardust/alias-migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
//! with test objects generated from
//! iota-genesis-builder/src/stardust/test_outputs.
use std::{fs, path::PathBuf, str::FromStr};
use std::str::FromStr;

use anyhow::{anyhow, Result};
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore};
use iota_move_build::BuildConfig;
use docs_examples::{clean_keystore, publish_custom_nft_package, setup_keystore};
use iota_keys::keystore::AccountKeystore;
use iota_sdk::{
rpc_types::{
IotaData, IotaObjectDataOptions, IotaTransactionBlockEffectsAPI,
IotaTransactionBlockResponseOptions,
},
rpc_types::{IotaData, IotaObjectDataOptions, IotaTransactionBlockResponseOptions},
types::{
base_types::{IotaAddress, ObjectID},
base_types::ObjectID,
crypto::SignatureScheme::ED25519,
gas_coin::GAS,
programmable_transaction_builder::ProgrammableTransactionBuilder,
Expand All @@ -26,7 +23,7 @@ use iota_sdk::{
transaction::{Argument, CallArg, ObjectArg, Transaction, TransactionData},
TypeTag, IOTA_FRAMEWORK_PACKAGE_ID, STARDUST_PACKAGE_ID,
},
IotaClient, IotaClientBuilder,
IotaClientBuilder,
};
use move_core_types::ident_str;
use shared_crypto::intent::Intent;
Expand Down Expand Up @@ -277,100 +274,3 @@ async fn main() -> Result<(), anyhow::Error> {
// Finish and clean the temporary keystore file
clean_keystore()
}

fn setup_keystore() -> Result<FileBasedKeystore, anyhow::Error> {
// Create a temporary keystore
let keystore_path = PathBuf::from("iotatempdb");
if !keystore_path.exists() {
let keystore = FileBasedKeystore::new(&keystore_path)?;
keystore.save()?;
}
// Read iota keystore
FileBasedKeystore::new(&keystore_path)
}

fn clean_keystore() -> Result<(), anyhow::Error> {
// Remove files
fs::remove_file("iotatempdb")?;
fs::remove_file("iotatempdb.aliases")?;
Ok(())
}

async fn publish_custom_nft_package(
sender: IotaAddress,
keystore: &mut FileBasedKeystore,
iota_client: &IotaClient,
package_path: &str,
) -> Result<ObjectID> {
// Get a gas coin
let gas_coin = iota_client
.coin_read_api()
.get_coins(sender, None, None, None)
.await?
.data
.into_iter()
.next()
.ok_or(anyhow!("No coins found"))?;

// Build custom nft package
let compiled_package = BuildConfig::default().build(package_path.into())?;
let modules = compiled_package
.get_modules()
.map(|module| {
let mut buf = Vec::new();
module.serialize(&mut buf)?;
Ok(buf)
})
.collect::<Result<Vec<Vec<u8>>>>()?;
let dependencies = compiled_package.get_dependency_original_package_ids();

// Publish package
let pt = {
let mut builder = ProgrammableTransactionBuilder::new();
builder.publish_immutable(modules, dependencies);
builder.finish()
};

// Setup gas budget and gas price
let gas_budget = 50_000_000;
let gas_price = iota_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,
vec![gas_coin.object_ref()],
pt,
gas_budget,
gas_price,
);

// Sign the transaction
let signature = keystore.sign_secure(&sender, &tx_data, Intent::iota_transaction())?;

// Execute transaction
let transaction_response = iota_client
.quorum_driver_api()
.execute_transaction_block(
Transaction::from_data(tx_data, vec![signature]),
IotaTransactionBlockResponseOptions::full_content(),
Some(ExecuteTransactionRequestType::WaitForLocalExecution),
)
.await?;

println!(
"Package publishing transaction digest: {}",
transaction_response.digest
);

// Extract package id from the transaction effects
let tx_effects = transaction_response
.effects
.expect("Transaction has no effects");
let package_ref = tx_effects
.created()
.first()
.expect("There are no created objects");
let package_id = package_ref.reference.object_id;
println!("Package ID: {}", package_id);
Ok(package_id)
}
24 changes: 3 additions & 21 deletions docs/examples/rust/stardust/alias-output-claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
//! In order to work, it requires a network with test objects
//! generated from iota-genesis-builder/src/stardust/test_outputs.
use std::{fs, path::PathBuf, str::FromStr};
use std::str::FromStr;

use anyhow::anyhow;
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore};
use docs_examples::{clean_keystore, setup_keystore};
use iota_keys::keystore::AccountKeystore;
use iota_sdk::{
rpc_types::{IotaData, IotaObjectDataOptions, IotaTransactionBlockResponseOptions},
types::{
Expand All @@ -29,25 +30,6 @@ use shared_crypto::intent::Intent;
/// Got from iota-genesis-builder/src/stardust/test_outputs/stardust_mix.rs
const MAIN_ADDRESS_MNEMONIC: &str = "okay pottery arch air egg very cave cash poem gown sorry mind poem crack dawn wet car pink extra crane hen bar boring salt";

/// Creates a temporary keystore.
fn setup_keystore() -> Result<FileBasedKeystore, anyhow::Error> {
// Create a temporary keystore.
let keystore_path = PathBuf::from("iotatempdb");
if !keystore_path.exists() {
let keystore = FileBasedKeystore::new(&keystore_path)?;
keystore.save()?;
}
// Read the iota keystore.
FileBasedKeystore::new(&keystore_path)
}

fn clean_keystore() -> Result<(), anyhow::Error> {
// Remove the keystore files.
fs::remove_file("iotatempdb")?;
fs::remove_file("iotatempdb.aliases")?;
Ok(())
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Build an IOTA client for a local network.
Expand Down
24 changes: 3 additions & 21 deletions docs/examples/rust/stardust/basic-output-claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
//! In order to work, it requires a network with test objects
//! generated from iota-genesis-builder/src/stardust/test_outputs.
use std::{fs, path::PathBuf, str::FromStr};
use std::str::FromStr;

use anyhow::anyhow;
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore};
use docs_examples::{clean_keystore, setup_keystore};
use iota_keys::keystore::AccountKeystore;
use iota_sdk::{
rpc_types::{IotaData, IotaObjectDataOptions, IotaTransactionBlockResponseOptions},
types::{
Expand All @@ -29,25 +30,6 @@ use shared_crypto::intent::Intent;
/// Got from iota-genesis-builder/src/stardust/test_outputs/stardust_mix.rs
const MAIN_ADDRESS_MNEMONIC: &str = "rain flip mad lamp owner siren tower buddy wolf shy tray exit glad come dry tent they pond wrist web cliff mixed seek drum";

/// Creates a temporary keystore
fn setup_keystore() -> Result<FileBasedKeystore, anyhow::Error> {
// Create a temporary keystore
let keystore_path = PathBuf::from("iotatempdb");
if !keystore_path.exists() {
let keystore = FileBasedKeystore::new(&keystore_path)?;
keystore.save()?;
}
// Read iota keystore
FileBasedKeystore::new(&keystore_path)
}

fn clean_keystore() -> Result<(), anyhow::Error> {
// Remove files
fs::remove_file("iotatempdb")?;
fs::remove_file("iotatempdb.aliases")?;
Ok(())
}

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Build an iota client for a local network
Expand Down
Loading

0 comments on commit dffd179

Please sign in to comment.