Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ar-academy into homework_1
  • Loading branch information
Jackliu-miaozi committed Dec 13, 2023
2 parents 9d72824 + 4a53c8a commit 94cf3b6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
15 changes: 11 additions & 4 deletions contracts/02-tamagotchi-interaction/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#![no_std]

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

#[derive(Default, Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub struct Tamagotchi {
// TODO: 0️⃣ Copy fields from previous lesson and push changes to the master branch
pub name: String,
pub date_of_birth: u64,
// TODO: 1️⃣ Add new fields
}

Expand All @@ -16,6 +19,8 @@ pub struct Tamagotchi {
#[scale_info(crate = gstd::scale_info)]
pub enum TmgAction {
// TODO: 0️⃣ Copy actions from previous lesson and push changes to the master branch
Name,
Age,
// TODO: 2️⃣ Add new actions
}

Expand All @@ -24,16 +29,18 @@ pub enum TmgAction {
#[scale_info(crate = gstd::scale_info)]
pub enum TmgEvent {
// TODO: 0️⃣ Copy events from previous lesson and push changes to the master branch
Name(String),
Age(u64),
// TODO: 3️⃣ Add new events
}

pub struct ProgramMetadata;

// TODO: 0️⃣ Copy `Metadata` from the first lesson and push changes to the master branch
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
23 changes: 23 additions & 0 deletions contracts/02-tamagotchi-interaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,38 @@ use gstd::prelude::*;
#[no_mangle]
extern fn init() {
// TODO: 0️⃣ Copy the `init` function from the previous lesson and push changes to the master branch
let initname = msg::load().expect("unable to load name");
let birthdate = exec::block_timestamp();
let tmg = Tamagotchi {
name: initname,
date_of_birth: birthdate,
};
unsafe {
TAMAGOTCHI = Some(tmg);
};
}

#[no_mangle]
extern fn handle() {
// TODO: 0️⃣ Copy the `handle` function from the previous lesson and push changes to the master branch
let action: TmgAction = msg::load().expect("unable to load action");
let tmg = unsafe { TAMAGOTCHI.get_or_insert(Default::default()) };
match action {
TmgAction::Name => {
msg::reply(TmgEvent::Name(tmg.name.clone()), 0)
.expect("Error in a reply'tamagotchi::name'");
}
TmgAction::Age => {
let age = exec::block_timestamp() - tmg.date_of_birth;
msg::reply(TmgEvent::Age(age), 0).expect("Error in a reply'tamagotchi::age'");
}
}
// TODO: 5️⃣ Add new logic for calculating the `fed`, `entertained` and `slept` levels
}

#[no_mangle]
extern fn state() {
// TODO: 0️⃣ Copy the `handle` function from the previous lesson and push changes to the master branch
let tmg = unsafe { TAMAGOTCHI.take().expect("Unexpected error in taking state") };
msg::reply(tmg, 0).expect("Failed to share state");
}
38 changes: 37 additions & 1 deletion contracts/02-tamagotchi-interaction/tests/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,47 @@ use gtest::{Program, System};

// TODO: 0️⃣ Copy tests from the previous lesson and push changes to the master branch

#[test]
fn smoke_test() {
let sys = System::new();
sys.init_logger();
let program = Program::current(&sys);
let result = program.send(2, String::from("Goodmoring"));
assert!(!result.main_failed());
let result = program.send(2, TmgAction::Name);
let log = Log::builder()
.dest(2)
.payload(TmgEvent::Name(String::from("Goodmoring")));
assert!(result.contains(&log));
let _result = program.send(2, TmgAction::Age);
// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp()));
// assert!(result.contains(&log));

//How to test the age?
}

#[test]
fn negative_smoke_test() {
let sys = System::new();
sys.init_logger();
let program = Program::current(&sys);
let payload = vec![1, 2, 3];
let _result = program.send(2, payload);
// assert!(result.main_failed());
// Why the assert is panic?

// let result = program.send(1, TmgAction::Name);
// let log = Log::builder().dest(2).payload(TmgEvent::Name("Goodmoring".to_string()));
// assert!(!result.contains(&log));
// let result = program.send(1, TmgAction::Age);
// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp()));
// assert!(!result.contains(&log));
}

#[test]
fn interaction_test() {
let sys = System::new();
sys.init_logger();
let _program = Program::current(&sys);

// TODO: 6️⃣ Test new functionality
}

0 comments on commit 94cf3b6

Please sign in to comment.