pub struct Client { /* private fields */ }Expand description
GraphQL client for Sui blockchain.
Implementations§
Source§impl Client
impl Client
Sourcepub async fn chain_identifier(&self) -> Result<Digest, Error>
pub async fn chain_identifier(&self) -> Result<Digest, Error>
Get the chain identifier (e.g., “35834a8a” for mainnet).
§Example
use sui_graphql::Client;
let client = Client::new("https://graphql.mainnet.sui.io/graphql")?;
let chain_id = client.chain_identifier().await?;
println!("Connected to chain: {}", chain_id);Sourcepub async fn protocol_version(&self) -> Result<u64, Error>
pub async fn protocol_version(&self) -> Result<u64, Error>
Get the current protocol version.
§Example
use sui_graphql::Client;
let client = Client::new("https://graphql.mainnet.sui.io/graphql")?;
let version = client.protocol_version().await?;
println!("Protocol version: {}", version);Sourcepub async fn epoch(&self, epoch_id: Option<u64>) -> Result<Option<Epoch>, Error>
pub async fn epoch(&self, epoch_id: Option<u64>) -> Result<Option<Epoch>, Error>
Get epoch information by ID, or the current epoch if no ID is provided.
Returns None if the epoch does not exist or was pruned.
§Example
use sui_graphql::Client;
let client = Client::new("https://graphql.mainnet.sui.io/graphql")?;
// Get current epoch
let epoch = client.epoch(None).await?;
// Get specific epoch
let epoch = client.epoch(Some(100)).await?;Source§impl Client
impl Client
Sourcepub async fn get_checkpoint(
&self,
sequence_number: Option<u64>,
) -> Result<Option<CheckpointResponse>, Error>
pub async fn get_checkpoint( &self, sequence_number: Option<u64>, ) -> Result<Option<CheckpointResponse>, Error>
Fetch a checkpoint by its sequence number, or the latest checkpoint if not specified.
Returns:
Ok(Some(response))if the checkpoint existsOk(None)if the checkpoint does not existErr(Error::Request)for network errorsErr(Error::Base64)/Err(Error::Bcs)for decoding errors
Source§impl Client
impl Client
Sourcepub async fn get_balance(
&self,
owner: Address,
coin_type: &StructTag,
) -> Result<Option<Balance>, Error>
pub async fn get_balance( &self, owner: Address, coin_type: &StructTag, ) -> Result<Option<Balance>, Error>
Get the balance for a specific coin type owned by an address.
§Example
use sui_graphql::Client;
use sui_sdk_types::Address;
use sui_sdk_types::StructTag;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(Client::MAINNET)?;
let owner: Address = "0x123...".parse()?;
// Get SUI balance using the helper
let sui_balance = client.get_balance(owner, &StructTag::sui()).await?;
if let Some(bal) = sui_balance {
println!("SUI balance: {}", bal.total_balance);
}
// Get balance for other coin types by parsing the type string
let usdc_type: StructTag = "0xdba...::usdc::USDC".parse()?;
let usdc_balance = client.get_balance(owner, &usdc_type).await?;
if let Some(bal) = usdc_balance {
println!("USDC balance: {}", bal.total_balance);
}
Ok(())
}Sourcepub fn list_balances(
&self,
owner: Address,
) -> impl Stream<Item = Result<Balance, Error>> + '_
pub fn list_balances( &self, owner: Address, ) -> impl Stream<Item = Result<Balance, Error>> + '_
Stream all coin balances owned by an address.
§Example
use futures::StreamExt;
use std::pin::pin;
use sui_graphql::Client;
use sui_sdk_types::Address;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new(Client::MAINNET)?;
let owner: Address = "0x123...".parse()?;
let mut stream = pin!(client.list_balances(owner));
while let Some(result) = stream.next().await {
let balance = result?;
println!("{}: {}", balance.coin_type, balance.total_balance);
}
Ok(())
}Source§impl Client
impl Client
Sourcepub fn dynamic_fields(&self, parent: Address) -> DynamicFieldsRequest<'_>
pub fn dynamic_fields(&self, parent: Address) -> DynamicFieldsRequest<'_>
Create a request builder for listing dynamic fields on an object.
Sourcepub fn dynamic_field<N: Serialize>(
&self,
parent: Address,
name_type: TypeTag,
name: Bcs<N>,
) -> DynamicFieldRequest<'_, N>
pub fn dynamic_field<N: Serialize>( &self, parent: Address, name_type: TypeTag, name: Bcs<N>, ) -> DynamicFieldRequest<'_, N>
Create a request builder for fetching a single dynamic field by name.
Sourcepub fn dynamic_object_field<N: Serialize>(
&self,
parent: Address,
name_type: TypeTag,
name: Bcs<N>,
) -> DynamicFieldRequest<'_, N>
pub fn dynamic_object_field<N: Serialize>( &self, parent: Address, name_type: TypeTag, name: Bcs<N>, ) -> DynamicFieldRequest<'_, N>
Create a request builder for fetching a single dynamic object field by name.
Source§impl Client
impl Client
Sourcepub async fn execute_transaction(
&self,
transaction: &Transaction,
signatures: &[UserSignature],
) -> Result<ExecutionResult, Error>
pub async fn execute_transaction( &self, transaction: &Transaction, signatures: &[UserSignature], ) -> Result<ExecutionResult, Error>
Execute a signed transaction on chain.
This commits the transaction to the blockchain and waits for finality.
Execution errors (e.g., invalid signatures, insufficient gas) are returned as
GraphQL errors with code BAD_USER_INPUT, accessible via Response::errors().
§Arguments
transaction- The transaction to executesignatures- List of signatures authorizing the transaction
§Returns
Ok(result)witheffectsandbalance_changesif successfulErr(...)for network or decoding errors
Source§impl Client
impl Client
Sourcepub async fn get_object(
&self,
object_id: Address,
) -> Result<Option<Object>, Error>
pub async fn get_object( &self, object_id: Address, ) -> Result<Option<Object>, Error>
Fetch an object by its ID and deserialize from BCS.
Returns:
Ok(Some(object))if the object existsOk(None)if the object does not existErr(Error::Request)for network errorsErr(Error::Base64)/Err(Error::Bcs)for decoding errors
§Example
use sui_graphql::Client;
use sui_sdk_types::Address;
let client = Client::new(Client::MAINNET)?;
let object_id: Address = "0x5".parse()?;
match client.get_object(object_id).await? {
Some(object) => println!("Object version: {}", object.version()),
None => println!("Object not found"),
}Sourcepub async fn get_object_at_version(
&self,
object_id: Address,
version: u64,
) -> Result<Option<Object>, Error>
pub async fn get_object_at_version( &self, object_id: Address, version: u64, ) -> Result<Option<Object>, Error>
Fetch an object at a specific version.
Sourcepub async fn get_object_at_checkpoint(
&self,
object_id: Address,
checkpoint: u64,
) -> Result<Option<Object>, Error>
pub async fn get_object_at_checkpoint( &self, object_id: Address, checkpoint: u64, ) -> Result<Option<Object>, Error>
Fetch an object at a specific checkpoint.
Returns the object’s state as of the given checkpoint.
Sourcepub async fn get_object_with_root_version(
&self,
object_id: Address,
root_version: u64,
) -> Result<Option<Object>, Error>
pub async fn get_object_with_root_version( &self, object_id: Address, root_version: u64, ) -> Result<Option<Object>, Error>
Fetch an object with a root version bound.
This is useful for fetching child or wrapped objects bounded by their root object’s version. The object will be fetched at the latest version at or before the given root version.
Sourcepub fn list_objects(
&self,
owner: Address,
) -> impl Stream<Item = Result<Object, Error>> + '_
pub fn list_objects( &self, owner: Address, ) -> impl Stream<Item = Result<Object, Error>> + '_
Stream all objects owned by an address.
Handles pagination automatically, fetching pages as needed.
§Example
use futures::StreamExt;
use std::pin::pin;
use sui_graphql::Client;
use sui_sdk_types::Address;
let client = Client::new(Client::TESTNET)?;
let owner: Address = "0x123...".parse()?;
let mut stream = pin!(client.list_objects(owner));
while let Some(result) = stream.next().await {
let object = result?;
println!("Object version: {}", object.version());
}Source§impl Client
impl Client
Sourcepub async fn get_transaction(
&self,
digest: &str,
) -> Result<Option<TransactionResponse>, Error>
pub async fn get_transaction( &self, digest: &str, ) -> Result<Option<TransactionResponse>, Error>
Fetch a transaction by its digest and deserialize from BCS.
Returns:
Ok(Some(response))if the transaction existsOk(None)if the transaction does not existErr(Error::Request)for network errorsErr(Error::Base64)/Err(Error::Bcs)for decoding errors
§Example
use sui_graphql::Client;
let client = Client::new("https://graphql.mainnet.sui.io/graphql")?;
let digest = "ABC123..."; // transaction digest
match client.get_transaction(digest).await? {
Some(tx) => {
println!("Sender: {}", tx.transaction.sender);
println!("Status: {:?}", tx.effects.status());
}
None => println!("Transaction not found"),
}Source§impl Client
impl Client
Sourcepub const MAINNET: &str = "https://graphql.mainnet.sui.io/graphql"
pub const MAINNET: &str = "https://graphql.mainnet.sui.io/graphql"
URL for the Sui Foundation provided GraphQL service for mainnet.
Sourcepub const TESTNET: &str = "https://graphql.testnet.sui.io/graphql"
pub const TESTNET: &str = "https://graphql.testnet.sui.io/graphql"
URL for the Sui Foundation provided GraphQL service for testnet.
Sourcepub const DEVNET: &str = "https://graphql.devnet.sui.io/graphql"
pub const DEVNET: &str = "https://graphql.devnet.sui.io/graphql"
URL for the Sui Foundation provided GraphQL service for devnet.
Sourcepub fn new(endpoint: &str) -> Result<Self, Error>
pub fn new(endpoint: &str) -> Result<Self, Error>
Create a new GraphQL client with the given endpoint.
§Example
use sui_graphql::Client;
let client = Client::new(Client::MAINNET).unwrap();Sourcepub async fn query<T: DeserializeOwned>(
&self,
query: &str,
variables: Value,
) -> Result<Response<T>, Error>
pub async fn query<T: DeserializeOwned>( &self, query: &str, variables: Value, ) -> Result<Response<T>, Error>
Execute a GraphQL query and return the response.
The response contains both data and any errors (GraphQL supports partial success).
§Example
use serde::Deserialize;
use sui_graphql::Client;
#[derive(Deserialize)]
struct MyResponse {
#[serde(rename = "chainIdentifier")]
chain_identifier: String,
}
#[tokio::main]
async fn main() -> Result<(), sui_graphql::Error> {
let client = Client::new(Client::MAINNET)?;
let response = client
.query::<MyResponse>("query { chainIdentifier }", serde_json::json!({}))
.await?;
// Check for partial errors
if response.has_errors() {
for err in response.errors() {
eprintln!("GraphQL error: {}", err.message());
}
}
// Access the data
if let Some(data) = response.data() {
println!("Chain: {}", data.chain_identifier);
}
Ok(())
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.