generated from gear-foundation/dapp-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alpha): add alpha program (#14)
* feat(alpha): alpha program * fix(alpha): build problems * feat(prog): state queries
- Loading branch information
Showing
12 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "vara-go-alpha" | ||
version.workspace = true | ||
edition.workspace = true | ||
publish.workspace = true | ||
|
||
[dependencies] | ||
gstd.workspace = true | ||
alpha-io.workspace = true | ||
|
||
[dev-dependencies] | ||
gtest.workspace = true | ||
|
||
[build-dependencies] | ||
alpha-io.workspace = true | ||
gear-wasm-builder.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use alpha_io::ContractMetadata; | ||
|
||
fn main() { | ||
gear_wasm_builder::build_with_metadata::<ContractMetadata>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "alpha-io" | ||
version.workspace = true | ||
edition.workspace = true | ||
publish.workspace = true | ||
|
||
[dependencies] | ||
gmeta.workspace = true | ||
gstd.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#![no_std] | ||
|
||
use gmeta::{In, Metadata, Out}; | ||
use gstd::*; | ||
|
||
/// The contract metadata. Used by frontend apps & for describing the types of messages that can be | ||
/// sent in contract's entry points. See also [`Metadata`]. | ||
pub struct ContractMetadata; | ||
|
||
pub type State = Vec<Resource>; | ||
|
||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub enum Label { | ||
Apple, | ||
Banana, | ||
Football, | ||
Vara, | ||
} | ||
|
||
impl AsRef<str> for Label { | ||
fn as_ref(&self) -> &str { | ||
match self { | ||
Label::Apple => "apple", | ||
Label::Banana => "banana", | ||
Label::Football => "football", | ||
Label::Vara => "vara", | ||
} | ||
} | ||
} | ||
|
||
impl Label { | ||
pub fn list() -> Vec<Label> { | ||
vec![Label::Apple, Label::Banana, Label::Football, Label::Vara] | ||
} | ||
} | ||
|
||
#[derive(Encode, Decode, TypeInfo, PartialEq, Eq, Debug, Clone)] | ||
#[codec(crate = gstd::codec)] | ||
#[scale_info(crate = gstd::scale_info)] | ||
pub struct Resource { | ||
pub labels: Vec<Label>, | ||
pub domain: String, | ||
pub description: String, | ||
pub html: String, | ||
pub styles: String, | ||
} | ||
|
||
/// `()` means the contract doesn't process & reply messages at the above written entry point or | ||
/// doesn't implement it. | ||
impl Metadata for ContractMetadata { | ||
/// I/O types for the `init()` entry point. | ||
type Init = In<()>; | ||
/// I/O types for the `handle()` entry point. | ||
type Handle = In<Resource>; | ||
/// Types for miscellaneous scenarios. | ||
type Others = (); | ||
/// The input type for the `handle_reply()` entry point. | ||
type Reply = (); | ||
/// The output type for the `handle_signal()` entry point. | ||
type Signal = (); | ||
/// I/O types for the `state()` entry point. | ||
/// | ||
/// You can also specify just an output ([`Out`]) or input ([`In`]) type, if both | ||
/// ([`In`]) are expected like here. | ||
type State = Out<State>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![no_std] | ||
|
||
use alpha_io::{Resource, State}; | ||
use gstd::{msg, prelude::*}; | ||
|
||
/// The resource of the domain. | ||
static mut STATE: Option<State> = None; | ||
|
||
// do nothing atm. | ||
#[no_mangle] | ||
extern fn init() { | ||
unsafe { | ||
STATE = Some(vec![]); | ||
} | ||
} | ||
|
||
// The `handle()` entry point. | ||
#[no_mangle] | ||
extern fn handle() { | ||
let res = msg::load::<Resource>().expect("Failed to load handle input"); | ||
let state = unsafe { STATE.as_mut().expect("State isn't initialized") }; | ||
state.push(res); | ||
} | ||
|
||
// The `state()` entry point. | ||
#[no_mangle] | ||
extern fn state() { | ||
let state = unsafe { STATE.take().expect("State isn't initialized") }; | ||
msg::reply(state, 0).expect("Failed to reply from `state()`"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "alpha-state" | ||
version.workspace = true | ||
edition.workspace = true | ||
publish.workspace = true | ||
|
||
[dependencies] | ||
gstd.workspace = true | ||
gmeta = { workspace = true, features = ["codegen"] } | ||
alpha-io.workspace = true | ||
|
||
[build-dependencies] | ||
gear-wasm-builder = { workspace = true, features = ["metawasm"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
gear_wasm_builder::build_metawasm(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![no_std] | ||
|
||
use alpha_io::{Label, Resource}; | ||
use gstd::prelude::*; | ||
|
||
#[gmeta::metawasm] | ||
pub mod metafns { | ||
pub type State = alpha_io::State; | ||
|
||
/// Returns all domains (pages) that matches the search input. | ||
pub fn labels(_state: State) -> Vec<Label> { | ||
Label::list() | ||
} | ||
|
||
/// Search all resouces matching the input label. | ||
pub fn search(state: State, label: Label) -> Vec<Resource> { | ||
state | ||
.into_iter() | ||
.filter(|s| s.labels.contains(&label)) | ||
.collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use alpha_io::{Label, Resource}; | ||
use gtest::{state_args, Program, System}; | ||
use std::{fs, path::PathBuf}; | ||
|
||
fn get_state_binary() -> Vec<u8> { | ||
let bin = PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
.join("../target/wasm32-unknown-unknown/debug/alpha_state.meta.wasm"); | ||
fs::read(bin).unwrap() | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
let system = System::new(); | ||
system.init_logger(); | ||
|
||
let program = Program::current(&system); | ||
let mut result = program.send_bytes(2, []); | ||
assert!(!result.main_failed()); | ||
|
||
let input = Resource { | ||
labels: Label::list(), | ||
domain: "vara-go".into(), | ||
description: "test test".into(), | ||
html: "<div>hello, world<div>".into(), | ||
styles: "".into(), | ||
}; | ||
result = program.send(2, input); | ||
assert!(!result.main_failed()); | ||
|
||
let state_bin = get_state_binary(); | ||
let search = Label::Vara; | ||
let output: Vec<Resource> = program | ||
.read_state_using_wasm(b"", "search", state_bin, state_args!(search)) | ||
.expect("Failed to search source"); | ||
// assert_eq!(output.get(&input.domain), Some(input.src).as_ref()); | ||
println!("{output:?}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters