From 3ee63d34458db0942d3cc817cb622e8c7a58d731 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 21 Sep 2021 12:21:34 +0200 Subject: [PATCH 1/2] add trait-incrementer example smart contract --- examples/trait-incrementer/.gitignore | 9 +++ examples/trait-incrementer/Cargo.toml | 35 +++++++++ examples/trait-incrementer/lib.rs | 108 ++++++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 examples/trait-incrementer/.gitignore create mode 100644 examples/trait-incrementer/Cargo.toml create mode 100644 examples/trait-incrementer/lib.rs diff --git a/examples/trait-incrementer/.gitignore b/examples/trait-incrementer/.gitignore new file mode 100644 index 00000000000..bf910de10af --- /dev/null +++ b/examples/trait-incrementer/.gitignore @@ -0,0 +1,9 @@ +# 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 diff --git a/examples/trait-incrementer/Cargo.toml b/examples/trait-incrementer/Cargo.toml new file mode 100644 index 00000000000..9f4bab41040 --- /dev/null +++ b/examples/trait-incrementer/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "trait-incrementer" +version = "3.0.0-rc3" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +ink_primitives = { version = "3.0.0-rc5", path = "../../crates/primitives", default-features = false } +ink_metadata = { version = "3.0.0-rc5", path = "../../crates/metadata", default-features = false, features = ["derive"], optional = true } +ink_env = { version = "3.0.0-rc5", path = "../../crates/env", default-features = false } +ink_storage = { version = "3.0.0-rc5", path = "../../crates/storage", default-features = false } +ink_lang = { version = "3.0.0-rc5", path = "../../crates/lang", default-features = false } + +scale = { package = "parity-scale-codec", version = "2", default-features = false, features = ["derive"] } +scale-info = { version = "0.6", default-features = false, features = ["derive"], optional = true } + +[lib] +name = "trait_incrementer" +path = "lib.rs" +crate-type = ["cdylib"] + +[features] +default = ["std"] +std = [ + "ink_primitives/std", + "ink_metadata", + "ink_metadata/std", + "ink_env/std", + "ink_storage/std", + "ink_lang/std", + "scale/std", + "scale-info", + "scale-info/std", +] +ink-as-dependency = [] diff --git a/examples/trait-incrementer/lib.rs b/examples/trait-incrementer/lib.rs new file mode 100644 index 00000000000..18606055626 --- /dev/null +++ b/examples/trait-incrementer/lib.rs @@ -0,0 +1,108 @@ +// Copyright 2018-2021 Parity Technologies (UK) Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![cfg_attr(not(feature = "std"), no_std)] +#![allow(clippy::new_without_default)] + +use ink_lang as ink; + +/// Allows to increment and get the current value. +#[ink::trait_definition] +pub trait Increment { + /// Increments the current value of the implementer by 1. + #[ink(message)] + fn inc(&mut self); + + /// Returns the current value of the implementer. + #[ink(message)] + fn get(&self) -> u64; +} + +/// Allows to reset the current value. +#[ink::trait_definition] +pub trait Reset { + /// Increments the current value of the implementer by 1. + #[ink(message)] + fn reset(&mut self); +} + +#[ink::contract] +pub mod incrementer { + use super::{ + Increment, + Reset, + }; + + /// A concrete incrementer smart contract. + #[ink(storage)] + pub struct Incrementer { + value: u64, + } + + impl Incrementer { + /// Creates a new incrementer smart contract initialized with `0`. + #[ink(constructor)] + pub fn new() -> Self { + Self { + value: Default::default(), + } + } + + /// Increases the value of the incrementer by the given delta. + #[ink(message)] + pub fn inc_by(&mut self, delta: u64) { + self.value += delta; + } + } + + impl Increment for Incrementer { + #[ink(message)] + fn inc(&mut self) { + self.inc_by(1) + } + + #[ink(message)] + fn get(&self) -> u64 { + self.value + } + } + + impl Reset for Incrementer { + #[ink(message)] + fn reset(&mut self) { + self.value = 0; + } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + fn default_works() { + let incrementer = Incrementer::new(); + assert_eq!(incrementer.get(), 0); + } + + #[test] + fn it_works() { + let mut incrementer = Incrementer::new(); + // Can call using universal call syntax using the trait. + assert_eq!(::get(&incrementer), 0); + ::inc(&mut incrementer); + // Normal call syntax possible to as long as the trait is in scope. + assert_eq!(incrementer.get(), 1); + } + } +} From 887297716baa731ef6535cdbe115b63095eb4106 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Tue, 21 Sep 2021 17:18:58 +0200 Subject: [PATCH 2/2] apply review comments --- examples/trait-incrementer/Cargo.toml | 2 +- examples/trait-incrementer/lib.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/trait-incrementer/Cargo.toml b/examples/trait-incrementer/Cargo.toml index 9f4bab41040..45a22af4155 100644 --- a/examples/trait-incrementer/Cargo.toml +++ b/examples/trait-incrementer/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trait-incrementer" -version = "3.0.0-rc3" +version = "3.0.0-rc5" authors = ["Parity Technologies "] edition = "2018" diff --git a/examples/trait-incrementer/lib.rs b/examples/trait-incrementer/lib.rs index 18606055626..da1ec86357a 100644 --- a/examples/trait-incrementer/lib.rs +++ b/examples/trait-incrementer/lib.rs @@ -20,7 +20,7 @@ use ink_lang as ink; /// Allows to increment and get the current value. #[ink::trait_definition] pub trait Increment { - /// Increments the current value of the implementer by 1. + /// Increments the current value of the implementer by one (1). #[ink(message)] fn inc(&mut self); @@ -32,7 +32,7 @@ pub trait Increment { /// Allows to reset the current value. #[ink::trait_definition] pub trait Reset { - /// Increments the current value of the implementer by 1. + /// Resets the current value to zero. #[ink(message)] fn reset(&mut self); } @@ -51,7 +51,7 @@ pub mod incrementer { } impl Incrementer { - /// Creates a new incrementer smart contract initialized with `0`. + /// Creates a new incrementer smart contract initialized with zero. #[ink(constructor)] pub fn new() -> Self { Self { @@ -59,7 +59,7 @@ pub mod incrementer { } } - /// Increases the value of the incrementer by the given delta. + /// Increases the value of the incrementer by an amount. #[ink(message)] pub fn inc_by(&mut self, delta: u64) { self.value += delta;