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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::sync::Arc;
use near_api_types::{
AccountId, Action, CryptoHash, TxExecutionStatus, transaction::PrepopulateTransaction,
};
use crate::{
common::{
query::{
ReceiptHandler, RequestBuilder, TransactionStatusHandler,
tx_rpc::{
ReceiptRef, ReceiptRpc, TransactionProofRef, TransactionProofRpc,
TransactionStatusRef, TransactionStatusRpc,
},
},
send::{ExecuteSignedTransaction, Transactionable},
},
config::NetworkConfig,
errors::{ArgumentValidationError, ValidationError},
signer::Signer,
};
#[derive(Clone, Debug)]
pub struct TransactionWithSign<T: Transactionable + 'static> {
pub tx: T,
}
impl<T: Transactionable> TransactionWithSign<T> {
pub fn with_signer(self, signer: Arc<Signer>) -> ExecuteSignedTransaction {
ExecuteSignedTransaction::new(self.tx, signer)
}
}
#[derive(Clone, Debug)]
pub struct SelfActionBuilder {
pub actions: Vec<Action>,
}
impl Default for SelfActionBuilder {
fn default() -> Self {
Self::new()
}
}
impl SelfActionBuilder {
pub const fn new() -> Self {
Self {
actions: Vec::new(),
}
}
/// Adds an action to the transaction.
pub fn add_action(mut self, action: Action) -> Self {
self.actions.push(action);
self
}
/// Adds multiple actions to the transaction.
pub fn add_actions(mut self, actions: Vec<Action>) -> Self {
self.actions.extend(actions);
self
}
/// Signs the transaction with the given account id and signer related to it.
pub fn with_signer(
self,
signer_account_id: AccountId,
signer: Arc<Signer>,
) -> ExecuteSignedTransaction {
ConstructTransaction::new(signer_account_id.clone(), signer_account_id)
.add_actions(self.actions)
.with_signer(signer)
}
}
/// A builder for constructing transactions using Actions.
#[derive(Debug, Clone)]
pub struct ConstructTransaction {
pub transaction: Result<PrepopulateTransaction, ArgumentValidationError>,
}
impl ConstructTransaction {
/// Pre-populates a transaction with the given signer and receiver IDs.
pub const fn new(signer_id: AccountId, receiver_id: AccountId) -> Self {
Self {
transaction: Ok(PrepopulateTransaction {
signer_id,
receiver_id,
actions: Vec::new(),
}),
}
}
pub fn with_deferred_error(mut self, error: ArgumentValidationError) -> Self {
self.transaction = Err(error);
self
}
/// Adds an action to the transaction.
pub fn add_action(mut self, action: Action) -> Self {
if let Ok(transaction) = &mut self.transaction {
transaction.actions.push(action);
}
self
}
/// Adds multiple actions to the transaction.
pub fn add_actions(mut self, actions: Vec<Action>) -> Self {
if let Ok(transaction) = &mut self.transaction {
transaction.actions.extend(actions);
}
self
}
/// Signs the transaction with the given signer.
pub fn with_signer(self, signer: Arc<Signer>) -> ExecuteSignedTransaction {
ExecuteSignedTransaction::new(self, signer)
}
}
#[async_trait::async_trait]
impl Transactionable for ConstructTransaction {
fn prepopulated(&self) -> Result<PrepopulateTransaction, ArgumentValidationError> {
self.transaction.clone()
}
async fn validate_with_network(&self, _: &NetworkConfig) -> Result<(), ValidationError> {
if let Err(e) = &self.transaction {
return Err(e.to_owned().into());
}
Ok(())
}
}
/// Transaction related functionality.
///
/// This struct provides ability to interact with transactions.
#[derive(Clone, Debug)]
pub struct Transaction;
impl Transaction {
/// Constructs a new transaction builder with the given signer and receiver IDs.
/// This pattern is useful for batching actions into a single transaction.
///
/// This is the low level interface for constructing transactions.
/// It is designed to be used in scenarios where more control over the transaction process is required.
///
/// # Example
///
/// This example constructs a transaction with a two transfer actions.
///
/// ```rust,no_run
/// use near_api::{*, types::{transaction::actions::{Action, TransferAction}, json::U128}};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let signer = Signer::from_ledger()?;
///
/// let transaction_result = Transaction::construct(
/// "sender.near".parse()?,
/// "receiver.near".parse()?
/// )
/// .add_action(Action::Transfer(
/// TransferAction {
/// deposit: NearToken::from_near(1),
/// },
/// ))
/// .add_action(Action::Transfer(
/// TransferAction {
/// deposit: NearToken::from_near(1),
/// },
/// ))
/// .with_signer(signer)
/// .send_to_mainnet()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub const fn construct(signer_id: AccountId, receiver_id: AccountId) -> ConstructTransaction {
ConstructTransaction::new(signer_id, receiver_id)
}
/// Signs a transaction with the given signer.
///
/// This provides ability to sign custom constructed pre-populated transactions.
///
/// # Examples
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let signer = Signer::from_ledger()?;
/// # let unsigned_tx = todo!();
///
/// let transaction_result = Transaction::use_transaction(
/// unsigned_tx,
/// signer
/// )
/// .send_to_mainnet()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn use_transaction(
unsigned_tx: PrepopulateTransaction,
signer: Arc<Signer>,
) -> ExecuteSignedTransaction {
ConstructTransaction::new(unsigned_tx.signer_id, unsigned_tx.receiver_id)
.add_actions(unsigned_tx.actions)
.with_signer(signer)
}
/// Sets up a query to fetch the current status of a transaction by its hash and sender account ID.
///
/// Waits until the transaction has been optimistically executed ([`TxExecutionStatus::ExecutedOptimistic`]),
/// ensuring that outcome fields (gas usage, logs, status) are populated.
/// If you need to wait until the transaction reaches a different stage
/// (e.g., [`TxExecutionStatus::Final`] or [`TxExecutionStatus::None`]),
/// use [`Transaction::status_with_options`] instead.
///
/// The returned result is an [`ExecutionFinalResult`](near_api_types::transaction::result::ExecutionFinalResult)
/// which provides details about gas usage, logs, and the execution status.
///
/// # Example
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let tx_hash: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
/// let sender: AccountId = "sender.near".parse()?;
///
/// let result = Transaction::status(sender, tx_hash)
/// .fetch_from_mainnet()
/// .await?;
/// println!("Transaction success: {}", result.is_success());
/// # Ok(())
/// # }
/// ```
pub fn status(
sender_account_id: AccountId,
tx_hash: CryptoHash,
) -> RequestBuilder<TransactionStatusHandler> {
Self::status_with_options(
sender_account_id,
tx_hash,
TxExecutionStatus::ExecutedOptimistic,
)
}
/// Sets up a query to fetch the status of a transaction, waiting until it reaches
/// the specified execution stage.
///
/// Use [`TxExecutionStatus::None`] to return immediately with whatever state is available,
/// or [`TxExecutionStatus::Final`] to wait until the transaction is fully finalized.
///
/// # Example
///
/// ```rust,no_run
/// use near_api::{*, types::TxExecutionStatus};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let tx_hash: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
/// let sender: AccountId = "sender.near".parse()?;
///
/// let result = Transaction::status_with_options(
/// sender,
/// tx_hash,
/// TxExecutionStatus::Final,
/// )
/// .fetch_from_mainnet()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn status_with_options(
sender_account_id: AccountId,
tx_hash: CryptoHash,
wait_until: TxExecutionStatus,
) -> RequestBuilder<TransactionStatusHandler> {
RequestBuilder::new(
TransactionStatusRpc,
TransactionStatusRef {
sender_account_id,
tx_hash,
wait_until,
},
TransactionStatusHandler,
)
}
/// Sets up a query to fetch a receipt by its ID.
///
/// This uses the `EXPERIMENTAL_receipt` RPC method to retrieve the details of a specific receipt.
///
/// # Example
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let receipt_id: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
///
/// let receipt = Transaction::receipt(receipt_id)
/// .fetch_from_mainnet()
/// .await?;
/// println!("Receipt receiver: {:?}", receipt.receiver_id);
/// # Ok(())
/// # }
/// ```
pub fn receipt(receipt_id: CryptoHash) -> RequestBuilder<ReceiptHandler> {
RequestBuilder::new(ReceiptRpc, ReceiptRef { receipt_id }, ReceiptHandler)
}
/// Sets up a query to fetch the light client execution proof for a transaction.
///
/// This is used to verify a transaction's execution against a light client block header.
/// The `light_client_head` parameter specifies the block hash of the light client's latest known head.
///
/// # Example
///
/// ```rust,no_run
/// use near_api::*;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let tx_hash: CryptoHash = "EaNakSaXUTjbPsUJbuDdbuq3e6Ynmjo8zYUgDVqt1iTn".parse()?;
/// let sender: AccountId = "sender.near".parse()?;
/// let head_hash: CryptoHash = "3i1SypXzBRhLMvpHmNJXpg18FgVW6jNFrFcUqBF5Wmit".parse()?;
///
/// let proof = Transaction::proof(sender, tx_hash, head_hash)
/// .fetch_from_mainnet()
/// .await?;
/// println!("Proof block header: {:?}", proof.block_header_lite);
/// # Ok(())
/// # }
/// ```
pub fn proof(
sender_id: AccountId,
transaction_hash: CryptoHash,
light_client_head: CryptoHash,
) -> RequestBuilder<TransactionProofRpc> {
RequestBuilder::new(
TransactionProofRpc,
TransactionProofRef {
sender_id,
transaction_hash,
light_client_head,
},
TransactionProofRpc,
)
}
}