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