Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext): add app-side library for NEAR #374

Merged
merged 43 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a851290
feat: add a fetch function inside the runtime host environment
saeed-zil Jun 12, 2024
052f4ed
Add headers
saeed-zil Jun 12, 2024
ad06f97
Add method
saeed-zil Jun 12, 2024
5279d24
Add body
saeed-zil Jun 12, 2024
16b37ae
feat: Add app-side library for NEAR
saeed-zil Jun 13, 2024
a6fbc46
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 18, 2024
7bef965
code review fixes
saeed-zil Jun 18, 2024
a63bc2b
Fix serde deserializer
saeed-zil Jun 18, 2024
854b6fe
Remove unwrap
saeed-zil Jun 18, 2024
22741e2
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 18, 2024
893c6c6
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 18, 2024
3d38742
Add methods
saeed-zil Jun 18, 2024
66d3042
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 19, 2024
96e6c6a
Move AltResult to primitives.
saeed-zil Jun 19, 2024
6bd911d
Make id incremental
saeed-zil Jun 19, 2024
222ae8d
Make view account more restricted.
saeed-zil Jun 19, 2024
792de59
Fix code style
saeed-zil Jun 19, 2024
4ed7b77
Add more query functions
saeed-zil Jun 20, 2024
b13f56f
add function call.
saeed-zil Jun 21, 2024
93d3144
change comment
saeed-zil Jun 21, 2024
9f9cf2d
Update style
saeed-zil Jun 21, 2024
a587f32
fix code style
saeed-zil Jun 21, 2024
c1c0f72
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 24, 2024
034a9eb
update cargo.lock
saeed-zil Jun 24, 2024
cd4b7d2
Code review fixes
saeed-zil Jun 24, 2024
c16ac26
Add error type for rpc calls.
saeed-zil Jun 24, 2024
3901d33
Change the design
saeed-zil Jun 24, 2024
17c0563
minor changes
saeed-zil Jun 24, 2024
4455f4b
unwanted change
saeed-zil Jun 24, 2024
364fc0d
remove useless code
saeed-zil Jun 24, 2024
6202fe1
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 24, 2024
38d146f
tiny fixes
saeed-zil Jun 24, 2024
93070fd
rename error.
saeed-zil Jun 24, 2024
961cf9f
Merge client and jsonrpcclient
saeed-zil Jun 24, 2024
8ffddab
make id a number.
saeed-zil Jun 24, 2024
aa2cfa2
fix error
saeed-zil Jun 24, 2024
424555e
Merge branch 'master' of github.com:calimero-network/core into 370-ad…
saeed-zil Jun 26, 2024
1b4474e
Add more error types.
saeed-zil Jun 26, 2024
104cb64
Merge branch 'master' into 370-add-near-app-side-library
its-saeed Jun 26, 2024
fd718f7
move code to better mods
saeed-zil Jun 26, 2024
5559c99
Update crates/runtime/src/logic.rs
its-saeed Jun 26, 2024
885a36f
Add an example for near library
saeed-zil Jun 26, 2024
3722fd6
Merge branch '370-add-near-app-side-library' of github.com:calimero-n…
saeed-zil Jun 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 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.

3 changes: 1 addition & 2 deletions crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ owo-colors = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
ureq = "2.9.7"
ureq.workspace = true
wasmer.workspace = true
wasmer-types.workspace = true
borsh = { workspace = true, features = ["derive"] }

[[example]]
name = "demo"
Expand Down
43 changes: 20 additions & 23 deletions crates/runtime/src/logic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::num::NonZeroU64;

use ouroboros::self_referencing;
Expand Down Expand Up @@ -309,20 +308,20 @@ impl<'a> VMHostFunctions<'a> {

pub fn fetch(
&mut self,
method_ptr: u64,
method_len: u64,
url_ptr: u64,
url_len: u64,
method_ptr: u64,
method_len: u64,
headers_ptr: u64,
headers_len: u64,
body_ptr: u64,
body_len: u64,
out_register_id: u64,
) -> Result<()> {
let method = self.get_string(method_ptr, method_len)?;
register_id: u64,
) -> Result<u32> {
let url = self.get_string(url_ptr, url_len)?;
let method = self.get_string(method_ptr, method_len)?;
let headers = self.read_guest_memory(headers_ptr, headers_len)?;
let headers: HashMap<String, String> = borsh::from_slice(&headers).unwrap();
let headers: Vec<(String, String)> = borsh::from_slice(&headers).unwrap(); // safety: headers are coming from an inner source. Safe to deserialize.
its-saeed marked this conversation as resolved.
Show resolved Hide resolved
let body = self.read_guest_memory(body_ptr, body_len)?;
let mut request = ureq::request(&method, &url);

Expand All @@ -334,22 +333,20 @@ impl<'a> VMHostFunctions<'a> {
request.send_bytes(&body)
} else {
request.call()
}
.map_err(|e| HostError::FetchError {
url: url.clone(),
error: e.to_string(),
})?
.into_string()
.map_err(|e| HostError::FetchError {
url,
error: e.to_string(),
})?;
};

self.with_logic_mut(|logic| {
logic
.registers
.set(&logic.limits, out_register_id, response.into_bytes())
})?;
Ok(())
let (status, data) = match response {
its-saeed marked this conversation as resolved.
Show resolved Hide resolved
Ok(response) => {
let mut buffer = vec![];
match response.into_reader().read_to_end(&mut buffer) {
Ok(_) => (0, buffer),
Err(_) => (1, "Failed to read the response body.".into()),
}
}
Err(e) => (1, e.to_string().into_bytes()),
its-saeed marked this conversation as resolved.
Show resolved Hide resolved
};

self.with_logic_mut(|logic| logic.registers.set(&logic.limits, register_id, data))?;
Ok(status)
}
}
14 changes: 12 additions & 2 deletions crates/runtime/src/logic/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ impl<'a> VMLogic<'a> {
register_id: u64,
) -> u32;
fn storage_read(key_ptr: u64, key_len: u64, register_id: u64) -> u32;
fn fetch(method_ptr: u64, method_len: u64, url_ptr: u64, url_len: u64, headers_ptr: u64,
headers_len: u64, body_ptr: u64, body_len: u64, out_register_id: u64);

fn fetch(
url_ptr: u64,
url_len: u64,
method_ptr: u64,
method_len: u64,
headers_ptr: u64,
headers_len: u64,
body_ptr: u64,
body_len: u64,
register_id: u64
) -> u32;
}
}
}
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 = "../../" }
its-saeed marked this conversation as resolved.
Show resolved Hide resolved

its-saeed marked this conversation as resolved.
Show resolved Hide resolved
67 changes: 67 additions & 0 deletions crates/sdk/libs/near/src/jsonrpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use calimero_sdk::env;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

#[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),
}
}
}
its-saeed marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) struct Client {
its-saeed marked this conversation as resolved.
Show resolved Hide resolved
url: String,
}

impl Client {
pub fn new(url: String) -> Self {
fbozic marked this conversation as resolved.
Show resolved Hide resolved
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",
its-saeed marked this conversation as resolved.
Show resolved Hide resolved
"method": method,
"params": params,
}))
.map_err(|err| format!("Cannot serialize request: {:?}", err))?;
its-saeed marked this conversation as resolved.
Show resolved Hide resolved

let response = unsafe { env::ext::fetch(&self.url, "POST", &headers, &body) }?;
let response = String::from_utf8(response).map_err(|e| e.to_string())?;
serde_json::from_str::<Response<T, E>>(&response).map_err(|e| e.to_string())
its-saeed marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

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

#[derive(Debug, Clone, Deserialize)]
pub struct RpcError<E> {
pub code: i32,
pub message: String,
pub data: Option<E>,
}
Loading