Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions contracts/01-tamagotchi/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]

use codec::{Decode, Encode};
use gmeta::Metadata;
use gmeta::{In, InOut, Metadata, Out};
use gstd::prelude::*;
use scale_info::TypeInfo;

Expand All @@ -10,29 +10,35 @@ use scale_info::TypeInfo;
#[scale_info(crate = gstd::scale_info)]
pub struct Tamagotchi {
// TODO: 1️⃣ Add `name` and `age` fields
pub name: String,
pub date_of_birth: u64
}

#[derive(Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum TmgAction {
// TODO: 2️⃣ Add `Name` and `Age` actions that set the name and age
Name,
Age
}

#[derive(Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum TmgEvent {
// TODO: 3️⃣ Add `Name` and `Age` events that return the name and age
Name(String),
Age(u64)
}

pub struct ProgramMetadata;

// TODO: 4️⃣ Fill `Init`, `Handle`, and `State` types
impl Metadata for ProgramMetadata {
type Init = ();
type Handle = ();
type State = ();
type Init = In<String>;
type Handle = InOut<TmgAction, TmgEvent>;
type State = Out<Tamagotchi>;
type Reply = ();
type Others = ();
type Signal = ();
Expand Down
39 changes: 39 additions & 0 deletions contracts/01-tamagotchi/src/lib.rs
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(),
Copy link
Contributor

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.

Suggested change
name: name.clone(),
name,

date_of_birth: exec::block_timestamp()
};

unsafe {
TAMAGOTCHI = Some(tamagotchi)
}

msg::reply(
TmgEvent::Name(name),
0
).unwrap();
Comment on lines +28 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The 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
msg::reply(
TmgEvent::Name(name),
0
).unwrap();

}

#[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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to clone Copy types. Also, Age should depend on the current block timestamp.

}
}
}

#[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");
}
14 changes: 13 additions & 1 deletion contracts/01-tamagotchi/tests/smoke.rs
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() {
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write a more expressive test by using the System::spend_blocks function.

Loading