near_jsonrpc_client/methods/send_tx.rs
1//! Sends a transaction.
2//!
3//! Sends a signed transaction to the RPC, returns the guaranteed execution status and the results the blockchain can provide at the moment.
4//!
5//! Constructs a signed transaction to be sent to an RPC node.
6//!
7//! This code sample doesn't make any requests to the RPC node. It only shows how to construct the request. It's been truncated for brevity.
8//!
9//! A full example on how to use `send_tx` method can be found at [`send_tx`](https://github.com/near/near-jsonrpc-client-rs/blob/master/examples/send_tx.rs).
10//!
11//! ## Example
12//!
13//! ```no_run
14//! use near_jsonrpc_client::{methods, JsonRpcClient};
15//! use near_jsonrpc_primitives::types::{query::QueryResponseKind, transactions::TransactionInfo};
16//! use near_primitives::types::{AccountId, BlockReference};
17//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
18//! use serde_json::json;
19//!
20//! # #[tokio::main]
21//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
22//! use near_primitives::views::TxExecutionStatus;
23//! let client = JsonRpcClient::connect("https://archival-rpc.testnet.fastnear.com");
24//!
25//! let signer_account_id = "fido.testnet".parse::<AccountId>()?;
26//! let signer_secret_key = "ed25519:12dhevYshfiRqFSu8DSfxA27pTkmGRv6C5qQWTJYTcBEoB7MSTyidghi5NWXzWqrxCKgxVx97bpXPYQxYN5dieU".parse()?;
27//!
28//! let signer = near_crypto::InMemorySigner::from_secret_key(signer_account_id, signer_secret_key);
29//!
30//! let other_account = "rpc_docs.testnet".parse::<AccountId>()?;
31//! let rating = "4.5".parse::<f32>()?;
32//!
33//! let transaction = Transaction::V0(TransactionV0 {
34//! signer_id: signer.get_account_id(),
35//! public_key: signer.public_key().clone(),
36//! nonce: 904565 + 1,
37//! receiver_id: "nosedive.testnet".parse::<AccountId>()?,
38//! block_hash: "AUDcb2iNUbsmCsmYGfGuKzyXKimiNcCZjBKTVsbZGnoH".parse()?,
39//! actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
40//! method_name: "rate".to_string(),
41//! args: json!({
42//! "account_id": other_account,
43//! "rating": rating,
44//! })
45//! .to_string()
46//! .into_bytes(),
47//! gas: 100_000_000_000_000, // 100 TeraGas
48//! deposit: 0,
49//! }))],
50//! });
51//!
52//! let request = methods::send_tx::RpcSendTransactionRequest {
53//! signed_transaction: transaction.sign(&signer),
54//! wait_until: TxExecutionStatus::IncludedFinal,
55//! };
56//! # Ok(())
57//! # }
58//! ```
59use super::*;
60pub use near_jsonrpc_primitives::types::transactions::{
61 RpcSendTransactionRequest, RpcTransactionResponse,
62};
63
64pub use near_jsonrpc_primitives::types::transactions::RpcTransactionError;
65pub use near_primitives::transaction::SignedTransaction;
66
67impl RpcMethod for RpcSendTransactionRequest {
68 type Response = RpcTransactionResponse;
69 type Error = RpcTransactionError;
70
71 fn method_name(&self) -> &str {
72 "send_tx"
73 }
74
75 fn params(&self) -> Result<serde_json::Value, io::Error> {
76 Ok(json!({
77 "signed_tx_base64": common::serialize_signed_transaction(&self.signed_transaction)?,
78 "wait_until": self.wait_until
79 }))
80 }
81}
82
83impl private::Sealed for RpcSendTransactionRequest {}