From df7ad5ab52142ff149f0393d9ecbfb918655b34b Mon Sep 17 00:00:00 2001 From: jonar12 Date: Thu, 11 Jan 2024 14:54:14 -0600 Subject: [PATCH] Completed HW 01 of Gear Academy --- contracts/01-tamagotchi/io/src/lib.rs | 14 ++++++--- contracts/01-tamagotchi/src/lib.rs | 39 ++++++++++++++++++++++++++ contracts/01-tamagotchi/tests/smoke.rs | 14 ++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/contracts/01-tamagotchi/io/src/lib.rs b/contracts/01-tamagotchi/io/src/lib.rs index 394f418f..a146d46b 100644 --- a/contracts/01-tamagotchi/io/src/lib.rs +++ b/contracts/01-tamagotchi/io/src/lib.rs @@ -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; @@ -10,6 +10,8 @@ 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)] @@ -17,6 +19,8 @@ pub struct Tamagotchi { #[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)] @@ -24,15 +28,17 @@ pub enum TmgAction { #[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; + type Handle = InOut; + type State = Out; type Reply = (); type Others = (); type Signal = (); diff --git a/contracts/01-tamagotchi/src/lib.rs b/contracts/01-tamagotchi/src/lib.rs index 85f152bd..055703be 100644 --- a/contracts/01-tamagotchi/src/lib.rs +++ b/contracts/01-tamagotchi/src/lib.rs @@ -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 = 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"); } diff --git a/contracts/01-tamagotchi/tests/smoke.rs b/contracts/01-tamagotchi/tests/smoke.rs index 5dd29f16..c98662c1 100644 --- a/contracts/01-tamagotchi/tests/smoke.rs +++ b/contracts/01-tamagotchi/tests/smoke.rs @@ -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()); }