Skip to content

Commit

Permalink
feat: introduce warp-contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiszczatowski committed Mar 15, 2023
1 parent 403a8f3 commit 8b2fcdd
Show file tree
Hide file tree
Showing 21 changed files with 1,052 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dependency directories

node_modules/
crates/warp-contracts/target

# Optional eslint cache
.eslintcache
Expand Down
214 changes: 214 additions & 0 deletions crates/warp-contracts/Cargo.lock

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

34 changes: 34 additions & 0 deletions crates/warp-contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "warp-contracts"
version = "0.1.1"
edition = "2021"
description = "Warp WASM contract utils for rust contracts"
license = "MIT"
documentation = "https://academy.warp.cc/docs/sdk/advanced/wasm"
homepage = "https://warp.cc"
repository = "https://github.com/warp-contracts/warp"
keywords = ["warp", "smart-contract", "SmartWeave", "web3"]
categories = ["api-bindings", "development-tools::ffi", "finance", "wasm"]

[dependencies]
wasm-bindgen = { workspace = true }
wasm-bindgen-futures = { workspace = true }
js-sys = { workspace = true }
serde = { workspace = true }
serde-wasm-bindgen = { workspace = true }
warp-contracts-macro = { version = "0.1.1", path = "warp-contracts-macro" }
warp-contracts-core = { version = "0.1.1", path = "warp-contracts-core" }

[features]
debug = ["warp-contracts-core/debug"]

[workspace]
members = ["warp-contracts-macro", "warp-contracts-core"]

[workspace.dependencies]
wasm-bindgen = "=0.2.84"
wasm-bindgen-futures = { version = "=0.4.34" }
js-sys = "=0.3.61"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "=0.5.0"
web-sys = { version = "=0.3.61" }
23 changes: 23 additions & 0 deletions crates/warp-contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Warp contracts

`warp-contracts` is an inherent part of [Warp SDK](https://github.com/warp-contracts/warp). This library allows for smooth integration with Warp implementation of SmartWeave protocol.

| Feature | Yes/No |
| ---------------------- | ----------- |
| Sandboxing ||
| Foreign contract read ||
| Foreign contract view ||
| Foreign contract write ||
| Arweave.utils | Soon |
| Evolve ||
| Logging from contract ||
| Transaction data (1) ||
| Block data (2) ||
| Contract data (3) ||
| Gas metering ||

Legend:
- `Soon` - the technology is already there, we just need to find some time to add the missing features :-)
- `(1)` - access current transaction data (id, owner, etc.)
- `(2)` - access current block data (indep_hash, height, timestamp)
- `(3)` - access current contract data (id, owner)
4 changes: 4 additions & 0 deletions crates/warp-contracts/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# contract_utils module

This is a module with boilerplate code for each SmartWeave RUST contract.
**Please don't modify it unless you 100% know what you are doing!**
65 changes: 65 additions & 0 deletions crates/warp-contracts/src/foreign_call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use super::js_imports::SmartWeave;
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_wasm_bindgen::from_value;
use core::fmt::Debug;
use warp_contracts_core::{
handler_result::{ViewResult, WriteResult},
methods::to_json_value,
warp_result::{transmission::from_json, WarpResult},
};

pub async fn read_foreign_contract_state<T: DeserializeOwned>(
contract_address: &str,
) -> Result<T, String> {
match SmartWeave::read_contract_state(contract_address).await {
Ok(s) => match from_value::<T>(s) {
Ok(v) => Ok(v),
Err(e) => Err(format!("{e:?}")),
},
Err(e) => Err(format!("{e:?}")),
}
}

pub async fn view_foreign_contract_state<
V: DeserializeOwned + Debug,
I: Serialize,
E: DeserializeOwned + Debug,
>(
contract_address: &str,
input: I,
) -> ViewResult<V, E> {
let input = match to_json_value(&input) {
Ok(v) => v,
Err(e) => return ViewResult::RuntimeError(format!("{e:?}")),
};
match SmartWeave::view_contract_state(contract_address, input).await {
Ok(s) => match from_json::<V, E>(s) {
WarpResult::WriteSuccess() => {
ViewResult::RuntimeError("got WriteResponse for view call".to_owned())
}
v => v.into(),
},
Err(e) => ViewResult::RuntimeError(format!("{e:?}")),
}
}

pub async fn write_foreign_contract<I: Serialize, E: DeserializeOwned + Debug>(
contract_address: &str,
input: I,
) -> WriteResult<(), E> {
let input = match to_json_value(&input) {
Ok(v) => v,
Err(e) => return WriteResult::RuntimeError(format!("{e:?}")),
};
let write_result = SmartWeave::write(contract_address, input).await;
match write_result {
Ok(s) => match from_json::<(), E>(s) {
WarpResult::ViewSuccess(_) => {
WriteResult::RuntimeError("got ViewResponse for write call".to_owned())
}
v => v.into(),
},
Err(e) => WriteResult::RuntimeError(format!("{e:?}")),
}
}
Loading

0 comments on commit 8b2fcdd

Please sign in to comment.