[examples/lang] Add incrementer lang example

parent 3e7111b3
[target.wasm32-unknown-unknown]
rustflags = [
"-C", "overflow-checks=on",
"-C", "link-args=-z stack-size=65536 --import-memory"
]
\ No newline at end of file
# Ignore build artifacts from the local tests sub-crate.
/target/
# Ignore backup files creates by cargo fmt.
**/*.rs.bk
# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
\ No newline at end of file
[package]
name = "incrementer"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
edition = "2018"
[dependencies]
pdsl_core = { path = "../../../core" }
pdsl_model = { path = "../../../model" }
pdsl_lang = { path = "../../../lang" }
# pdsl_core = { git = "https://github.com/Robbepop/pdsl", package = "pdsl_core" }
# pdsl_model = { git = "https://github.com/Robbepop/pdsl", package = "pdsl_model" }
# pdsl_lang = { git = "https://github.com/Robbepop/pdsl", package = "pdsl_lang" }
parity-codec = { version = "3.2", default-features = false, features = ["derive"] }
[lib]
name = "incrementer"
crate-type = ["cdylib"]
[features]
default = []
test-env = [
"pdsl_core/test-env",
"pdsl_model/test-env",
"pdsl_lang/test-env",
]
generate-api-description = [
"pdsl_lang/generate-api-description"
]
[profile.release]
panic = "abort"
lto = true
opt-level = "z"
\ No newline at end of file
#!/bin/bash
PROJNAME=incrementer
CARGO_INCREMENTAL=0 &&
cargo +nightly build --release --target=wasm32-unknown-unknown --verbose &&
wasm2wat -o target/$PROJNAME.wat target/wasm32-unknown-unknown/release/$PROJNAME.wasm &&
cat target/$PROJNAME.wat | sed "s/(import \"env\" \"memory\" (memory (;0;) 2))/(import \"env\" \"memory\" (memory (;0;) 2 16))/" > target/$PROJNAME-fixed.wat &&
wat2wasm -o target/$PROJNAME.wasm target/$PROJNAME-fixed.wat &&
wasm-opt -Oz target/$PROJNAME.wasm -o target/$PROJNAME-opt.wasm
wasm-prune --exports call,deploy target/$PROJNAME-opt.wasm target/$PROJNAME-pruned.wasm
\ No newline at end of file
nightly-2019-03-10
\ No newline at end of file
#![no_std]
use pdsl_core::{
storage,
memory::format,
};
use pdsl_lang::contract;
contract! {
/// A simple incrementer contract that can only increment,
/// compare and return its internal value.
struct Incrementer {
/// The current value.
value: storage::Value<u32>,
}
impl Deploy for Incrementer {
/// Initializes the value to the initial value.
fn deploy(&mut self, init_value: u32) {
self.value.set(init_value)
}
}
impl Incrementer {
/// Flips the current state of our smart contract.
pub(external) fn inc(&mut self, by: u32) {
env.println(&format!("Incrementer::inc"));
self.value += by;
}
/// Returns the current state.
pub(external) fn get(&self) -> u32 {
env.println("Incrementer::get");
*self.value
}
/// Returns `true` if the internal value is greater than or equal to the provided value.
pub(external) fn compare(&self, with: u32) -> bool {
env.println("Incrementer::compare");
*self.value >= with
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment