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 all 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
48 changes: 31 additions & 17 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ members = [
"./crates/primitives",
"./crates/runtime",
"./crates/sdk",
"./crates/sdk/libs/near",
"./crates/sdk/macros",
"./crates/server",
"./crates/server-primitives",
"./crates/store",

"./apps/kv-store",
"./apps/only-peers",
"./apps/gen-ext",

"./contracts/package-manager",
]
Expand All @@ -46,8 +48,10 @@ futures-util = "0.3.30"
hex = "0.4.3"
libp2p = "0.53.2"
libp2p-stream = "0.1.0-alpha.1"
libp2p-identity = "0.2.9"
multiaddr = "0.18.1"
# multibase = "0.9.1"
near-account-id = "1.0.0"
near-jsonrpc-client = "0.8.0"
near-jsonrpc-primitives = "0.20.0"
near-primitives = "0.20.0"
Expand All @@ -67,6 +71,7 @@ sha3 = "0.10.8"
semver = "1.0.22"
serde = "1.0.196"
serde_json = "1.0.113"
serde_with = "3.8.1"
syn = "2.0"
thiserror = "1.0.56"
tokio = "1.35.1"
Expand Down
14 changes: 14 additions & 0 deletions apps/gen-ext/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
authors.workspace = true
edition.workspace = true
license.workspace = true
name = "gen-ext"
repository.workspace = true
version = "0.1.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
calimero-sdk = {path = "../../crates/sdk"}
calimero-sdk-near = { path = "../../crates/sdk/libs/near" }
18 changes: 18 additions & 0 deletions apps/gen-ext/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
set -e

cd "$(dirname $0)"

TARGET="${CARGO_TARGET_DIR:-../../target}"

rustup target add wasm32-unknown-unknown

cargo build --target wasm32-unknown-unknown --profile app-release

mkdir -p res

cp $TARGET/wasm32-unknown-unknown/app-release/gen_ext.wasm ./res/

if command -v wasm-opt > /dev/null; then
wasm-opt -Oz ./res/gen_ext.wasm -o ./res/gen_ext.wasm
fi
27 changes: 27 additions & 0 deletions apps/gen-ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use calimero_sdk::app;
use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize};
use calimero_sdk_near::query::RpcQueryRequest;
use calimero_sdk_near::views::QueryRequest;
use calimero_sdk_near::Client;

#[app::state]
#[derive(Default, BorshSerialize, BorshDeserialize)]
#[borsh(crate = "calimero_sdk::borsh")]
struct GenExt;

#[app::logic]
impl GenExt {
pub fn view_account(&mut self, account_id: &str, block_height: u64) -> String {
let client = Client::testnet();
let request = RpcQueryRequest {
block_id: calimero_sdk_near::BlockId::Height(block_height),
request: QueryRequest::ViewAccount {
account_id: account_id.parse().unwrap(),
},
};
match client.call(request) {
Ok(r) => format!("{:?}", r),
Err(e) => format!("{:?}", e),
}
}
}
2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license.workspace = true
[dependencies]
bs58.workspace = true
ed25519-dalek.workspace = true
libp2p = { workspace = true, features = ["serde"] }
libp2p-identity = { workspace = true, features = ["peerid", "serde"] }
semver = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
Expand Down
20 changes: 20 additions & 0 deletions crates/primitives/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
use serde::{Deserialize, Serialize};

pub const fn bool_true() -> bool {
true
}

#[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),
}
}
}
2 changes: 1 addition & 1 deletion crates/primitives/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct ExecutedTransactionPayload {
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct PeerJoinedPayload {
pub peer_id: libp2p::PeerId,
pub peer_id: libp2p_identity::PeerId,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub mod serde_signing_key {
pub mod serde_identity {
use std::fmt;

use libp2p::identity::Keypair;
use libp2p_identity::Keypair;
use serde::de::{self, MapAccess};
use serde::ser::{self, SerializeMap};
use serde::{Deserializer, Serializer};
Expand Down
9 changes: 6 additions & 3 deletions crates/runtime/examples/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use calimero_runtime::{logic, run, store, Constraint};
use serde_json::json;

fn main() -> eyre::Result<()> {
let file = include_bytes!("../../../apps/only-peers/res/only_peers.wasm");
let file = include_bytes!("../../../apps/gen-ext/res/gen_ext.wasm");

let mut storage = store::InMemoryStorage::default();

Expand All @@ -22,9 +22,12 @@ fn main() -> eyre::Result<()> {
};

let cx = logic::VMContext {
input: serde_json::to_vec(&json!({}))?,
input: serde_json::to_vec(&json!({
"block_height": 167345193,
"account_id": "nearkat.testnet",
}))?,
};
let get_outcome = run(file, "fetch", cx, &mut storage, &limits)?;
let get_outcome = run(file, "view_account", cx, &mut storage, &limits)?;
let returns = String::from_utf8(get_outcome.returns.unwrap().unwrap()).unwrap();
println!("{returns}");

Expand Down
6 changes: 5 additions & 1 deletion crates/runtime/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,11 @@ impl<'a> VMHostFunctions<'a> {
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: Vec<(String, String)> = borsh::from_slice(&headers).unwrap(); // safety: headers are coming from an inner source. Safe to deserialize.

// Safety: The `fetch` function cannot be directly called by applications.
// Therefore, the headers are generated exclusively by our code, ensuring
// that it is safe to deserialize them.
let headers: Vec<(String, String)> = borsh::from_slice(&headers).unwrap();
let body = self.read_guest_memory(body_ptr, body_len)?;
let mut request = ureq::request(&method, &url);

Expand Down
17 changes: 17 additions & 0 deletions crates/sdk/libs/near/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "calimero-sdk-near"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
repository.workspace = true
license.workspace = true

[dependencies]
near-account-id = { workspace = true, features = ["serde"]}
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" }
39 changes: 39 additions & 0 deletions crates/sdk/libs/near/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use serde_json::Value;
use thiserror::Error;

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

#[error("Failed to fetch: {0}")]
FetchError(String),

#[error("Server error: {0}")]
ServerError(RpcError<R>),
}

#[derive(Debug, serde::Deserialize, Clone, PartialEq)]
#[serde(tag = "name", content = "cause", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RpcErrorKind<R> {
RequestValidationError(RpcRequestValidationErrorKind),
HandlerError(R),
InternalError(Value),
}

#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RpcRequestValidationErrorKind {
MethodNotFound { method_name: String },
ParseError { error_message: String },
}

#[derive(Debug, serde::Deserialize, Clone, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct RpcError<T> {
#[serde(flatten)]
pub error_struct: Option<RpcErrorKind<T>>,
pub code: i64,
pub message: String,
pub data: Option<Value>,
}
Loading