Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
saeed-zil committed Jun 18, 2024
1 parent a6fbc46 commit 7bef965
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 127 deletions.
23 changes: 11 additions & 12 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = [
"./crates/primitives",
"./crates/runtime",
"./crates/sdk",
"./crates/near-sdk",
"./crates/sdk/libs/near",
"./crates/sdk/macros",
"./crates/server",
"./crates/server-primitives",
Expand Down
1 change: 0 additions & 1 deletion apps/only-peers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ crate-type = ["cdylib"]

[dependencies]
calimero-sdk = { path = "../../crates/sdk" }
near-sdk = { path = "../../crates/near-sdk" }
49 changes: 0 additions & 49 deletions crates/near-sdk/src/jsonrpc.rs

This file was deleted.

46 changes: 0 additions & 46 deletions crates/near-sdk/src/lib.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ thiserror.workspace = true
ureq.workspace = true
wasmer.workspace = true
wasmer-types.workspace = true
borsh = { workspace = true, features = ["derive"] }

[[example]]
name = "demo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "near-sdk"
name = "calimero-sdk-near"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
Expand All @@ -9,5 +9,5 @@ license.workspace = true
[dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
calimero-sdk = { path = "../sdk" }
calimero-sdk = { path = "../../" }

66 changes: 66 additions & 0 deletions crates/sdk/libs/near/src/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use calimero_sdk::env;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

pub(crate) struct Client {
url: String,
}

impl Client {
pub fn new(url: String) -> Self {
Self { url }
}

pub fn call<T: DeserializeOwned, E: DeserializeOwned>(
&self,
method: &str,
params: serde_json::Value,
) -> Result<Response<T, E>, String> {
let headers = [("Content-Type", "application/json")];

let body = serde_json::to_vec(&serde_json::json!({
"jsonrpc": "2.0",
"id": "1",
"method": method,
"params": 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).unwrap()
}
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "Result")]
pub enum ResultAlt<T, E> {
#[serde(rename = "result")]
Ok(T),
#[serde(rename = "error")]
Err(E),
}

impl<T, E> From<ResultAlt<T, E>> for Result<T, E> {
fn from(result: ResultAlt<T, E>) -> Self {
match result {
ResultAlt::Ok(value) => Ok(value),
ResultAlt::Err(err) => Err(err),
}
}
}

#[derive(Debug, Clone, Deserialize)]
pub struct Response<T: DeserializeOwned, E: DeserializeOwned> {
pub jsonrpc: Option<String>,
pub id: String,

#[serde(with = "ResultAlt")]
pub data: Result<T, RpcError<E>>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct RpcError<E> {
pub code: i32,
pub message: String,
pub data: Option<E>,
}
43 changes: 43 additions & 0 deletions crates/sdk/libs/near/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use jsonrpc::Response;
use serde::Deserialize;

mod jsonrpc;

pub struct NearSdk {
client: jsonrpc::Client,
}

#[derive(Debug, Clone, Deserialize)]
pub struct BalanceResponse {
pub amount: String,
}

impl NearSdk {
pub fn testnet() -> Self {
Self {
client: jsonrpc::Client::new("https://rpc.testnet.near.org/".to_string()),
}
}

pub fn mainnet() -> Self {
Self {
client: jsonrpc::Client::new("https://rpc.mainnet.near.org/".to_string()),
}
}

pub fn get_balance(&self, account_id: &str) -> Result<BalanceResponse, String> {
let response: Response<BalanceResponse, String> = self.client.call(
"query",
serde_json::json!({
"request_type": "view_account",
"finality": "final",
"account_id": account_id,
}),
)?;

match response.data {
Ok(r) => Ok(r),
Err(e) => Err(e.message),
}
}
}
15 changes: 0 additions & 15 deletions crates/sdk/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use crate::sys;

#[doc(hidden)]
Expand Down Expand Up @@ -157,16 +155,3 @@ pub fn state_write<T: crate::state::AppState>(state: &T) {
};
storage_write(STATE_KEY, &data);
}

pub fn fetch(method: &str, url: &str, headers: HashMap<String, String>, body: Vec<u8>) -> String {
let headers = match borsh::to_vec(&headers) {
Ok(data) => data,
Err(err) => panic_str(&format!("Cannot serialize headers: {:?}", err)),
};
let method = sys::Buffer::from(method);
let url = sys::Buffer::from(url);
let headers = sys::Buffer::from(headers.as_slice());
let body = sys::Buffer::from(body.as_slice());
unsafe { sys::fetch(method, url, headers, body, DATA_REGISTER) }
String::from_utf8(read_register(DATA_REGISTER).unwrap_or_else(expected_register)).unwrap()
}

0 comments on commit 7bef965

Please sign in to comment.