-
Notifications
You must be signed in to change notification settings - Fork 447
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). | ||
#[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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😉There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.