-
Notifications
You must be signed in to change notification settings - Fork 48
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
completed smart contract section #54
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,19 +1,62 @@ | ||||||
#![no_std] | ||||||
|
||||||
#[allow(unused_imports)] | ||||||
use gstd::prelude::*; | ||||||
use gstd::{prelude::*, debug, exec, msg}; | ||||||
use tamagotchi_io::{Tamagotchi, TmgAction, TmgEvent}; | ||||||
|
||||||
|
||||||
|
||||||
static mut STATE: Option<Tamagotchi> = None; | ||||||
|
||||||
#[no_mangle] | ||||||
extern fn init() { | ||||||
// TODO: 5️⃣ Initialize the Tamagotchi program | ||||||
let name: String = msg::load().expect("Cant decode the tamagotchi name"); | ||||||
|
||||||
let tamagotchi = Tamagotchi { | ||||||
name, | ||||||
date_of_birth: exec::block_timestamp(), | ||||||
}; | ||||||
|
||||||
debug!("Program was initialized with Tamagotchi {:?}", tamagotchi); | ||||||
unsafe {STATE = Some(tamagotchi)}; | ||||||
} | ||||||
|
||||||
#[no_mangle] | ||||||
extern fn handle() { | ||||||
// TODO: 6️⃣ Add handling of `Name` and `Age` actions | ||||||
extern "C" fn handle() { | ||||||
let action: TmgAction = msg::load().expect("Unable to decode incoming TmgAction"); | ||||||
|
||||||
let state = unsafe { | ||||||
STATE.as_ref().expect("The contract is not initialized") | ||||||
}; | ||||||
|
||||||
match action { | ||||||
TmgAction::Name => { | ||||||
debug!("Tamagotchi: Name Requested"); | ||||||
// Directly reply with the name, as TmgAction::Name does not carry data | ||||||
msg::reply(TmgEvent::Name(state.name.clone()), 0) | ||||||
.expect("Error handling Tamagotchi name"); | ||||||
} | ||||||
TmgAction::Age => { | ||||||
debug!("Tamagotchi: Age Requested"); | ||||||
// Directly reply with the date of birth, as TmgAction::Age does not carry data | ||||||
msg::reply(TmgEvent::Age(state.date_of_birth), 0) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Age should depend on the current block timestamp. |
||||||
.expect("Error handling Tamagotchi age"); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
|
||||||
#[no_mangle] | ||||||
extern fn state() { | ||||||
// TODO: 7️⃣ Return the Tamagotchi state | ||||||
let tomagotchi = unsafe { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo:
Suggested change
|
||||||
STATE | ||||||
.as_ref() | ||||||
.expect("The contract is not initialized") | ||||||
}; | ||||||
if let Err(e) = msg::reply(tomagotchi, 0) { | ||||||
debug!("Failed to send state: {:?}", e); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,28 @@ | ||||||
use gtest::{Program, System}; | ||||||
use gtest::{Log, Program, System}; | ||||||
use tamagotchi_io::{TmgAction, TmgEvent}; | ||||||
|
||||||
#[test] | ||||||
fn smoke_test() { | ||||||
let sys = System::new(); | ||||||
sys.init_logger(); | ||||||
let _program = Program::current(&sys); | ||||||
let program = Program::current(&sys); | ||||||
|
||||||
// TODO: 8️⃣ Test the program initialization and message handling | ||||||
let tamagotchi_name = String::from("Keno"); | ||||||
let initialize_contract = program.send(2, tamagotchi_name.clone()); | ||||||
|
||||||
assert!(!initialize_contract.main_failed(), "init function failed"); | ||||||
|
||||||
let get_name_action = TmgAction::Name; | ||||||
let get_name_result = program.send(3, get_name_action); | ||||||
|
||||||
assert!(!get_name_result.main_failed(), "Name request failed"); | ||||||
|
||||||
let expected_tamagatchi = Log::builder() | ||||||
.dest(3) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo
Suggested change
|
||||||
.payload(TmgEvent::Name(String::from("Keno"))); | ||||||
|
||||||
assert!(get_name_result.contains(&expected_tamagatchi)); | ||||||
|
||||||
|
||||||
} |
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.
Please use
cargo fmt
before committing.