1use std::sync::Arc;
2
3use near_api_types::{transaction::PrepopulateTransaction, AccountId, Action};
4
5use crate::{
6 common::send::{ExecuteSignedTransaction, Transactionable},
7 config::NetworkConfig,
8 errors::{ArgumentValidationError, ValidationError},
9 signer::Signer,
10};
11
12#[derive(Clone, Debug)]
13pub struct TransactionWithSign<T: Transactionable + 'static> {
14 pub tx: T,
15}
16
17impl<T: Transactionable> TransactionWithSign<T> {
18 pub fn with_signer(self, signer: Arc<Signer>) -> ExecuteSignedTransaction {
19 ExecuteSignedTransaction::new(self.tx, signer)
20 }
21}
22
23#[derive(Clone, Debug)]
24pub struct SelfActionBuilder {
25 pub actions: Vec<Action>,
26}
27
28impl Default for SelfActionBuilder {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl SelfActionBuilder {
35 pub const fn new() -> Self {
36 Self {
37 actions: Vec::new(),
38 }
39 }
40
41 pub fn add_action(mut self, action: Action) -> Self {
43 self.actions.push(action);
44 self
45 }
46
47 pub fn add_actions(mut self, actions: Vec<Action>) -> Self {
49 self.actions.extend(actions);
50 self
51 }
52
53 pub fn with_signer(
55 self,
56 signer_account_id: AccountId,
57 signer: Arc<Signer>,
58 ) -> ExecuteSignedTransaction {
59 ConstructTransaction::new(signer_account_id.clone(), signer_account_id)
60 .add_actions(self.actions)
61 .with_signer(signer)
62 }
63}
64
65#[derive(Debug, Clone)]
67pub struct ConstructTransaction {
68 pub transaction: Result<PrepopulateTransaction, ArgumentValidationError>,
69}
70
71impl ConstructTransaction {
72 pub const fn new(signer_id: AccountId, receiver_id: AccountId) -> Self {
74 Self {
75 transaction: Ok(PrepopulateTransaction {
76 signer_id,
77 receiver_id,
78 actions: Vec::new(),
79 }),
80 }
81 }
82
83 pub fn with_deferred_error(mut self, error: ArgumentValidationError) -> Self {
84 self.transaction = Err(error);
85 self
86 }
87
88 pub fn add_action(mut self, action: Action) -> Self {
90 if let Ok(transaction) = &mut self.transaction {
91 transaction.actions.push(action);
92 }
93 self
94 }
95
96 pub fn add_actions(mut self, actions: Vec<Action>) -> Self {
98 if let Ok(transaction) = &mut self.transaction {
99 transaction.actions.extend(actions);
100 }
101 self
102 }
103
104 pub fn with_signer(self, signer: Arc<Signer>) -> ExecuteSignedTransaction {
106 ExecuteSignedTransaction::new(self, signer)
107 }
108}
109
110#[async_trait::async_trait]
111impl Transactionable for ConstructTransaction {
112 fn prepopulated(&self) -> Result<PrepopulateTransaction, ArgumentValidationError> {
113 self.transaction.clone()
114 }
115
116 async fn validate_with_network(&self, _: &NetworkConfig) -> Result<(), ValidationError> {
117 if let Err(e) = &self.transaction {
118 return Err(e.to_owned().into());
119 }
120 Ok(())
121 }
122}
123
124#[derive(Clone, Debug)]
128pub struct Transaction;
129
130impl Transaction {
131 pub const fn construct(signer_id: AccountId, receiver_id: AccountId) -> ConstructTransaction {
168 ConstructTransaction::new(signer_id, receiver_id)
169 }
170
171 pub fn use_transaction(
194 unsigned_tx: PrepopulateTransaction,
195 signer: Arc<Signer>,
196 ) -> ExecuteSignedTransaction {
197 ConstructTransaction::new(unsigned_tx.signer_id, unsigned_tx.receiver_id)
198 .add_actions(unsigned_tx.actions)
199 .with_signer(signer)
200 }
201
202 }