From 7ade0752c676b94c8d54b2079d69732303427e35 Mon Sep 17 00:00:00 2001 From: MeowCrypto <112191530+FucktheKingcode@users.noreply.github.com> Date: Sat, 1 Jun 2024 02:00:10 +0700 Subject: [PATCH] [rust-sdk] add function_move_call example (#18008) ## Description I have added an example in [Sui SDKs] [Rust SDK] of Move call function with parameters to call hello wolrd module on Sui Blockchain ## Test plan Completed --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] Indexer: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK: --------- Co-authored-by: root Co-authored-by: stefan-mysten <135084671+stefan-mysten@users.noreply.github.com> --- crates/sui-sdk/examples/function_move_call.rs | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 crates/sui-sdk/examples/function_move_call.rs diff --git a/crates/sui-sdk/examples/function_move_call.rs b/crates/sui-sdk/examples/function_move_call.rs new file mode 100644 index 0000000000000..a6776d7f02361 --- /dev/null +++ b/crates/sui-sdk/examples/function_move_call.rs @@ -0,0 +1,104 @@ +// Copyright (c) Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +mod utils; +use anyhow::anyhow; +use shared_crypto::intent::Intent; +use sui_config::{sui_config_dir, SUI_KEYSTORE_FILENAME}; +use sui_keys::keystore::{AccountKeystore, FileBasedKeystore}; +use sui_sdk::{ + rpc_types::SuiTransactionBlockResponseOptions, + types::{ + base_types::ObjectID, + programmable_transaction_builder::ProgrammableTransactionBuilder, + quorum_driver_types::ExecuteTransactionRequestType, + transaction::{ + Argument, CallArg, Command, ProgrammableMoveCall, Transaction, TransactionData, + }, + Identifier, + }, +}; +use utils::setup_for_write; + +// This example shows how to use programmable transactions to chain multiple +// commands into one transaction, and specifically how to call a function from a move package +// These are the following steps: +// 1) finds a coin from the active address that has Sui, +// 2) creates a PTB and adds an input to it, +// 3) adds a move call to the PTB, +// 4) signs the transaction, +// 5) executes it. +// For some of these actions it prints some output. +// Finally, at the end of the program it prints the number of coins for the +// Sui address that received the coin. +// If you run this program several times, you should see the number of coins +// for the recipient address increases. + +#[tokio::main] +async fn main() -> Result<(), anyhow::Error> { + // 1) get the Sui client, the sender and recipient that we will use + // for the transaction, and find the coin we use as gas + let (sui, sender, _recipient) = setup_for_write().await?; + + // we need to find the coin we will use as gas + let coins = sui + .coin_read_api() + .get_coins(sender, None, None, None) + .await?; + let coin = coins.data.into_iter().next().unwrap(); + + // 2) create a programmable transaction builder to add commands and create a PTB + let mut ptb = ProgrammableTransactionBuilder::new(); + + // Create an Argument::Input for Pure 6 value of type u64 + let input_value = 10u64; + let input_argument = CallArg::Pure(bcs::to_bytes(&input_value).unwrap()); + + // Add this input to the builder + ptb.input(input_argument)?; + + // 3) add a move call to the PTB + // Replace the pkg_id with the package id you want to call + let pkg_id = "0x883393ee444fb828aa0e977670cf233b0078b41d144e6208719557cb3888244d"; + let package = ObjectID::from_hex_literal(pkg_id).map_err(|e| anyhow!(e))?; + let module = Identifier::new("hello_wolrd").map_err(|e| anyhow!(e))?; + let function = Identifier::new("hello_world").map_err(|e| anyhow!(e))?; + ptb.command(Command::MoveCall(Box::new(ProgrammableMoveCall { + package, + module, + function, + type_arguments: vec![], + arguments: vec![Argument::Input(0)], + }))); + + // build the transaction block by calling finish on the ptb + let builder = ptb.finish(); + + let gas_budget = 10_000_000; + let gas_price = sui.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![coin.object_ref()], + builder, + gas_budget, + gas_price, + ); + + // 4) sign transaction + let keystore = FileBasedKeystore::new(&sui_config_dir()?.join(SUI_KEYSTORE_FILENAME))?; + let signature = keystore.sign_secure(&sender, &tx_data, Intent::sui_transaction())?; + + // 5) execute the transaction + print!("Executing the transaction..."); + let transaction_response = sui + .quorum_driver_api() + .execute_transaction_block( + Transaction::from_data(tx_data, vec![signature]), + SuiTransactionBlockResponseOptions::full_content(), + Some(ExecuteTransactionRequestType::WaitForLocalExecution), + ) + .await?; + println!("{}", transaction_response); + Ok(()) +}