Browse Source

Merge pull request #12 from Relayz-io/silvestr/implement-view-call

Add view RPC implementation for NearClient
develop
Predko Silvestr 2 years ago
parent
commit
726d87314d
No account linked to committer's email address

+ 1
- 1
.github/workflows/ci.yml View File

@@ -9,7 +9,7 @@ on:
9 9
       - main
10 10
       - develop
11 11
   pull_request:
12
-    types: [opened, reopened, edited]
12
+    types: [opened, reopened, edited, synchronize]
13 13
     branches:    
14 14
       - main
15 15
       - develop

+ 0
- 1
near-client/Cargo.toml View File

@@ -10,7 +10,6 @@ description = """
10 10
 crate-type = ["cdylib", "rlib"]
11 11
 
12 12
 [dependencies]
13
-async-trait = "0.1"
14 13
 base64 = { version = "0.13" }
15 14
 bs58 = "0.4"
16 15
 borsh = "0.9"

+ 35
- 7
near-client/src/client.rs View File

@@ -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
- 8
near-client/src/types/crypto.rs View File

@@ -244,14 +244,6 @@ impl Keypair {
244 244
 
245 245
 macro_rules! util_impl {
246 246
     ($key_type: ty) => {
247
-        impl TryFrom<&[u8]> for $key_type {
248
-            type Error = Error;
249
-
250
-            fn try_from(data: &[u8]) -> Result<Self> {
251
-                Self::from_bytes(data)
252
-            }
253
-        }
254
-
255 247
         impl From<$key_type> for String {
256 248
             fn from(key: $key_type) -> Self {
257 249
                 Key::to_string(&key)

+ 0
- 9
near-client/src/types/near.rs View File

@@ -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
-}

+ 1
- 1
wasm-client/Cargo.toml View File

@@ -10,7 +10,7 @@ description = """
10 10
 crate-type = ["cdylib", "rlib"]
11 11
 
12 12
 [dependencies]
13
-vodozemac = { version = "0.2.0", features = ["js"] }
13
+vodozemac = { version = "0.2", features = ["js"] }
14 14
 serde = { version = "1", default-features = false, features = ["derive"] }
15 15
 serde_json = { version = "1", default-features = false }
16 16
 wasm-bindgen = "0.2"

Loading…
Cancel
Save