Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trait-incrementer example smart contract #932

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/trait-incrementer/.gitignore
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions examples/trait-incrementer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[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 = []
108 changes: 108 additions & 0 deletions examples/trait-incrementer/lib.rs
Original file line number Diff line number Diff line change
@@ -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 one (1).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My nitpick here was that it's up to the trait implementer to choose the amount inc increases by 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not agree because this would violate the invariant stated by the docs of the trait's message.

Copy link
Collaborator Author

@Robbepop Robbepop Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just because our concrete incrementer does _also_support inc_by does not mean this invariant must not be uphold for the trait implementation in particular. You could say the demonstrated concrete incrementer smart contract provides a richer API than what the ink! trait mandates but that's always fine, also in typical Rust contexts.

#[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)]
Robbepop marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}