Skip to main content

Transaction

Struct Transaction 

Source
pub struct Transaction;
Expand description

Transaction related functionality.

This struct provides ability to interact with transactions.

Implementations§

Source§

impl Transaction

Source

pub const fn construct( signer_id: AccountId, receiver_id: AccountId, ) -> ConstructTransaction

Constructs a new transaction builder with the given signer and receiver IDs. This pattern is useful for batching actions into a single transaction.

This is the low level interface for constructing transactions. It is designed to be used in scenarios where more control over the transaction process is required.

§Example

This example constructs a transaction with a two transfer actions.

use near_api::{*, types::{transaction::actions::{Action, TransferAction}, json::U128}};

let signer = Signer::from_ledger()?;

let transaction_result = Transaction::construct(
    "sender.near".parse()?,
    "receiver.near".parse()?
)
.add_action(Action::Transfer(
    TransferAction {
        deposit: NearToken::from_near(1),
    },
))
.add_action(Action::Transfer(
    TransferAction {
        deposit: NearToken::from_near(1),
    },
))
.with_signer(signer)
.send_to_mainnet()
.await?;
Source

pub fn use_transaction( unsigned_tx: PrepopulateTransaction, signer: Arc<Signer>, ) -> ExecuteSignedTransaction

Signs a transaction with the given signer.

This provides ability to sign custom constructed pre-populated transactions.

§Examples
use near_api::*;

let signer = Signer::from_ledger()?;

let transaction_result = Transaction::use_transaction(
    unsigned_tx,
    signer
)
.send_to_mainnet()
.await?;
Source

pub fn status( sender_account_id: AccountId, tx_hash: CryptoHash, ) -> RequestBuilder<TransactionStatusHandler>

Sets up a query to fetch the current status of a transaction by its hash and sender account ID.

Waits until the transaction has been optimistically executed (TxExecutionStatus::ExecutedOptimistic), ensuring that outcome fields (gas usage, logs, status) are populated. If you need to wait until the transaction reaches a different stage (e.g., TxExecutionStatus::Final or TxExecutionStatus::None), use Transaction::status_with_options instead.

The returned result is an ExecutionFinalResult which provides details about gas usage, logs, and the execution status.

§Example
use near_api::*;

let tx_hash: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
let sender: AccountId = "sender.near".parse()?;

let result = Transaction::status(sender, tx_hash)
    .fetch_from_mainnet()
    .await?;
println!("Transaction success: {}", result.is_success());
Source

pub fn status_with_options( sender_account_id: AccountId, tx_hash: CryptoHash, wait_until: TxExecutionStatus, ) -> RequestBuilder<TransactionStatusHandler>

Sets up a query to fetch the status of a transaction, waiting until it reaches the specified execution stage.

Use TxExecutionStatus::None to return immediately with whatever state is available, or TxExecutionStatus::Final to wait until the transaction is fully finalized.

§Example
use near_api::{*, types::TxExecutionStatus};

let tx_hash: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
let sender: AccountId = "sender.near".parse()?;

let result = Transaction::status_with_options(
    sender,
    tx_hash,
    TxExecutionStatus::Final,
)
.fetch_from_mainnet()
.await?;
Source

pub fn receipt(receipt_id: CryptoHash) -> RequestBuilder<ReceiptHandler>

Sets up a query to fetch a receipt by its ID.

This uses the EXPERIMENTAL_receipt RPC method to retrieve the details of a specific receipt.

§Example
use near_api::*;

let receipt_id: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;

let receipt = Transaction::receipt(receipt_id)
    .fetch_from_mainnet()
    .await?;
println!("Receipt receiver: {:?}", receipt.receiver_id);
Source

pub fn proof( sender_id: AccountId, transaction_hash: CryptoHash, light_client_head: CryptoHash, ) -> RequestBuilder<TransactionProofRpc>

Sets up a query to fetch the light client execution proof for a transaction.

This is used to verify a transaction’s execution against a light client block header. The light_client_head parameter specifies the block hash of the light client’s latest known head.

§Example
use near_api::*;

let tx_hash: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
let sender: AccountId = "sender.near".parse()?;
let head_hash: CryptoHash = "3i1SypXzBRhLMvpHmNJXpg18FgVW6jNFrFcUqBF5Wmit".parse()?;

let proof = Transaction::proof(sender, tx_hash, head_hash)
    .fetch_from_mainnet()
    .await?;
println!("Proof block header: {:?}", proof.block_header_lite);

Trait Implementations§

Source§

impl Clone for Transaction

Source§

fn clone(&self) -> Transaction

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Transaction

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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
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
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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> PolicyExt for T
where T: ?Sized,

Source§

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

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

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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