Skip to content

Commit

Permalink
Completed HW 01 of Gear Academy
Browse files Browse the repository at this point in the history
  • Loading branch information
jonar12 committed Jan 11, 2024
1 parent 147c5bc commit df7ad5a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
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(),
date_of_birth: exec::block_timestamp()
};

unsafe {
TAMAGOTCHI = Some(tamagotchi)
}

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");
}
}
}

#[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());
}

0 comments on commit df7ad5a

Please sign in to comment.