TransactionBuilder

Struct TransactionBuilder 

Source
pub struct TransactionBuilder { /* private fields */ }
Expand description

A builder for creating programmable transaction blocks.

Inputs and commands are added incrementally through methods like pure, object, move_call, and transfer_objects. Once all commands and metadata have been set, call try_build for offline building, or build (with the intents feature) to resolve intents and gas via an RPC client.

§Example

use sui_sdk_types::Address;
use sui_sdk_types::Digest;
use sui_transaction_builder::ObjectInput;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();

let amount = tx.pure(&1_000_000_000u64);
let gas = tx.gas();
let coins = tx.split_coins(gas, vec![amount]);

let recipient = tx.pure(&Address::ZERO);
tx.transfer_objects(coins, recipient);

tx.set_sender(Address::ZERO);
tx.set_gas_budget(500_000_000);
tx.set_gas_price(1000);
tx.add_gas_objects([ObjectInput::owned(Address::ZERO, 1, Digest::ZERO)]);

let transaction = tx.try_build().expect("build should succeed");

Implementations§

Source§

impl TransactionBuilder

Source

pub fn new() -> Self

Create a new, empty transaction builder.

use sui_transaction_builder::TransactionBuilder;

let tx = TransactionBuilder::new();
Source

pub fn gas(&mut self) -> Argument

Return an Argument referring to the gas coin.

The gas coin can be used as an input to commands such as split_coins.

Note: The gas coin cannot be used when using an account’s Address Balance to pay for gas fees.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let gas = tx.gas();
Source

pub fn pure_bytes(&mut self, bytes: Vec<u8>) -> Argument

Add a pure input from raw BCS bytes.

If the same bytes have already been added, the existing Argument is returned (inputs are deduplicated). Use pure_bytes_unique when deduplication is not desired.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let a = tx.pure_bytes(vec![1, 0, 0, 0, 0, 0, 0, 0]);
let b = tx.pure_bytes(vec![1, 0, 0, 0, 0, 0, 0, 0]);
// `a` and `b` refer to the same input
Source

pub fn pure<T: Serialize>(&mut self, value: &T) -> Argument

Add a pure input by serializing value to BCS.

Pure inputs are values like integers, addresses, and strings — anything that is not an on-chain object. Identical values are deduplicated; use pure_unique if each call must produce a distinct input.

use sui_sdk_types::Address;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let amount = tx.pure(&1_000_000_000u64);
let recipient = tx.pure(&Address::ZERO);
Source

pub fn pure_bytes_unique(&mut self, bytes: Vec<u8>) -> Argument

Add a pure input from raw BCS bytes, always creating a new input.

Unlike pure_bytes, this method never deduplicates — each call produces a distinct input even if the bytes are identical.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let a = tx.pure_bytes_unique(vec![42]);
let b = tx.pure_bytes_unique(vec![42]);
// `a` and `b` are distinct inputs despite identical bytes
Source

pub fn pure_unique<T: Serialize>(&mut self, value: &T) -> Argument

Add a pure input by serializing value to BCS, always creating a new input.

This is the non-deduplicating variant of pure.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let a = tx.pure_unique(&1u64);
let b = tx.pure_unique(&1u64);
// `a` and `b` are distinct inputs
Source

pub fn object(&mut self, object: ObjectInput) -> Argument

Add an object input to the transaction.

If an object with the same ID has already been added, the existing Argument is returned and any additional metadata (version, digest, mutability) from the new ObjectInput is merged in.

use sui_sdk_types::Address;
use sui_sdk_types::Digest;
use sui_transaction_builder::ObjectInput;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let obj = tx.object(ObjectInput::owned(Address::ZERO, 1, Digest::ZERO));
Source

pub fn funds_withdrawal(&mut self, coin_type: TypeTag, amount: u64) -> Argument

Add a funds-withdrawal input that requests amount of coin_type from the sender’s Address Balance.

The returned Argument represents the raw FundsWithdrawal input. In most cases you’ll want funds_withdrawal_coin or funds_withdrawal_balance which additionally call the appropriate redeem_funds function.

Source

pub fn funds_withdrawal_coin( &mut self, coin_type: TypeTag, amount: u64, ) -> Argument

Withdraw funds from the sender’s Address Balance and redeem them as a Coin<T>.

This adds a FundsWithdrawal input and calls 0x2::coin::redeem_funds to convert it into a Coin<T>.

Source

pub fn funds_withdrawal_balance( &mut self, coin_type: TypeTag, amount: u64, ) -> Argument

Withdraw funds from the sender’s Address Balance and redeem them as a Balance<T>.

This adds a FundsWithdrawal input and calls 0x2::balance::redeem_funds to convert it into a Balance<T>.

Source

pub fn add_gas_objects<O, I>(&mut self, gas: I)
where O: Into<ObjectInput>, I: IntoIterator<Item = O>,

Add one or more gas objects to use to pay for the transaction.

use sui_sdk_types::Address;
use sui_sdk_types::Digest;
use sui_transaction_builder::ObjectInput;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
tx.add_gas_objects([ObjectInput::owned(Address::ZERO, 1, Digest::ZERO)]);
Source

pub fn set_gas_budget(&mut self, budget: u64)

Set the gas budget for the transaction.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
tx.set_gas_budget(500_000_000);
Source

pub fn set_gas_price(&mut self, price: u64)

Set the gas price for the transaction.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
tx.set_gas_price(1000);
Source

pub fn set_sender(&mut self, sender: Address)

Set the sender of the transaction.

use sui_sdk_types::Address;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
tx.set_sender(Address::ZERO);
Source

pub fn set_sponsor(&mut self, sponsor: Address)

Set the sponsor of the transaction.

If not set, the sender is used as the gas owner.

Source

pub fn set_expiration(&mut self, expiration: TransactionExpiration)

Set the expiration of the transaction.

Source

pub fn move_call( &mut self, function: Function, arguments: Vec<Argument>, ) -> Argument

Call a Move function with the given arguments.

function is a structured representation of a package::module::function, optionally with type arguments (see Function and Function::with_type_args).

The return value is a result argument that can be used in subsequent commands. If the Move call returns multiple results, access them with Argument::to_nested.

use sui_sdk_types::Address;
use sui_sdk_types::Identifier;
use sui_transaction_builder::Function;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let result = tx.move_call(
    Function::new(
        Address::TWO,
        Identifier::from_static("coin"),
        Identifier::from_static("zero"),
    )
    .with_type_args(vec!["0x2::sui::SUI".parse().unwrap()]),
    vec![],
);
Source

pub fn transfer_objects(&mut self, objects: Vec<Argument>, address: Argument)

Transfer a list of objects to the given address.

use sui_sdk_types::Address;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let gas = tx.gas();
let amount = tx.pure(&1_000_000_000u64);
let coins = tx.split_coins(gas, vec![amount]);
let recipient = tx.pure(&Address::ZERO);
tx.transfer_objects(coins, recipient);
Source

pub fn split_coins( &mut self, coin: Argument, amounts: Vec<Argument>, ) -> Vec<Argument>

Split a coin by the provided amounts, returning multiple results (as many as there are amounts). The returned vector of Arguments is guaranteed to be the same length as the provided amounts vector.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let gas = tx.gas();
let a = tx.pure(&1_000u64);
let b = tx.pure(&2_000u64);
let coins = tx.split_coins(gas, vec![a, b]);
assert_eq!(coins.len(), 2);
Source

pub fn merge_coins(&mut self, coin: Argument, coins_to_merge: Vec<Argument>)

Merge a list of coins into a single coin.

use sui_sdk_types::Address;
use sui_sdk_types::Digest;
use sui_transaction_builder::ObjectInput;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let coin_a = tx.object(ObjectInput::owned(Address::ZERO, 1, Digest::ZERO));
let coin_b = tx.object(ObjectInput::owned(
    Address::from_static("0x1"),
    1,
    Digest::ZERO,
));
tx.merge_coins(coin_a, vec![coin_b]);
Source

pub fn make_move_vec( &mut self, type_: Option<TypeTag>, elements: Vec<Argument>, ) -> Argument

Make a Move vector from a list of elements.

If the elements are not objects, or the vector is empty, a type_ must be supplied. Returns the Move vector as an argument that can be used in subsequent commands.

use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();
let a = tx.pure(&1u64);
let b = tx.pure(&2u64);
let vec = tx.make_move_vec(Some("u64".parse().unwrap()), vec![a, b]);
Source

pub fn publish( &mut self, modules: Vec<Vec<u8>>, dependencies: Vec<Address>, ) -> Argument

Publish a list of modules with the given dependencies. The result is the 0x2::package::UpgradeCap Move type. Note that the upgrade capability needs to be handled after this call:

  • transfer it to the transaction sender or another address
  • burn it
  • wrap it for access control
  • discard the it to make a package immutable

The arguments required for this command are:

  • modules: is the modules’ bytecode to be published
  • dependencies: is the list of IDs of the transitive dependencies of the package
Source

pub fn upgrade( &mut self, modules: Vec<Vec<u8>>, dependencies: Vec<Address>, package: Address, ticket: Argument, ) -> Argument

Upgrade a Move package.

  • modules: is the modules’ bytecode for the modules to be published
  • dependencies: is the list of IDs of the transitive dependencies of the package to be upgraded
  • package: is the ID of the current package being upgraded
  • ticket: is the upgrade ticket

To get the ticket, you have to call the 0x2::package::authorize_upgrade function, and pass the package ID, the upgrade policy, and package digest.

Source

pub fn intent<I: Intent>(&mut self, intent: I) -> Argument

Available on crate feature intents only.

Register a transaction intent which is resolved later to either an input or a sequence of commands.

Intents are high-level descriptions of what the transaction needs (e.g., a coin of a certain value) that get resolved when build is called. See CoinWithBalance for an example of an Intent.

use sui_transaction_builder::TransactionBuilder;
use sui_transaction_builder::intent::CoinWithBalance;

let mut tx = TransactionBuilder::new();
let coin = tx.intent(CoinWithBalance::sui(1_000_000_000));
// `coin` can be passed to subsequent commands
Source

pub fn try_build(self) -> Result<Transaction, Error>

Build the transaction offline.

All metadata (sender, gas budget, gas price, gas objects) and any object inputs must be fully specified before calling this method. Returns an Error if any required fields are missing or if unresolved intents remain.

use sui_sdk_types::Address;
use sui_sdk_types::Digest;
use sui_transaction_builder::ObjectInput;
use sui_transaction_builder::TransactionBuilder;

let mut tx = TransactionBuilder::new();

let gas = tx.gas();
let amount = tx.pure(&1_000_000_000u64);
let coins = tx.split_coins(gas, vec![amount]);
let recipient = tx.pure(&Address::ZERO);
tx.transfer_objects(coins, recipient);

tx.set_sender(Address::ZERO);
tx.set_gas_budget(500_000_000);
tx.set_gas_price(1000);
tx.add_gas_objects([ObjectInput::owned(Address::ZERO, 1, Digest::ZERO)]);

let transaction = tx.try_build().unwrap();
Source

pub async fn build(self, client: &mut Client) -> Result<Transaction, Error>

Available on crate feature intents only.

Build the transaction by resolving intents and gas via an RPC client.

This method resolves any registered intents (e.g., CoinWithBalance), performs gas selection if needed, and simulates the transaction before returning the finalized [Transaction].

§Errors

Returns an Error if the sender is not set, intent resolution fails, or the simulated execution fails.

Trait Implementations§

Source§

impl Default for TransactionBuilder

Source§

fn default() -> TransactionBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<U> As for U

§

fn as_<T>(self) -> T
where T: CastFrom<U>,

Casts self to type T. The semantics of numeric casting with the as operator are followed, so <T as As>::as_::<U> can be used in the same way as T as U for numeric conversions. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Pipe for T
where T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more