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

Tamagotchi_nft #5

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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;

Choose a reason for hiding this comment

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

One should reset the approved_account after transferring the ownership.

Copy link
Owner Author

Choose a reason for hiding this comment

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

image

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() {

Choose a reason for hiding this comment

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

Better would be better to check the account permissions inside the TmgAction::Transfer branch to not double the code.

Also, it is better to not call msg::source twice.

Copy link
Owner Author

Choose a reason for hiding this comment

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

image

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()));

Choose a reason for hiding this comment

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

Need to check the transfer from approved and not approved accounts.

Copy link
Owner Author

@Jackliu-miaozi Jackliu-miaozi Dec 19, 2023

Choose a reason for hiding this comment

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

Why am I unable to pass the test?
IMAGE 2023-12-19 10:02:43

// 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
}
Loading