-
Notifications
You must be signed in to change notification settings - Fork 47
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 HW 01 of Gear Academy #55
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,58 @@ | ||||||||||
#![no_std] | ||||||||||
|
||||||||||
use gstd::{debug, exec, msg}; | ||||||||||
#[allow(unused_imports)] | ||||||||||
use gstd::prelude::*; | ||||||||||
use tamagotchi_io::{Tamagotchi, TmgAction, TmgEvent}; | ||||||||||
|
||||||||||
static mut TAMAGOTCHI: Option<Tamagotchi> = None; | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
extern fn init() { | ||||||||||
// TODO: 5️⃣ Initialize the Tamagotchi program | ||||||||||
let name: String = msg::load() | ||||||||||
.expect("Can't decode the init message"); | ||||||||||
|
||||||||||
debug!("Program was initialized with message {:?}", | ||||||||||
name); | ||||||||||
|
||||||||||
let tamagotchi = Tamagotchi { | ||||||||||
name: name.clone(), | ||||||||||
date_of_birth: exec::block_timestamp() | ||||||||||
}; | ||||||||||
|
||||||||||
unsafe { | ||||||||||
TAMAGOTCHI = Some(tamagotchi) | ||||||||||
} | ||||||||||
|
||||||||||
msg::reply( | ||||||||||
TmgEvent::Name(name), | ||||||||||
0 | ||||||||||
).unwrap(); | ||||||||||
Comment on lines
+28
to
+31
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. This reply is not defined in metadata, it is pretty senseless.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
extern fn handle() { | ||||||||||
// TODO: 6️⃣ Add handling of `Name` and `Age` actions | ||||||||||
let input_msg = msg::load().expect("Error in loading Tmg Input Message"); | ||||||||||
let tmg = unsafe { | ||||||||||
TAMAGOTCHI.as_mut().expect("The contract is not initialized") | ||||||||||
}; | ||||||||||
match input_msg { | ||||||||||
TmgAction::Name => { | ||||||||||
msg::reply(TmgEvent::Name(tmg.name.clone()), 0).expect("Name not loaded correctly"); | ||||||||||
} | ||||||||||
TmgAction::Age => { | ||||||||||
msg::reply(TmgEvent::Age(tmg.date_of_birth.clone()), 0).expect("Age not loaded correctly"); | ||||||||||
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. No need to clone |
||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
#[no_mangle] | ||||||||||
extern fn state() { | ||||||||||
// TODO: 7️⃣ Return the Tamagotchi state | ||||||||||
let tmg = unsafe { | ||||||||||
TAMAGOTCHI.as_ref().expect("The contract is not initialized") | ||||||||||
}; | ||||||||||
msg::reply(tmg, 0).expect("Failed to share state"); | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use gtest::{Program, System}; | ||
use gtest::{Log, Program, System}; | ||
use tamagotchi_io::{TmgAction, TmgEvent}; | ||
|
||
#[test] | ||
fn smoke_test() { | ||
|
@@ -7,4 +8,15 @@ fn smoke_test() { | |
let _program = Program::current(&sys); | ||
|
||
// TODO: 8️⃣ Test the program initialization and message handling | ||
let res = _program.send(2, String::from("Tamagotchi Name")); | ||
assert!(!res.main_failed()); | ||
|
||
let res = _program.send(2, TmgAction::Name); | ||
let expected_log = Log::builder() | ||
.dest(2) | ||
.payload(TmgEvent::Name(String::from("Tamagotchi Name"))); | ||
assert!(res.contains(&expected_log)); | ||
|
||
let res = _program.send(2, TmgAction::Age); | ||
assert!(!res.log().is_empty()); | ||
} | ||
Comment on lines
+20
to
+21
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. You could write a more expressive test by using the |
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.
No need to clone the
name
, it is better to move it.