-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
executable file
·76 lines (66 loc) · 2.45 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract(version = "0.1.0")]
mod xpawnshop {
use ink_core::storage;
/// Defines the storage of your contract.
/// Add new fields to the below struct in order
/// to add new static storage fields to your contract.
#[ink(storage)]
struct Xpawnshop {
/// Stores a single `bool` value on the storage.
value: storage::Value<bool>,
}
impl Xpawnshop {
/// Constructor that initializes the `bool` value to the given `init_value`.
#[ink(constructor)]
fn new(&mut self, init_value: bool) {
self.value.set(init_value);
}
/// Constructor that initializes the `bool` value to `false`.
///
/// Constructors can delegate to other constructors.
#[ink(constructor)]
fn default(&mut self) {
self.new(false)
}
/// A message that can be called on instantiated contracts.
/// This one flips the value of the stored `bool` from `true`
/// to `false` and vice versa.
#[ink(message)]
fn flip(&mut self) {
*self.value = !self.get();
}
/// Simply returns the current value of our `bool`.
#[ink(message)]
fn get(&self) -> bool {
*self.value
}
}
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
/// module and test functions are marked with a `#[test]` attribute.
/// The below code is technically just normal Rust code.
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
/// We test if the default constructor does its job.
#[test]
fn default_works() {
// Note that even though we defined our `#[ink(constructor)]`
// above as `&mut self` functions that return nothing we can call
// them in test code as if they were normal Rust constructors
// that take no `self` argument but return `Self`.
let xpawnshop = Xpawnshop::default();
assert_eq!(xpawnshop.get(), false);
}
/// We test a simple use case of our contract.
#[test]
fn it_works() {
let mut xpawnshop = Xpawnshop::new(false);
assert_eq!(xpawnshop.get(), false);
xpawnshop.flip();
assert_eq!(xpawnshop.get(), true);
}
}
}