666lcz/knowledge_base
0
1---2title: Interact with Sui using the Rust SDK3---4 5## Overview6 7The [Sui SDK](https://github.com/MystenLabs/sui/tree/main/crates/sui-sdk) is a collection of Rust language JSON-RPC wrapper and crypto utilities you can use to interact with Sui.8 9Use the [`SuiClient`](cli-client.md) to create an HTTP or a WebSocket client (`SuiClient::new`). See the [JSON-RPC](json-rpc.md#sui-json-rpc-methods) documentation for the list of available methods.10 11**Note:** The WebSocket client supports only [subscription](event_api.md#subscribe-to-sui-events); use the HTTP client for other API methods.12 13## References14 15View the documentation for the [crates used in Sui](https://mystenlabs.github.io/sui/).16 17## Configuration18 19Add the `sui-sdk` crate in your [`Cargo.toml`](https://doc.rust-lang.org/cargo/reference/manifest.html) file:20 21```bash22[dependencies]23sui-sdk = { git = "https://github.com/MystenLabs/sui" }24```25 26Include the `branch` argument to use a specific branch of the Sui repository:27 28```bash29[dependencies]30sui-sdk = { git = "https://github.com/MystenLabs/sui", branch = "devnet" }31```32 33## Example 1 - Get all objects owned by an address34 35This code example prints a list of object summaries owned by the specified address.36 37```rust38use std::str::FromStr;39use sui_sdk::types::base_types::SuiAddress;40use sui_sdk::{SuiClient, SuiClientBuilder};41 42#[tokio::main]43async fn main() -> Result<(), anyhow::Error> {44 let sui = SuiClientBuilder::default().build(45 "https://fullnode.devnet.sui.io:443",46 ).await.unwrap();47 let address = SuiAddress::from_str("0xbcab7526033aa0e014f634bf51316715dda0907a7fab5a8d7e3bd44e634a4d44")?;48 let objects = sui.read_api().get_owned_objects(address).await?;49 println!("{:?}", objects.data);50 Ok(())51}52```53 54You can verify the result with the [Sui Explorer](https://suiexplorer.com/) if you are using a Sui Devnet Full node.55 56## Example 2 - Create and execute transaction57 58Use this example to conduct a transaction in Sui using the Sui Devnet Full node:59 60```rust61use std::str::FromStr;62use sui_sdk::{63 crypto::{FileBasedKeystore, Keystore},64 types::{65 base_types::{ObjectID, SuiAddress},66 crypto::Signature,67 messages::Transaction,68 },69 SuiClient,70 SuiClientBuilder,71};72 73#[tokio::main]74async fn main() -> Result<(), anyhow::Error> {75 let sui = SuiClientBuilder::default().build(76 "https://fullnode.devnet.sui.io:443",77 ).await.unwrap();78 // Load keystore from ~/.sui/sui_config/sui.keystore79 let keystore_path = match dirs::home_dir() {80 Some(v) => v.join(".sui").join("sui_config").join("sui.keystore"),81 None => panic!("Cannot obtain home directory path"),82 };83 84 let my_address = SuiAddress::from_str("0xbcab7526033aa0e014f634bf51316715dda0907a7fab5a8d7e3bd44e634a4d44")?;85 let gas_object_id = ObjectID::from_str("0xe638c76768804cebc0ab43e103999886641b0269a46783f2b454e2f8880b5255")?;86 let recipient = SuiAddress::from_str("0x727b37454ab13d5c1dbb22e8741bff72b145d1e660f71b275c01f24e7860e5e5")?;87 88 // Create a sui transfer transaction89 let transfer_tx = sui90 .transaction_builder()91 .transfer_sui(my_address, gas_object_id, 1000, recipient, Some(1000))92 .await?;93 94 // Sign transaction95 let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?);96 let signature = keystore.sign_secure(&my_address, &transfer_tx, Intent::default())?;97 98 // Execute the transaction99 let transaction_response = sui100 .quorum_driver()101 .execute_transaction_block(Transaction::from_data(transfer_tx, Intent::default(), signature))102 103 println!("{:?}", transaction_response);104 105 Ok(())106}107```108 109## Example 3 - Event subscription110 111Use the WebSocket client to [subscribe to events](event_api.md#subscribe-to-sui-events).112 113```rust114use futures::StreamExt;115use sui_sdk::rpc_types::SuiEventFilter;116use sui_sdk::{SuiClient, SuiClientBuilder};117 118#[tokio::main]119async fn main() -> Result<(), anyhow::Error> {120 let sui = SuiClientBuilder::default().build(121 "https://fullnode.devnet.sui.io:443",122 ).await.unwrap();123 let mut subscribe_all = sui.event_api().subscribe_event(SuiEventFilter::All(vec![])).await?;124 loop {125 println!("{:?}", subscribe_all.next().await);126 }127}128```129 130**Note:** The Event subscription service requires a running Sui Full node. To learn more, see [Full node setup](fullnode.md#fullnode-setup).131 