Commit ca0f548a authored by Shawn Tabrizi's avatar Shawn Tabrizi Committed by Hero Bird

[examples] Add flipper contract to examples folder (#65)

* [examples] Add flipper contract to examples folder

* [examples] Simplify flipping logic

* [examples] Fix flipper test

* [examples] Update Flipper and Incrementer examples

* [examples] cargo fmt on Flipper
parent 6b1ffcf3
[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 = "flipper"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
ink_core = { path = "../../../core" }
ink_model = { path = "../../../model" }
ink_lang = { path = "../../../lang" }
parity-codec = { version = "3.3", default-features = false, features = ["derive"] }
[lib]
name = "flipper"
crate-type = ["cdylib"]
[features]
default = []
test-env = [
"ink_core/test-env",
"ink_model/test-env",
"ink_lang/test-env",
]
generate-api-description = [
"ink_lang/generate-api-description"
]
[profile.release]
panic = "abort"
lto = true
opt-level = "z"
\ No newline at end of file
#!/bin/bash
PROJNAME=flipper
# cargo clean
# rm Cargo.lock
CARGO_INCREMENTAL=0 cargo build --release --features generate-api-description --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
#![no_std]
use ink_core::{
env::println,
memory::format,
storage,
};
use ink_lang::contract;
contract! {
/// This simple dummy contract has a `bool` value that can
/// alter between `true` and `false` using the `flip` message.
/// Users can retrieve its current state using the `get` message.
struct Flipper {
/// The current state of our flag.
value: storage::Value<bool>,
}
impl Deploy for Flipper {
/// Initializes our state to `false` upon deploying our smart contract.
fn deploy(&mut self) {
self.value.set(false)
}
}
impl Flipper {
/// Flips the current state of our smart contract.
pub(external) fn flip(&mut self) {
*self.value = !*self.value;
}
/// Returns the current state.
pub(external) fn get(&self) -> bool {
println(&format!("Flipper Value: {:?}", *self.value));
*self.value
}
}
}
#[cfg(test)]
mod tests {
use super::Flipper;
#[test]
fn it_works() {
let mut flipper = Flipper::deploy_mock();
assert_eq!(flipper.get(), false);
flipper.flip();
assert_eq!(flipper.get(), true);
}
}
[package]
name = "incrementer"
version = "0.1.0"
authors = ["[your_name] <[your_email]>"]
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018"
[dependencies]
ink_core = { path = "../../../core" }
ink_model = { path = "../../../model" }
ink_lang = { path = "../../../lang" }
# ink_core = { git = "https://github.com/Robbepop/pdsl", package = "ink_core" }
# ink_model = { git = "https://github.com/Robbepop/pdsl", package = "ink_model" }
# ink_lang = { git = "https://github.com/Robbepop/pdsl", package = "ink_lang" }
parity-codec = { version = "3.2", default-features = false, features = ["derive"] }
[lib]
......
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