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

Homework - Tamagotchi 3 NFT #76

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
22 changes: 22 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
1 change: 1 addition & 0 deletions contracts/01-tamagotchi/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ publish.workspace = true
[dependencies]
gmeta.workspace = true
gstd.workspace = true
gear-core.workspace = true
19 changes: 12 additions & 7 deletions contracts/01-tamagotchi/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
#![no_std]

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

#[derive(Default, Encode, Decode, TypeInfo)]

#[derive(Default, Encode, Decode, TypeInfo, Debug)]
#[codec(crate = gstd::codec)]
#[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
59 changes: 57 additions & 2 deletions contracts/01-tamagotchi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,74 @@
#![no_std]


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


static mut TAMAGOTCHI: Option<Tamagotchi> = None;

#[no_mangle]
extern fn init() {
// TODO: 5️⃣ Initialize the Tamagotchi program
let tamagochi: Tamagotchi = Tamagotchi {
name: String::from("Ivan"),
date_of_birth: 45,

Choose a reason for hiding this comment

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

Why 45?

};
let init_msg: String = msg::load().expect("Can't decode an init message");

let tamagotchi = Tamagotchi {
name: "Ivan".to_string(),

Choose a reason for hiding this comment

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

It must be set from incoming message

date_of_birth: exec::block_timestamp(),
};
debug!(
"The Tamagotchi Program was initialized with name {:?} and birth date {:?}",
tamagotchi.name, tamagotchi.date_of_birth
);
unsafe { TAMAGOTCHI = Some(tamagotchi) };


debug!("Program was initialized with message {:?}",
init_msg);
let block = exec::block_timestamp();
debug!("Current block timestamp is {}", block);

}

#[no_mangle]
extern fn handle() {
// TODO: 6️⃣ Add handling of `Name` and `Age` actions
let _tamagotchi = unsafe {
TAMAGOTCHI
.as_mut()
.expect("The contract is not initialized")
};

let name = &_tamagotchi.name;
let current_time = exec::block_timestamp();
let age = current_time - _tamagotchi.date_of_birth;
let action: TmgAction = msg::load().expect("Can't decode an action message");

//

let _event = match action {
TmgAction::Name => {
msg::reply(name, 0).expect("Error in sending name");
}
TmgAction::Age =>{
msg::reply(age, 0).expect("Error in sending age");

}
};

}

#[no_mangle]
extern fn state() {
// TODO: 7️⃣ Return the Tamagotchi state
let tamagotchi = unsafe {
TAMAGOTCHI
.as_ref()
.expect("The contract is not initialized")
};
msg::reply(tamagotchi, 0).expect("Failed to share state");
}
27 changes: 24 additions & 3 deletions contracts/01-tamagotchi/tests/smoke.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
use gtest::{Program, System};
use gstd::debug;
use gtest::{Log, Program, System};
use tamagotchi_io:: TmgAction;







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

let res = program.send(2, String::from("Init Tamagotchi"));

assert!(!res.main_failed());

let res_tmgAction_name = program.send(2, TmgAction::Name);

let expected_log = Log::builder()
.dest(2)
.payload(String::from("Ivan"));
assert!(res_tmgAction_name.contains(&expected_log));

// TODO: 8️⃣ Test the program initialization and message handling
// let res_tmgAction_age = program.send(2, TmgAction::Age);
// debug!("tamagotchi age is: {:?}", res_tmgAction_age);

}
16 changes: 12 additions & 4 deletions contracts/02-tamagotchi-interaction/io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
#![no_std]

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

#[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

}

#[derive(Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
#[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 +30,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
53 changes: 53 additions & 0 deletions contracts/02-tamagotchi-interaction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,73 @@
#[allow(unused_imports)]
use gstd::prelude::*;

use tamagotchi_io::{Tamagotchi, TmgAction, TmgEvent};

// TODO: 4️⃣ Define constants

static mut TAMAGOTCHI: Option<Tamagotchi> = None;

#[no_mangle]
extern fn init() {
// TODO: 0️⃣ Copy the `init` function from the previous lesson and push changes to the master branch
let tamagochi: Tamagotchi = Tamagotchi {
name: String::from("Ivan"),
date_of_birth: 45,
};
let init_msg: String = msg::load().expect("Can't decode an init message");

let tamagotchi = Tamagotchi {
name: "Ivan".to_string(),
date_of_birth: exec::block_timestamp(),
};
debug!(
"The Tamagotchi Program was initialized with name {:?} and birth date {:?}",
tamagotchi.name, tamagotchi.date_of_birth
);
unsafe { TAMAGOTCHI = Some(tamagotchi) };


debug!("Program was initialized with message {:?}",
init_msg);
let block = exec::block_timestamp();
debug!("Current block timestamp is {}", block);
}

#[no_mangle]
extern fn handle() {
// TODO: 0️⃣ Copy the `handle` function from the previous lesson and push changes to the master branch
let _tamagotchi = unsafe {
TAMAGOTCHI
.as_mut()
.expect("The contract is not initialized")
};

let name = &_tamagotchi.name;
let current_time = exec::block_timestamp();
let age = current_time - _tamagotchi.date_of_birth;
let action: TmgAction = msg::load().expect("Can't decode an action message");

//

let _event = match action {
TmgAction::Name => {
msg::reply(name, 0).expect("Error in sending name");
}
TmgAction::Age =>{
msg::reply(age, 0).expect("Error in sending age");

}
};
// TODO: 5️⃣ Add new logic for calculating the `fed`, `entertained` and `slept` levels

Choose a reason for hiding this comment

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

No implementation that is requires

}

#[no_mangle]
extern fn state() {
// TODO: 0️⃣ Copy the `handle` function from the previous lesson and push changes to the master branch
let tamagotchi = unsafe {
TAMAGOTCHI
.as_ref()
.expect("The contract is not initialized")
};
msg::reply(tamagotchi, 0).expect("Failed to share state");
}
Loading