-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,454 additions
and
15 deletions.
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 |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
.idea | ||
*.log | ||
tmp/ | ||
|
||
bin/ | ||
target | ||
.projectile |
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
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,115 @@ | ||
#![cfg_attr(not(feature = "std"), no_std, no_main)] | ||
|
||
#[ink::contract] | ||
pub mod flipper { | ||
#[ink(storage)] | ||
pub struct Flipper { | ||
value: bool, | ||
} | ||
|
||
impl Flipper { | ||
/// Creates a new flipper smart contract initialized with the given value. | ||
#[ink(constructor)] | ||
pub fn new(init_value: bool) -> Self { | ||
Self { value: init_value } | ||
} | ||
|
||
/// Creates a new flipper smart contract initialized to `false`. | ||
#[ink(constructor)] | ||
pub fn new_default() -> Self { | ||
Self::new(Default::default()) | ||
} | ||
|
||
/// Flips the current value of the Flipper's boolean. | ||
#[ink(message, selector = 0xcde4efa9)] | ||
pub fn flip(&mut self) { | ||
self.value = !self.value; | ||
} | ||
|
||
/// Returns the current value of the Flipper's boolean. | ||
#[ink(message)] | ||
pub fn get(&self) -> bool { | ||
self.value | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[ink::test] | ||
fn default_works() { | ||
let flipper = Flipper::new_default(); | ||
assert!(!flipper.get()); | ||
} | ||
|
||
#[ink::test] | ||
fn it_works() { | ||
let mut flipper = Flipper::new(false); | ||
assert!(!flipper.get()); | ||
flipper.flip(); | ||
assert!(flipper.get()); | ||
} | ||
} | ||
|
||
#[cfg(all(test, feature = "e2e-tests"))] | ||
mod e2e_tests { | ||
use super::*; | ||
use ink_e2e::build_message; | ||
|
||
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
|
||
#[ink_e2e::test] | ||
async fn it_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> { | ||
// given | ||
let constructor = FlipperRef::new(false); | ||
let contract_acc_id = client | ||
.instantiate("flipper", &ink_e2e::alice(), constructor, 0, None) | ||
.await | ||
.expect("instantiate failed") | ||
.account_id; | ||
|
||
let get = build_message::<FlipperRef>(contract_acc_id.clone()) | ||
.call(|flipper| flipper.get()); | ||
let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; | ||
assert!(matches!(get_res.return_value(), false)); | ||
|
||
// when | ||
let flip = build_message::<FlipperRef>(contract_acc_id.clone()) | ||
.call(|flipper| flipper.flip()); | ||
let _flip_res = client | ||
.call(&ink_e2e::bob(), flip, 0, None) | ||
.await | ||
.expect("flip failed"); | ||
|
||
// then | ||
let get = build_message::<FlipperRef>(contract_acc_id.clone()) | ||
.call(|flipper| flipper.get()); | ||
let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; | ||
assert!(matches!(get_res.return_value(), true)); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[ink_e2e::test] | ||
async fn default_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> { | ||
// given | ||
let constructor = FlipperRef::new_default(); | ||
|
||
// when | ||
let contract_acc_id = client | ||
.instantiate("flipper", &ink_e2e::bob(), constructor, 0, None) | ||
.await | ||
.expect("instantiate failed") | ||
.account_id; | ||
|
||
// then | ||
let get = build_message::<FlipperRef>(contract_acc_id.clone()) | ||
.call(|flipper| flipper.get()); | ||
let get_res = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await; | ||
assert!(matches!(get_res.return_value(), false)); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -2,5 +2,3 @@ | |
.idea | ||
*.log | ||
tmp/ | ||
|
||
build |
Oops, something went wrong.