Skip to content

Commit

Permalink
homework_04
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackliu-miaozi committed Dec 15, 2023
1 parent 1b1d278 commit 0676fdd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
2 changes: 2 additions & 0 deletions contracts/04-tamagotchi-shop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ publish.workspace = true
[dependencies]
gstd.workspace = true
tamagotchi-shop-io.workspace = true
store-io.workspace = true
sharded-fungible-token-io.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true
Expand Down
1 change: 1 addition & 0 deletions contracts/04-tamagotchi-shop/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

22 changes: 22 additions & 0 deletions contracts/04-tamagotchi-shop/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ pub struct Tamagotchi {
pub slept_block: u64,
pub approved_account: Option<ActorId>,
// TODO: 2️⃣ Add new fields
pub ft_contract_id: ActorId,
pub transaction_id: TransactionId,
pub approve_transaction: Option<(TransactionId, ActorId, u128)>,
}
pub type TransactionId = u64;
pub type AttributeId = u32;
pub type Price = u128;
pub type TamagotchiId = ActorId;

#[derive(Encode, Decode, TypeInfo)]
#[codec(crate = gstd::codec)]
Expand All @@ -36,6 +43,15 @@ pub enum TmgAction {
Approve(ActorId),
RevokeApproval,
// TODO: 3️⃣ Add new actions
SetFTokenContract(ActorId),
ApproveTokens {
account: ActorId,
amount: u128,
},
BuyAttribute {
store_id: ActorId,
attribute_id: AttributeId,
},
}

#[derive(Encode, Decode, TypeInfo)]
Expand All @@ -52,6 +68,12 @@ pub enum TmgEvent {
Approved(ActorId),
ApprovalRevoked,
// TODO: 4️⃣ Add new events
FTokenContractSet,
TokensApproved { account: ActorId, amount: u128 },
ApprovalError,
AttributeBought(AttributeId),
CompletePrevPurchase(AttributeId),
ErrorDuringPurchase,
}

pub struct ProgramMetadata;
Expand Down
84 changes: 81 additions & 3 deletions contracts/04-tamagotchi-shop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#[allow(unused_imports)]
use gstd::{exec, msg, prelude::*, ActorId};
use sharded_fungible_token_io::{FTokenAction, FTokenEvent, LogicAction};
use store_io::*;
use tamagotchi_shop_io::*;

#[derive(Default, Encode, Decode, TypeInfo)]
Expand All @@ -18,7 +20,15 @@ pub struct Tamagotchi {
pub slept: u64,
pub slept_block: u64,
pub approved_account: Option<ActorId>,
pub ft_contract_id: ActorId,
pub transaction_id: TransactionId,
pub approve_transaction: Option<(TransactionId, ActorId, u128)>,
}
pub type TransactionId = u64;
pub type AttributeId = u32;
pub type Price = u128;
pub type TamagotchiId = ActorId;

impl Tamagotchi {
fn current_fed(&mut self) -> u64 {
let a: u64 =
Expand All @@ -35,6 +45,58 @@ impl Tamagotchi {
- (ENERGY_PER_BLOCK as u64) * ((exec::block_height() as u64) - self.slept_block);
c
}
async fn approve_tokens(&mut self, account: &ActorId, amount: u128) {
if self.ft_contract_id == ActorId::default() {
msg::reply(TmgEvent::ApprovalError, 0)
.expect("Error in a reply'tamagotchi::approve_tokens'");
panic!("You need to set the FToken contract first");
} else {
let _unused = msg::send_for_reply_as::<_, FTokenEvent>(
self.ft_contract_id,
FTokenAction::Message {
transaction_id: self.transaction_id,
payload: LogicAction::Approve {
approved_account: *account,
amount,
},
},
0,
0,
)
.expect("Error in sending a message `FTokenAction::Message`")
.await;
self.transaction_id += 1;
self.approve_transaction = Some((self.transaction_id, *account, amount));
msg::reply(
TmgEvent::TokensApproved {
account: *account,
amount,
},
0,
)
.expect("Error in a reply'tamagotchi::approve_tokens'");
};
}
async fn buy_attribute(&mut self, store_id: ActorId, attribute_id: AttributeId) {
if self.approve_transaction.is_none() {
msg::reply(TmgEvent::ErrorDuringPurchase, 0)
.expect("Error in a reply'tamagotchi::buy_attribute'");
} else if self.approve_transaction.unwrap().0 != self.transaction_id {
msg::reply(TmgEvent::CompletePrevPurchase(attribute_id), 0)
.expect("Error in a reply'tamagotchi::buy_attribute'");
} else {
let _unused = msg::send_for_reply_as::<_, StoreEvent>(
store_id,
StoreAction::BuyAttribute { attribute_id },
0,
0,
)
.expect("Error in sending a message `StoreAction::BuyAttribute`")
.await;
msg::reply(TmgEvent::AttributeBought(attribute_id), 0)
.expect("Error in a reply'tamagotchi::buy_attribute'");
}
}
}
static mut TAMAGOTCHI: Option<Tamagotchi> = None;

Expand All @@ -59,14 +121,17 @@ extern fn init() {
slept: 2000,
slept_block: sleptblock,
approved_account: None,
ft_contract_id: ActorId::default(),
transaction_id: 0,
approve_transaction: None,
};
unsafe {
TAMAGOTCHI = Some(tmg);
};
}

#[no_mangle]
extern fn handle() {
#[gstd::async_main]
async fn main() {
// 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()) };
Expand Down Expand Up @@ -148,6 +213,20 @@ extern fn handle() {
msg::reply(TmgEvent::ApprovalRevoked, 0)
.expect("Error in a reply'tamagotchi::approval_revoked'");
}
TmgAction::SetFTokenContract(contract_id) => {
tmg.ft_contract_id = contract_id;
msg::reply(TmgEvent::FTokenContractSet, 0)
.expect("Error in a reply'tamagotchi::ftoken_contract_set'");
}
TmgAction::ApproveTokens { account, amount } => {
tmg.approve_tokens(&account, amount).await;
}
TmgAction::BuyAttribute {
store_id,
attribute_id,
} => {
tmg.buy_attribute(store_id, attribute_id).await;
}
}
} else if msg::source() == tmg.approved_account.unwrap_or_default() {
match action {
Expand All @@ -163,7 +242,6 @@ extern fn handle() {
} else {
panic!("You are not the owner of this tamagotchi");
}
// TODO; 6️⃣ Add handling new actions
}

#[no_mangle]
Expand Down
2 changes: 2 additions & 0 deletions contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0676fdd

Please sign in to comment.