near_jsonrpc_client/methods/
tx.rs

1//! Queries the status of a transaction.
2//!
3//! ## Example
4//! Returns the final transaction result for
5//! <https://explorer.near.org/transactions/B9aypWiMuiWR5kqzewL9eC96uZWA3qCMhLe67eBMWacq>
6//!
7//! ```no_run
8//! use near_jsonrpc_client::{methods, JsonRpcClient};
9//!
10//! # #[tokio::main]
11//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
12//! use near_primitives::views::{FinalExecutionOutcomeViewEnum, TxExecutionStatus};
13//! let client = JsonRpcClient::connect("https://archival-rpc.mainnet.fastnear.com");
14//! let tx_hash = "B9aypWiMuiWR5kqzewL9eC96uZWA3qCMhLe67eBMWacq".parse()?;
15//!
16//! let request = methods::tx::RpcTransactionStatusRequest {
17//!     transaction_info: methods::tx::TransactionInfo::TransactionId {
18//!         tx_hash,
19//!         sender_account_id: "itranscend.near".parse()?,
20//!    },
21//!     wait_until: TxExecutionStatus::Executed,
22//! };
23//!
24//! let response = client.call(request).await?;
25//! let outcome = response.final_execution_outcome.expect("Should be executed by this moment");
26//! match outcome {
27//!     FinalExecutionOutcomeViewEnum::FinalExecutionOutcome(outcome) => {
28//!         assert_eq!(tx_hash, outcome.transaction.hash);
29//!     }
30//!     FinalExecutionOutcomeViewEnum::FinalExecutionOutcomeWithReceipt(_) => {
31//!         panic!("We haven't asked for the receipts");
32//!     }
33//! };
34//! # Ok(())
35//! # }
36//! ```
37use super::*;
38
39pub use near_jsonrpc_primitives::types::transactions::RpcTransactionError;
40pub use near_jsonrpc_primitives::types::transactions::RpcTransactionResponse;
41pub use near_jsonrpc_primitives::types::transactions::TransactionInfo;
42
43#[derive(Debug)]
44pub struct RpcTransactionStatusRequest {
45    pub transaction_info: TransactionInfo,
46    pub wait_until: near_primitives::views::TxExecutionStatus,
47}
48
49impl From<RpcTransactionStatusRequest>
50    for near_jsonrpc_primitives::types::transactions::RpcTransactionStatusRequest
51{
52    fn from(this: RpcTransactionStatusRequest) -> Self {
53        Self {
54            transaction_info: this.transaction_info,
55            wait_until: this.wait_until,
56        }
57    }
58}
59
60impl RpcMethod for RpcTransactionStatusRequest {
61    type Response = RpcTransactionResponse;
62    type Error = RpcTransactionError;
63
64    fn method_name(&self) -> &str {
65        "tx"
66    }
67
68    fn params(&self) -> Result<serde_json::Value, io::Error> {
69        Ok(match &self.transaction_info {
70            TransactionInfo::Transaction { signed_tx } => {
71                json!({
72                    "signed_tx_base64": common::serialize_signed_transaction(signed_tx)?,
73                    "wait_until": self.wait_until
74                })
75            }
76            TransactionInfo::TransactionId {
77                tx_hash,
78                sender_account_id,
79            } => {
80                json!({
81                    "tx_hash": tx_hash,
82                    "sender_account_id": sender_account_id,
83                    "wait_until": self.wait_until
84                })
85            }
86        })
87    }
88}
89
90impl private::Sealed for RpcTransactionStatusRequest {}