Procházet zdrojové kódy

Add view call to NearClient

develop
Silvestr Predko před 2 roky
rodič
revize
6e14ba5023
2 změnil soubory, kde provedl 35 přidání a 16 odebrání
  1. 35
    7
      near-client/src/client.rs
  2. 0
    9
      near-client/src/types/near.rs

+ 35
- 7
near-client/src/client.rs Zobrazit soubor

@@ -10,12 +10,13 @@ use crate::{
10 10
         crypto::{ED25519PublicKey, ED25519Signature, Keypair},
11 11
         near::{
12 12
             Action, Chunks, FinalExecutionOutcomeView, FinalExecutionStatus, FunctionCallAction,
13
-            SignedTransaction, Transaction,
13
+            SignedTransaction, Transaction, ViewResult,
14 14
         },
15 15
     },
16 16
     Error,
17 17
 };
18 18
 
19
+use serde::de::DeserializeOwned;
19 20
 use serde_json::{json, Value};
20 21
 use url::Url;
21 22
 
@@ -85,6 +86,38 @@ impl NearClient {
85 86
             .prev_block_hash)
86 87
     }
87 88
 
89
+    pub async fn view<'a, T: DeserializeOwned>(
90
+        &'a self,
91
+        contract_id: &'a AccountId,
92
+        method: &'static str,
93
+        args: Option<Value>,
94
+    ) -> Result<T> {
95
+        let view_result = self
96
+            .rpc_client
97
+            .request::<ViewResult>(
98
+                "query",
99
+                Some(json!({
100
+                    "request_type": "call_function",
101
+                    "finality": "optimistic",
102
+                    "account_id": contract_id,
103
+                    "method_name": method,
104
+                    "args_base64": args
105
+                        .map(|value| serde_json::to_vec(&value))
106
+                        .transpose()
107
+                        .map_err(Error::ArgsSerialize)?
108
+                        .map(base64::encode)
109
+                        .unwrap_or_default()
110
+                })),
111
+            )
112
+            .await
113
+            .map_err(|error| Error::Rpc {
114
+                error,
115
+                method: "query",
116
+            })?;
117
+
118
+        serde_json::from_slice(&view_result.result).map_err(Error::DeserializeTxResp)
119
+    }
120
+
88 121
     pub fn call<'a>(
89 122
         &'a self,
90 123
         signer: &'a mut Signer,
@@ -93,17 +126,12 @@ impl NearClient {
93 126
     ) -> TransactionBuilder {
94 127
         TransactionBuilder::new(self, signer, method, contract_id)
95 128
     }
96
-
97
-    pub fn view<'a>(&'a self, _contract_id: &'a AccountId, _method: &'static str) {}
98 129
 }
99 130
 
100 131
 pub struct Call<'a>(TransactionBuilder<'a>);
101 132
 
102 133
 impl<'a> Call<'a> {
103
-    pub async fn commit<T>(self) -> Result<T>
104
-    where
105
-        T: serde::de::DeserializeOwned,
106
-    {
134
+    pub async fn commit<T: DeserializeOwned>(self) -> Result<T> {
107 135
         let transaction_bytes = serialize_transaction(&self.0).await?;
108 136
 
109 137
         let execution_outcome = self

+ 0
- 9
near-client/src/types/near.rs Zobrazit soubor

@@ -352,12 +352,3 @@ pub struct ViewResult {
352 352
     pub result: Vec<u8>,
353 353
     pub logs: Vec<String>,
354 354
 }
355
-
356
-#[derive(Debug, Clone, Serialize, Deserialize)]
357
-pub struct View {
358
-    pub request_type: String,
359
-    pub finality: String,
360
-    pub method_name: String,
361
-    #[serde(with = "base64_format")]
362
-    pub args_base64: Vec<u8>,
363
-}

Načítá se…
Zrušit
Uložit