Skip to content

Commit

Permalink
tamagotchi_nft
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackliu-miaozi committed Dec 14, 2023
1 parent 8e3ae3d commit 34baecc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
33 changes: 20 additions & 13 deletions contracts/03-tamagotchi-nft/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Tamagotchi {
pub slept: u64,
pub slept_block: u64,
// TODO: 1️⃣ Add new fields
pub approved_account: Option<ActorId>,
}

#[derive(Encode, Decode, TypeInfo)]
Expand All @@ -27,11 +28,14 @@ pub struct Tamagotchi {
pub enum TmgAction {
// TODO: 0️⃣ Copy actions from previous lesson and push changes to the master branch
Name,
Age,
Feed,
Entertain,
Sleep,
Age,
Feed,
Entertain,
Sleep,
// TODO: 2️⃣ Add new actions
Transfer(ActorId),
Approve(ActorId),
RevokeApproval,
}

#[derive(Encode, Decode, TypeInfo)]
Expand All @@ -40,11 +44,14 @@ pub enum TmgAction {
pub enum TmgEvent {
// TODO: 0️⃣ Copy events from previous lesson and push changes to the master branch
Name(String),
Age(u64),
Fed,
Entertained,
Slept,
Age(u64),
Fed,
Entertained,
Slept,
// TODO: 3️⃣ Add new events
Transferred(ActorId),
Approved(ActorId),
ApprovalRevoked,
}

pub struct ProgramMetadata;
Expand All @@ -60,9 +67,9 @@ impl Metadata for ProgramMetadata {
}

pub const HUNGER_PER_BLOCK: u64 = 1;
pub const BOREDOM_PER_BLOCK: u64 = 2;
pub const ENERGY_PER_BLOCK: u64 = 2;
pub const BOREDOM_PER_BLOCK: u64 = 2;
pub const ENERGY_PER_BLOCK: u64 = 2;

pub const FILL_PER_FEED: u64 = 1000;
pub const FILL_PER_ENTERTAINMENT: u64 = 1000;
pub const FILL_PER_SLEEP: u64 = 1000;
pub const FILL_PER_FEED: u64 = 1000;
pub const FILL_PER_ENTERTAINMENT: u64 = 1000;
pub const FILL_PER_SLEEP: u64 = 1000;
28 changes: 28 additions & 0 deletions contracts/03-tamagotchi-nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Tamagotchi {
pub entertained_block: u64,
pub slept: u64,
pub slept_block: u64,
pub approved_account: Option<ActorId>,
}
impl Tamagotchi {
fn current_fed(&mut self) -> u64 {
Expand Down Expand Up @@ -55,6 +56,7 @@ extern fn init() {
entertained_block: entertainedblock,
slept: 2000,
slept_block: sleptblock,
approved_account: None,
};
unsafe {
TAMAGOTCHI = Some(tmg);
Expand Down Expand Up @@ -129,6 +131,32 @@ extern fn handle() {
msg::reply(TmgEvent::Slept, 1).expect("Error in a reply'tamagotchi::slept'");
}
}
TmgAction::Transfer(account) => {
tmg.owner = account;
msg::reply(TmgEvent::Transferred(account), 0)
.expect("Error in a reply'tamagotchi::transferred'");
}
TmgAction::Approve(account) => {
tmg.approved_account = Some(account);
msg::reply(TmgEvent::Approved(account), 0)
.expect("Error in a reply'tamagotchi::approved'");
}
TmgAction::RevokeApproval => {
tmg.approved_account = None;
msg::reply(TmgEvent::ApprovalRevoked, 0)
.expect("Error in a reply'tamagotchi::approval_revoked'");
}
}
} else if msg::source() == tmg.approved_account.unwrap_or_default() {
match action {
TmgAction::Transfer(account) => {
tmg.owner = account;
msg::reply(TmgEvent::Transferred(account), 0)
.expect("Error in a reply'tamagotchi::transfered'");
}
_ => {
panic!("You are not the approved people of this tamagotchi");
}
}
} else {
panic!("You are not the owner of this tamagotchi");
Expand Down
25 changes: 23 additions & 2 deletions contracts/03-tamagotchi-nft/tests/owning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn interaction_test() {
let log = Log::builder().dest(2).payload(TmgEvent::Slept);
assert!(result.contains(&log));

let _result = program.send(1, TmgAction::Sleep);
// let _result = program.send(1, TmgAction::Sleep);
//how to test the panic result?
//negetive test
}
Expand All @@ -66,7 +66,28 @@ fn interaction_test() {
fn owning_test() {
let sys = System::new();
sys.init_logger();
let _program = Program::current(&sys);
let program = Program::current(&sys);
let _result = program.send(2, String::from("Goodmoring"));
let result = program.send(2, TmgAction::Transfer(1.into()));
let log = Log::builder()
.dest(2)
.payload(TmgEvent::Transferred(1.into()));
assert!(result.contains(&log));

// let result = program.send(1, TmgAction::Approve(2.into()));
// let log = Log::builder().dest(1).payload(TmgEvent::Approved(2.into()));
// assert!(result.contains(&log));

// let result = program.send(2, TmgAction::Transfer(3.into()));
// let log = Log::builder().dest(2).payload(TmgEvent::Transferred(3.into()));
// assert!(result.contains(&log));

// let result = program.send(3, TmgAction::RevokeApproval);
// let log = Log::builder().dest(3).payload(TmgEvent::ApprovalRevoked);
// assert!(result.contains(&log));

//why the test is panic?
//TODO: I don't know how to test the code.

// TODO: 6️⃣ Test new functionality
}

0 comments on commit 34baecc

Please sign in to comment.