Skip to content

Commit

Permalink
Add error type for rpc calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
saeed-zil committed Jun 24, 2024
1 parent cd4b7d2 commit c16ac26
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/sdk/libs/near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ near-account-id = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_with = {workspace = true, features = ["base64"]}
thiserror.workspace = true

calimero-sdk = { path = "../../" }
calimero-primitives = { path = "../../../primitives" }
10 changes: 10 additions & 0 deletions crates/sdk/libs/near/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use thiserror::Error;

#[derive(Debug, Error)]
pub enum NearLibError {
#[error(transparent)]
JsonError(#[from] serde_json::Error),

#[error("Failed to fetch: {0}")]
FetchError(String),
}
13 changes: 7 additions & 6 deletions crates/sdk/libs/near/src/jsonrpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use calimero_sdk::env;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

use crate::error::NearLibError;

pub(crate) struct Client {
url: String,
id: std::cell::RefCell<u64>,
Expand All @@ -19,7 +21,7 @@ impl Client {
&self,
method: &str,
params: P,
) -> Result<Response<T, E>, String> {
) -> Result<Response<T, E>, NearLibError> {
let headers = [("Content-Type", "application/json")];

*self.id.borrow_mut() += 1;
Expand All @@ -28,12 +30,11 @@ impl Client {
id: &*self.id.borrow().to_string(),
method,
params,
})
.map_err(|err| format!("Cannot serialize request: {:?}", err))?;
})?;

let response = unsafe { env::ext::fetch(&self.url, "POST", &headers, &body) }?;
serde_json::from_slice::<Response<T, E>>(&response)
.map_err(|e| format!("Failed to parse response: {}", e.to_string(),))
let response = unsafe { env::ext::fetch(&self.url, "POST", &headers, &body) }
.map_err(NearLibError::FetchError)?;
Ok(serde_json::from_slice::<Response<T, E>>(&response)?)
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/sdk/libs/near/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use query::{QueryResponseKind, RpcQueryRequest};
use types::{BlockId, FunctionArgs, StoreKey};
use views::QueryRequest;

pub mod error;
mod jsonrpc;
pub mod query;
pub mod types;
Expand Down
1 change: 1 addition & 0 deletions crates/sdk/libs/near/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub type BlockHash = calimero_primitives::hash::Hash;
pub type AccountId = near_account_id::AccountId;
pub type StorageUsage = u64;
pub type Nonce = u64;
pub type Balance = u128;

#[derive(Debug, Clone, serde::Serialize)]
#[serde(untagged)]
Expand Down
28 changes: 17 additions & 11 deletions crates/sdk/libs/near/src/views.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serde_with::DisplayFromStr;
use std::sync::Arc;

use serde_with::base64::Base64;
use serde_with::serde_as;

use crate::types::{
AccountId, BlockHeight, FunctionArgs, Nonce, StorageUsage, StoreKey, StoreValue,
AccountId, Balance, BlockHeight, FunctionArgs, Nonce, StorageUsage, StoreKey, StoreValue,
};

#[derive(serde::Serialize, Debug)]
Expand Down Expand Up @@ -38,11 +39,14 @@ pub enum QueryRequest {
},
}

#[serde_as]
#[derive(serde::Deserialize, Debug, Clone)]
pub struct AccountView {
pub amount: String,
pub locked: String,
pub code_hash: String,
#[serde_as(as = "DisplayFromStr")]
pub amount: Balance,
#[serde_as(as = "DisplayFromStr")]
pub locked: Balance,
pub code_hash: calimero_primitives::hash::Hash,
pub storage_usage: StorageUsage,
pub storage_paid_at: BlockHeight,
}
Expand All @@ -53,7 +57,7 @@ pub struct ContractCodeView {
#[serde(rename = "code_base64")]
#[serde_as(as = "Base64")]
pub code: Box<[u8]>,
pub hash: String,
pub hash: Box<str>,
}

#[derive(serde::Deserialize, Debug, Clone)]
Expand All @@ -77,12 +81,14 @@ pub struct AccessKeyView {
pub permission: AccessKeyPermissionView,
}

#[serde_as]
#[derive(Debug, Clone, serde::Deserialize)]
pub enum AccessKeyPermissionView {
FunctionCall {
allowance: Option<String>,
receiver_id: String,
method_names: Vec<String>,
#[serde_as(as = "Option<DisplayFromStr>")]
allowance: Option<Balance>,
receiver_id: Box<str>,
method_names: Box<[Box<str>]>,
},
FullAccess,
}
Expand All @@ -94,12 +100,12 @@ pub struct AccessKeyList {

#[derive(serde::Deserialize, Debug, Clone)]
pub struct AccessKeyInfoView {
pub public_key: String,
pub public_key: Box<str>,
pub access_key: AccessKeyView,
}

#[derive(serde::Deserialize, Debug, Clone)]
pub struct CallResult {
pub result: Vec<u8>,
pub logs: Vec<String>,
pub result: Box<[u8]>,
pub logs: Box<[Box<str>]>,
}

0 comments on commit c16ac26

Please sign in to comment.