From 7c056e92fa26a84b4f11b0e320db1386bb96a998 Mon Sep 17 00:00:00 2001 From: "matej.vukosav" Date: Mon, 4 Nov 2024 16:54:31 +0800 Subject: [PATCH] chore: move demo app to core as temp solution --- Cargo.lock | 8 ++++ Cargo.toml | 1 + apps/blockchain/Cargo.toml | 21 ++++++++++ apps/blockchain/build.sh | 21 ++++++++++ apps/blockchain/src/lib.rs | 79 ++++++++++++++++++++++++++++++++++++++ crates/sdk/src/env.rs | 4 ++ 6 files changed, 134 insertions(+) create mode 100644 apps/blockchain/Cargo.toml create mode 100755 apps/blockchain/build.sh create mode 100644 apps/blockchain/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9dd3d106d..e411d8398 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -932,6 +932,14 @@ dependencies = [ "generic-array 0.14.7", ] +[[package]] +name = "blockchain" +version = "0.1.0" +dependencies = [ + "calimero-sdk", + "calimero-storage", +] + [[package]] name = "blocking" version = "1.6.1" diff --git a/Cargo.toml b/Cargo.toml index 817da3387..075b482c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ members = [ "./apps/kv-store", "./apps/only-peers", "./apps/gen-ext", + "./apps/blockchain", "./contracts/context-config", "./contracts/registry", diff --git a/apps/blockchain/Cargo.toml b/apps/blockchain/Cargo.toml new file mode 100644 index 000000000..6022f2ae4 --- /dev/null +++ b/apps/blockchain/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "blockchain" +description = "Calimero increment/decrement application" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +calimero-sdk = { path = "../../crates/sdk" } +calimero-storage = { path = "../../crates/storage" } + +[profile.app-release] +inherits = "release" +codegen-units = 1 +opt-level = "z" +lto = true +debug = false +panic = "abort" +overflow-checks = true diff --git a/apps/blockchain/build.sh b/apps/blockchain/build.sh new file mode 100755 index 000000000..1ee9eb651 --- /dev/null +++ b/apps/blockchain/build.sh @@ -0,0 +1,21 @@ +#!/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 + +name=$(cargo read-manifest | jq -r '.name') +sanitized_name=$(echo $name | tr '-' '_') + +cp "$TARGET/wasm32-unknown-unknown/app-release/$sanitized_name.wasm" ./res/ + +if command -v wasm-opt >/dev/null; then + wasm-opt -Oz ./res/$sanitized_name.wasm -o ./res/$sanitized_name.wasm +fi diff --git a/apps/blockchain/src/lib.rs b/apps/blockchain/src/lib.rs new file mode 100644 index 000000000..b9e6065e3 --- /dev/null +++ b/apps/blockchain/src/lib.rs @@ -0,0 +1,79 @@ +use calimero_sdk::app; +use calimero_sdk::borsh::{BorshDeserialize, BorshSerialize}; +use calimero_sdk::env::{self}; +use calimero_sdk::serde::{Deserialize, Serialize}; +use calimero_storage::entities::Element; +use calimero_storage::AtomicUnit; + +#[derive(Default, BorshDeserialize, BorshSerialize, Serialize, Deserialize)] +#[borsh(crate = "calimero_sdk::borsh")] +#[serde(crate = "calimero_sdk::serde")] +pub struct CreateProposalRequest { + proposal_id: String, + author: Option, + // status: ProposalStatus, +} + +#[app::event] +pub enum Event { + ProposalCreated(), +} + +#[app::state(emits = Event)] +#[derive(AtomicUnit, Clone, Debug, PartialEq, PartialOrd)] +#[root] +#[type_id(1)] +pub struct AppState { + count: u32, + #[storage] + storage: Element, +} + +#[app::logic] +impl AppState { + #[app::init] + pub fn init() -> AppState { + AppState { + count: 0, + storage: Element::root(), + } + } + + pub fn create_new_proposal(&mut self, _request: CreateProposalRequest) -> bool { + // let proposal_id = Blockchain::create_proposal("transfer", "xabi.near", 999999); + // let enhanced_proposal = Proposal { + // proposal_id, + // title, + // description + // }; + // storage.save(enhanced_proposal) + + //?? + + DraftProposal::new() + .transfer(AccountId("xabi.near".to_string()), 999999) + .send(); + + true + } + + pub fn approve_proposal(&mut self, proposal_id: String) -> bool { + // let proposal = storage.get_proposal(proposal_id); + // let vote = Vote { + // proposal_id, + // voter_id: "xabi.near", + // vote_type: VoteType::Accept(), + // voted_at: 1234567890, + // }; + // storage.save(vote) + true + } + + // Messages (discussion) + // pub fn get_proposal_messages(proposal_id: String) -> Vec { + // vec![] + // } + // pub fn send_message(proposal_id: String, message: Message) -> bool { + // true + // } +} diff --git a/crates/sdk/src/env.rs b/crates/sdk/src/env.rs index 6951de846..e62968cc3 100644 --- a/crates/sdk/src/env.rs +++ b/crates/sdk/src/env.rs @@ -192,6 +192,10 @@ pub fn random_bytes(buf: &mut [u8]) { unsafe { sys::random_bytes(BufferMut::new(buf)) } } +#[inline] +pub fn send_proposal(value: &[u8], buf: &mut [u8]) { + unsafe { sys::send_proposal(Buffer::new(value), BufferMut::new(buf)) } +} /// Gets the current time. #[inline] #[must_use]