-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbroadcast_tx_commit.rs
More file actions
96 lines (88 loc) · 3.52 KB
/
broadcast_tx_commit.rs
File metadata and controls
96 lines (88 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Sends blocking transactions.
//!
//! Sends a signed transaction to the RPC and waits until the transaction is fully complete.
//!
//! Constructs a signed transaction to be sent to an RPC node.
//!
//! 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.
//!
//! A full example on how to use `broadcast_tx_commit` method can be found at [`contract_change_method`](https://github.com/near/near-jsonrpc-client-rs/blob/master/examples/contract_change_method_commit.rs).
//!
//! ## Example
//!
//! ```no_run
//! use near_jsonrpc_client::{methods, JsonRpcClient};
//! use near_jsonrpc_primitives::types::{query::QueryResponseKind, transactions::TransactionInfo};
//! use near_primitives::gas::Gas;
//! use near_primitives::types::{AccountId, Balance, BlockReference};
//! use near_primitives::transaction::{Action, FunctionCallAction, Transaction, TransactionV0};
//! use serde_json::json;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
//! let client = JsonRpcClient::connect("https://archival-rpc.testnet.fastnear.com");
//!
//! let signer_account_id = "fido.testnet".parse::<AccountId>()?;
//! let signer_secret_key = "ed25519:12dhevYshfiRqFSu8DSfxA27pTkmGRv6C5qQWTJYTcBEoB7MSTyidghi5NWXzWqrxCKgxVx97bpXPYQxYN5dieU".parse()?;
//!
//! let signer = near_crypto::InMemorySigner::from_secret_key(signer_account_id, signer_secret_key);
//!
//! let other_account = "rpc_docs.testnet".parse::<AccountId>()?;
//! let rating = "4.5".parse::<f32>()?;
//!
//! let transaction = Transaction::V0(TransactionV0 {
//! signer_id: signer.get_account_id(),
//! public_key: signer.public_key().clone(),
//! nonce: 904565 + 1,
//! receiver_id: "nosedive.testnet".parse::<AccountId>()?,
//! block_hash: "AUDcb2iNUbsmCsmYGfGuKzyXKimiNcCZjBKTVsbZGnoH".parse()?,
//! actions: vec![Action::FunctionCall(Box::new(FunctionCallAction {
//! method_name: "rate".to_string(),
//! args: json!({
//! "account_id": other_account,
//! "rating": rating,
//! })
//! .to_string()
//! .into_bytes(),
//! gas: Gas::from_teragas(100),
//! deposit: Balance::ZERO,
//! }))],
//! });
//!
//! let request = methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
//! signed_transaction: transaction.sign(&signer)
//! };
//! # Ok(())
//! # }
//! ```
use super::*;
pub use near_jsonrpc_primitives::types::transactions::RpcTransactionError;
pub use near_primitives::transaction::SignedTransaction;
pub type RpcBroadcastTxCommitResponse = near_primitives::views::FinalExecutionOutcomeView;
#[derive(Debug)]
pub struct RpcBroadcastTxCommitRequest {
pub signed_transaction: SignedTransaction,
}
impl From<RpcBroadcastTxCommitRequest>
for near_jsonrpc_primitives::types::transactions::RpcSendTransactionRequest
{
fn from(this: RpcBroadcastTxCommitRequest) -> Self {
Self {
signed_transaction: this.signed_transaction,
wait_until: near_primitives::views::TxExecutionStatus::default(),
}
}
}
impl RpcMethod for RpcBroadcastTxCommitRequest {
type Response = RpcBroadcastTxCommitResponse;
type Error = RpcTransactionError;
fn method_name(&self) -> &str {
"broadcast_tx_commit"
}
fn params(&self) -> Result<serde_json::Value, io::Error> {
Ok(json!([common::serialize_signed_transaction(
&self.signed_transaction
)?]))
}
}
impl private::Sealed for RpcBroadcastTxCommitRequest {}