unc_jsonrpc_client/methods/
broadcast_tx_commit.rs

1//! Sends blocking transactions.
2//!
3//! Sends a signed transaction to the RPC and waits until the transaction is fully complete.
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 `broadcast_tx_commit` method can be found at [`contract_change_method`](https://github.com/unc/unc-jsonrpc-client-rs/blob/master/examples/contract_change_method_commit.rs).
10//!
11//! ## Example
12//!
13//! ```no_run
14//! use unc_jsonrpc_client::{methods, JsonRpcClient};
15//! use unc_jsonrpc_primitives::types::{query::QueryResponseKind, transactions::TransactionInfo};
16//! use unc_primitives::types::{AccountId, BlockReference};
17//! use unc_primitives::transaction::{Action, FunctionCallAction, Transaction};
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.unc.org");
23//!
24//! let signer_account_id = "fido.testnet".parse::<AccountId>()?;
25//! let signer_secret_key = "ed25519:12dhevYshfiRqFSu8DSfxA27pTkmGRv6C5qQWTJYTcBEoB7MSTyidghi5NWXzWqrxCKgxVx97bpXPYQxYN5dieU".parse()?;
26//!
27//! let signer = unc_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 {
33//!     signer_id: signer.account_id.clone(),
34//!     public_key: signer.public_key.clone(),
35//!     nonce: 904565 + 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: 100_000_000_000_000, // 100 TeraGas
47//!         deposit: 0,
48//!     }))],
49//! };
50//!
51//! let request = methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
52//!     signed_transaction: transaction.sign(&signer)
53//! };
54//! # Ok(())
55//! # }
56//! ```
57use super::*;
58
59pub use unc_jsonrpc_primitives::types::transactions::RpcTransactionError;
60pub use unc_primitives::transaction::SignedTransaction;
61
62pub type RpcBroadcastTxCommitResponse = unc_primitives::views::FinalExecutionOutcomeView;
63
64#[derive(Debug)]
65pub struct RpcBroadcastTxCommitRequest {
66    pub signed_transaction: SignedTransaction,
67}
68
69impl From<RpcBroadcastTxCommitRequest>
70    for unc_jsonrpc_primitives::types::transactions::RpcSendTransactionRequest
71{
72    fn from(this: RpcBroadcastTxCommitRequest) -> Self {
73        Self {
74            signed_transaction: this.signed_transaction,
75            wait_until: unc_primitives::views::TxExecutionStatus::None,
76        }
77    }
78}
79
80impl RpcMethod for RpcBroadcastTxCommitRequest {
81    type Response = RpcBroadcastTxCommitResponse;
82    type Error = RpcTransactionError;
83
84    fn method_name(&self) -> &str {
85        "broadcast_tx_commit"
86    }
87
88    fn params(&self) -> Result<serde_json::Value, io::Error> {
89        Ok(json!([common::serialize_signed_transaction(
90            &self.signed_transaction
91        )?]))
92    }
93}
94
95impl private::Sealed for RpcBroadcastTxCommitRequest {}