From 3901d33f54feea31039873d381c81d3903cb22c7 Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Mon, 24 Jun 2024 13:01:52 +0330 Subject: [PATCH] Change the design --- crates/sdk/libs/near/src/error.rs | 3 + crates/sdk/libs/near/src/lib.rs | 153 +++--------------------------- crates/sdk/libs/near/src/query.rs | 15 +++ 3 files changed, 31 insertions(+), 140 deletions(-) diff --git a/crates/sdk/libs/near/src/error.rs b/crates/sdk/libs/near/src/error.rs index 041d0588d..294eded51 100644 --- a/crates/sdk/libs/near/src/error.rs +++ b/crates/sdk/libs/near/src/error.rs @@ -5,6 +5,9 @@ pub enum NearLibError { #[error(transparent)] JsonError(#[from] serde_json::Error), + #[error(transparent)] + IoError(#[from] std::io::Error), + #[error("Failed to fetch: {0}")] FetchError(String), } diff --git a/crates/sdk/libs/near/src/lib.rs b/crates/sdk/libs/near/src/lib.rs index 9a4b288b2..913b2320b 100644 --- a/crates/sdk/libs/near/src/lib.rs +++ b/crates/sdk/libs/near/src/lib.rs @@ -1,7 +1,5 @@ +use error::NearLibError; use jsonrpc::Response; -use query::{QueryResponseKind, RpcQueryRequest}; -use types::{BlockId, FunctionArgs, StoreKey}; -use views::QueryRequest; pub mod error; mod jsonrpc; @@ -13,6 +11,13 @@ pub struct Client { client: jsonrpc::Client, } +pub trait RpcMethod { + type Response: serde::de::DeserializeOwned; + + fn method_name(&self) -> &str; + fn params(&self) -> Result; +} + impl Client { pub fn testnet() -> Self { Self { @@ -26,142 +31,10 @@ impl Client { } } - pub fn view_account( - &self, - account_id: &str, - block_id: BlockId, - ) -> Result { - let request = RpcQueryRequest { - block_id, - request: QueryRequest::ViewAccount { - account_id: account_id.to_string(), - }, - }; - let response: Response = - self.client.call("query", request)?; - - match response.data { - Ok(r) => { - if let QueryResponseKind::ViewAccount(va) = r.kind { - return Ok(va); - } - return Err("Unexpected response returned.".to_string()); - } - Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), - } - } - - pub fn view_code( - &self, - account_id: &str, - block_id: BlockId, - ) -> Result { - let request = RpcQueryRequest { - block_id, - request: QueryRequest::ViewCode { - account_id: account_id.to_string(), - }, - }; - - let response: Response = - self.client.call("query", request)?; - - match response.data { - Ok(r) => { - if let QueryResponseKind::ViewCode(vc) = r.kind { - return Ok(vc); - } - return Err("Unexpected response returned.".to_string()); - } - Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), - } - } - - pub fn view_state( - &self, - account_id: &str, - prefix: StoreKey, - include_proof: bool, - block_id: BlockId, - ) -> Result { - let request = RpcQueryRequest { - block_id, - request: QueryRequest::ViewState { - account_id: account_id.to_string(), - prefix, - include_proof, - }, - }; - - let response: Response = - self.client.call("query", request)?; - - match response.data { - Ok(r) => { - if let QueryResponseKind::ViewState(vs) = r.kind { - return Ok(vs); - } - return Err("Unexpected response returned.".to_string()); - } - Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), - } - } - - pub fn view_access_key( - &self, - account_id: &str, - public_key: &str, - block_id: BlockId, - ) -> Result { - let request = RpcQueryRequest { - block_id, - request: QueryRequest::ViewAccessKey { - account_id: account_id.to_string(), - public_key: public_key.to_string(), - }, - }; - - let response: Response = - self.client.call("query", request)?; - - match response.data { - Ok(r) => { - if let QueryResponseKind::ViewState(vs) = r.kind { - return Ok(vs); - } - return Err("Unexpected response returned.".to_string()); - } - Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), - } - } - - pub fn call_function( - &self, - account_id: &str, - method_name: &str, - args: FunctionArgs, - block_id: BlockId, - ) -> Result { - let request = RpcQueryRequest { - block_id, - request: QueryRequest::CallFunction { - account_id: account_id.to_string(), - method_name: method_name.to_string(), - args, - }, - }; - - let response: Response = - self.client.call("query", request)?; - - match response.data { - Ok(r) => { - if let QueryResponseKind::CallResult(cr) = r.kind { - return Ok(cr); - } - return Err("Unexpected response returned.".to_string()); - } - Err(e) => Err(format!("Error: {}, Code: {}", e.message, e.code,)), - } + pub fn call(&self, method: M) -> Result, NearLibError> + where + M: RpcMethod, + { + self.client.call(method.method_name(), &method.params()?) } } diff --git a/crates/sdk/libs/near/src/query.rs b/crates/sdk/libs/near/src/query.rs index 6d383ba74..44ff4bbb8 100644 --- a/crates/sdk/libs/near/src/query.rs +++ b/crates/sdk/libs/near/src/query.rs @@ -1,8 +1,11 @@ +use serde_json::json; + use crate::types::{BlockHash, BlockHeight, BlockId}; use crate::views::{ AccessKeyList, AccessKeyView, AccountView, CallResult, ContractCodeView, QueryRequest, ViewStateResult, }; +use crate::RpcMethod; #[derive(serde::Serialize, Debug)] pub struct RpcQueryRequest { @@ -29,3 +32,15 @@ pub enum QueryResponseKind { AccessKeyList(AccessKeyList), CallResult(CallResult), } + +impl RpcMethod for RpcQueryRequest { + type Response = RpcQueryResponse; + + fn method_name(&self) -> &str { + "query" + } + + fn params(&self) -> Result { + Ok(json!(self)) + } +}