near_jsonrpc_client/methods/broadcast_tx_async.rs
1//! Sends asynchronous transactions.
2//!
3//! ## Example
4//!
5//! Constructs a signed transaction to be sent to an RPC node. It returns the transaction hash if successful.
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 sake.
8//!
9//! A full example on how to use `broadcast_tx_async` method can be found at [`contract_change_method`](https://github.com/near/near-jsonrpc-client-rs/blob/master/examples/contract_change_method.rs).
10//!
11//! ```no_run
12//! use near_jsonrpc_client::{methods, JsonRpcClient};
13//! use near_primitives::gas::Gas;
14//! use near_primitives::types::{AccountId, Balance};
15//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
16//! use near_crypto::SecretKey;
17//! use core::str::FromStr;
18//! use serde_json::json;
19//!
20//! # #[tokio::main]
21//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
22//! let client = JsonRpcClient::connect("https://archival-rpc.testnet.fastnear.com");
23//!
24//! let signer_account_id = "fido.testnet".parse::<AccountId>()?;
25//! let signer_secret_key = SecretKey::from_str("ed25519:12dhevYshfiRqFSu8DSfxA27pTkmGRv6C5qQWTJYTcBEoB7MSTyidghi5NWXzWqrxCKgxVx97bpXPYQxYN5dieU")?;
26//!
27//! let signer = near_crypto::InMemorySigner::from_secret_key(signer_account_id, signer_secret_key);
28//!
29//! let other_account = "rpc_docs.testnet".parse::<AccountId>()?;
30//! let rating = "4.5".parse::<f32>()?;
31//!
32//! let transaction = Transaction::V0(TransactionV0 {
33//! signer_id: signer.get_account_id(),
34//! public_key: signer.public_key().clone(),
35//! nonce: 10223934 + 1,
36//! receiver_id: "nosedive.testnet".parse::<AccountId>()?,
37//! block_hash: "AUDcb2iNUbsmCsmYGfGuKzyXKimiNcCZjBKTVsbZGnoH".parse()?,
38//! actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
39//! method_name: "rate".to_string(),
40//! args: json!({
41//! "account_id": other_account,
42//! "rating": rating,
43//! })
44//! .to_string()
45//! .into_bytes(),
46//! gas: Gas::from_teragas(100),
47//! deposit: Balance::ZERO,
48//! }))],
49//! });
50//!
51//! let request = methods::broadcast_tx_async::RpcBroadcastTxAsyncRequest {
52//! signed_transaction: transaction.sign(&signer)
53//! };
54//! # Ok(())
55//! # }
56//! ```
57use super::*;
58
59pub use near_primitives::transaction::SignedTransaction;
60
61pub type RpcBroadcastTxAsyncResponse = near_primitives::hash::CryptoHash;
62
63#[derive(Debug)]
64pub struct RpcBroadcastTxAsyncRequest {
65 pub signed_transaction: SignedTransaction,
66}
67
68impl From<RpcBroadcastTxAsyncRequest>
69 for near_jsonrpc_primitives::types::transactions::RpcSendTransactionRequest
70{
71 fn from(this: RpcBroadcastTxAsyncRequest) -> Self {
72 Self {
73 signed_transaction: this.signed_transaction,
74 wait_until: near_primitives::views::TxExecutionStatus::None,
75 }
76 }
77}
78
79#[derive(Debug, Serialize, Deserialize, Error)]
80#[error("{}", unreachable!("fatal: this error should never be constructed"))]
81pub enum RpcBroadcastTxAsyncError {}
82
83impl RpcHandlerResponse for RpcBroadcastTxAsyncResponse {}
84
85impl RpcHandlerError for RpcBroadcastTxAsyncError {}
86
87impl RpcMethod for RpcBroadcastTxAsyncRequest {
88 type Response = RpcBroadcastTxAsyncResponse;
89 type Error = RpcBroadcastTxAsyncError;
90
91 fn method_name(&self) -> &str {
92 "broadcast_tx_async"
93 }
94
95 fn params(&self) -> Result<serde_json::Value, io::Error> {
96 Ok(json!([common::serialize_signed_transaction(
97 &self.signed_transaction
98 )?]))
99 }
100}
101
102impl private::Sealed for RpcBroadcastTxAsyncRequest {}