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 smart contract section #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 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::{Metadata, In, InOut, Out};
use gstd::prelude::*;
use scale_info::TypeInfo;

Expand All @@ -10,29 +10,34 @@ 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
49 changes: 46 additions & 3 deletions contracts/01-tamagotchi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
#![no_std]

#[allow(unused_imports)]
use gstd::prelude::*;
use gstd::{prelude::*, debug, exec, msg};
use tamagotchi_io::{Tamagotchi, TmgAction, TmgEvent};


Comment on lines +6 to +7
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use cargo fmt before committing.

Suggested change


static mut STATE: Option<Tamagotchi> = None;

#[no_mangle]
extern fn init() {
// TODO: 5️⃣ Initialize the Tamagotchi program
let name: String = msg::load().expect("Cant decode the tamagotchi name");

let tamagotchi = Tamagotchi {
name,
date_of_birth: exec::block_timestamp(),
};

debug!("Program was initialized with Tamagotchi {:?}", tamagotchi);
unsafe {STATE = Some(tamagotchi)};
}

#[no_mangle]
extern fn handle() {
// TODO: 6️⃣ Add handling of `Name` and `Age` actions
extern "C" fn handle() {
let action: TmgAction = msg::load().expect("Unable to decode incoming TmgAction");

let state = unsafe {
STATE.as_ref().expect("The contract is not initialized")
};

match action {
TmgAction::Name => {
debug!("Tamagotchi: Name Requested");
// Directly reply with the name, as TmgAction::Name does not carry data
msg::reply(TmgEvent::Name(state.name.clone()), 0)
.expect("Error handling Tamagotchi name");
}
TmgAction::Age => {
debug!("Tamagotchi: Age Requested");
// Directly reply with the date of birth, as TmgAction::Age does not carry data
msg::reply(TmgEvent::Age(state.date_of_birth), 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Age should depend on the current block timestamp.

.expect("Error handling Tamagotchi age");
}
}
}



#[no_mangle]
extern fn state() {
// TODO: 7️⃣ Return the Tamagotchi state
let tomagotchi = unsafe {
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo:

Suggested change
let tomagotchi = unsafe {
let tamagotchi = unsafe {

STATE
.as_ref()
.expect("The contract is not initialized")
};
if let Err(e) = msg::reply(tomagotchi, 0) {
debug!("Failed to send state: {:?}", e);
}
}
22 changes: 20 additions & 2 deletions contracts/01-tamagotchi/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
use gtest::{Program, System};
use gtest::{Log, Program, System};
use tamagotchi_io::{TmgAction, TmgEvent};

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

// TODO: 8️⃣ Test the program initialization and message handling
let tamagotchi_name = String::from("Keno");
let initialize_contract = program.send(2, tamagotchi_name.clone());

assert!(!initialize_contract.main_failed(), "init function failed");

let get_name_action = TmgAction::Name;
let get_name_result = program.send(3, get_name_action);

assert!(!get_name_result.main_failed(), "Name request failed");

let expected_tamagatchi = Log::builder()
.dest(3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo

Suggested change
let expected_tamagatchi = Log::builder()
let expected_tamagotchi = Log::builder()

.payload(TmgEvent::Name(String::from("Keno")));

assert!(get_name_result.contains(&expected_tamagatchi));


}
Loading