-
Notifications
You must be signed in to change notification settings - Fork 14
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 pay_iota example (#1978)
* Add pay_iota example * Refactor utils * Capitalize comments * Use client method * Rename variable * Update crates/iota-sdk/examples/pay_iota.rs --------- Co-authored-by: Thibault Martinez <[email protected]>
- Loading branch information
1 parent
30df2be
commit 3632ee8
Showing
3 changed files
with
85 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! This example shows how to pay IOTAs to another address. | ||
//! | ||
//! cargo run --example pay_iota | ||
mod utils; | ||
|
||
use iota_config::{iota_config_dir, IOTA_KEYSTORE_FILENAME}; | ||
use iota_keys::keystore::{AccountKeystore, FileBasedKeystore}; | ||
use iota_sdk::{ | ||
rpc_types::IotaTransactionBlockResponseOptions, | ||
types::{quorum_driver_types::ExecuteTransactionRequestType, transaction::Transaction}, | ||
}; | ||
use shared_crypto::intent::Intent; | ||
use utils::setup_for_write; | ||
|
||
#[tokio::main] | ||
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?; | ||
|
||
// 2) Get the coin we will use as gas and for the payment | ||
let coins = iota | ||
.coin_read_api() | ||
.get_coins(sender, None, None, None) | ||
.await?; | ||
let gas_coin = coins.data.into_iter().next().unwrap(); | ||
|
||
let gas_budget = 5_000_000; | ||
|
||
// 3) Build the transaction data, to transfer 1_000 NANOS from the gas coin to | ||
// the recipient address | ||
let tx_data = iota | ||
.transaction_builder() | ||
.pay_iota( | ||
sender, | ||
vec![gas_coin.coin_object_id], | ||
vec![recipient], | ||
vec![1_000], | ||
gas_budget, | ||
) | ||
.await?; | ||
|
||
// 4) Sign transaction | ||
let keystore = FileBasedKeystore::new(&iota_config_dir()?.join(IOTA_KEYSTORE_FILENAME))?; | ||
let signature = keystore.sign_secure(&sender, &tx_data, Intent::iota_transaction())?; | ||
|
||
// 5) Execute the transaction | ||
println!("Executing the transaction..."); | ||
let transaction_response = iota | ||
.quorum_driver_api() | ||
.execute_transaction_block( | ||
Transaction::from_data(tx_data, vec![signature]), | ||
IotaTransactionBlockResponseOptions::full_content(), | ||
Some(ExecuteTransactionRequestType::WaitForLocalExecution), | ||
) | ||
.await?; | ||
|
||
println!("Transaction sent {}", transaction_response.digest); | ||
println!("Object changes:"); | ||
for object_change in transaction_response.object_changes.unwrap() { | ||
println!("{:?}", object_change); | ||
} | ||
|
||
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