openmina_node_common/service/rpc/
transaction_pool.rs

1#[cfg(target_family = "wasm")]
2use gloo_utils::format::JsValueSerdeExt;
3use mina_p2p_messages::v2;
4use node::rpc::*;
5#[cfg(target_family = "wasm")]
6use wasm_bindgen::prelude::*;
7
8use super::RpcSender;
9
10#[derive(Clone)]
11#[cfg_attr(target_family = "wasm", wasm_bindgen)]
12pub struct TransactionPool {
13    #[allow(unused)]
14    sender: RpcSender,
15}
16
17#[derive(Clone)]
18#[cfg_attr(target_family = "wasm", wasm_bindgen)]
19pub struct TransactionPoolInject {
20    #[allow(unused)]
21    sender: RpcSender,
22}
23
24impl TransactionPool {
25    pub fn new(sender: RpcSender) -> Self {
26        Self { sender }
27    }
28
29    async fn _get(&self) -> Option<RpcTransactionPoolResponse> {
30        self.sender
31            .oneshot_request(RpcRequest::TransactionPoolGet)
32            .await
33    }
34}
35
36#[cfg_attr(target_family = "wasm", wasm_bindgen)]
37impl TransactionPool {
38    pub fn inject(&self) -> TransactionPoolInject {
39        TransactionPoolInject {
40            sender: self.sender.clone(),
41        }
42    }
43}
44
45#[cfg(not(target_family = "wasm"))]
46impl TransactionPool {
47    pub async fn get(&self) -> Option<RpcTransactionPoolResponse> {
48        self._get().await
49    }
50}
51
52#[cfg(target_family = "wasm")]
53#[cfg_attr(target_family = "wasm", wasm_bindgen)]
54impl TransactionPool {
55    pub async fn get(&self) -> JsValue {
56        JsValue::from_serde(&self._get().await).unwrap_or_default()
57    }
58}
59
60impl TransactionPoolInject {
61    async fn _payment(
62        &self,
63        payments: Vec<RpcInjectPayment>,
64    ) -> Result<Option<RpcTransactionInjectResponse>, String> {
65        let res = self
66            .sender
67            .oneshot_request(RpcRequest::TransactionInject(
68                payments
69                    .into_iter()
70                    .map(v2::MinaBaseUserCommandStableV2::try_from)
71                    .collect::<Result<_, _>>()
72                    .map_err(|err| err.to_string())?,
73            ))
74            .await;
75        Ok(res)
76    }
77}
78
79#[cfg(not(target_family = "wasm"))]
80impl TransactionPoolInject {
81    pub async fn payment(
82        &self,
83        payments: Vec<RpcInjectPayment>,
84    ) -> Result<Option<RpcTransactionInjectResponse>, String> {
85        self._payment(payments).await
86    }
87}
88
89#[cfg(target_family = "wasm")]
90#[cfg_attr(target_family = "wasm", wasm_bindgen)]
91impl TransactionPoolInject {
92    pub async fn payment(&self, payments: JsValue) -> Result<JsValue, JsValue> {
93        let payments: Vec<RpcInjectPayment> = if payments.is_array() {
94            payments.into_serde().map_err(|err| err.to_string())?
95        } else {
96            let payment = payments.into_serde().map_err(|err| err.to_string())?;
97            vec![payment]
98        };
99
100        self._payment(payments)
101            .await
102            .map(|res| JsValue::from_serde(&res).unwrap_or_default())
103            .map_err(Into::into)
104    }
105}