Unverified Commit 7577c678 authored by Robin Freyler's avatar Robin Freyler Committed by GitHub
Browse files

Add trait-incrementer example smart contract (#932)

* add trait-incrementer example smart contract

* apply review comments
parent 0270add0
Pipeline #158439 passed with stages
in 25 minutes and 32 seconds
# 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 = "trait-incrementer"
version = "3.0.0-rc5"
authors = ["Parity Technologies <admin@parity.io>"]
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 = []
// 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 one (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 {
/// Resets the current value to zero.
#[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 zero.
#[ink(constructor)]
pub fn new() -> Self {
Self {
value: Default::default(),
}
}
/// Increases the value of the incrementer by an amount.
#[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!(<Incrementer as Increment>::get(&incrementer), 0);
<Incrementer as Increment>::inc(&mut incrementer);
// Normal call syntax possible to as long as the trait is in scope.
assert_eq!(incrementer.get(), 1);
}
}
}
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