From 0676fdd40a1168c6b8dd91cd10f4c16473aeddb4 Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:11:33 +0800 Subject: [PATCH 1/2] homework_04 --- contracts/04-tamagotchi-shop/Cargo.toml | 2 + contracts/04-tamagotchi-shop/io/Cargo.toml | 1 + contracts/04-tamagotchi-shop/io/src/lib.rs | 22 ++++++ contracts/04-tamagotchi-shop/src/lib.rs | 84 +++++++++++++++++++++- contracts/Cargo.lock | 2 + 5 files changed, 108 insertions(+), 3 deletions(-) diff --git a/contracts/04-tamagotchi-shop/Cargo.toml b/contracts/04-tamagotchi-shop/Cargo.toml index 04921ed2..7c1dd868 100644 --- a/contracts/04-tamagotchi-shop/Cargo.toml +++ b/contracts/04-tamagotchi-shop/Cargo.toml @@ -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 diff --git a/contracts/04-tamagotchi-shop/io/Cargo.toml b/contracts/04-tamagotchi-shop/io/Cargo.toml index c1934f54..ad2a78cf 100644 --- a/contracts/04-tamagotchi-shop/io/Cargo.toml +++ b/contracts/04-tamagotchi-shop/io/Cargo.toml @@ -7,3 +7,4 @@ publish.workspace = true [dependencies] gmeta.workspace = true gstd.workspace = true + diff --git a/contracts/04-tamagotchi-shop/io/src/lib.rs b/contracts/04-tamagotchi-shop/io/src/lib.rs index adb11e32..bc142de6 100644 --- a/contracts/04-tamagotchi-shop/io/src/lib.rs +++ b/contracts/04-tamagotchi-shop/io/src/lib.rs @@ -20,7 +20,14 @@ pub struct Tamagotchi { pub slept_block: u64, pub approved_account: Option, // 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)] @@ -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)] @@ -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; diff --git a/contracts/04-tamagotchi-shop/src/lib.rs b/contracts/04-tamagotchi-shop/src/lib.rs index 9ff2f388..3148b541 100644 --- a/contracts/04-tamagotchi-shop/src/lib.rs +++ b/contracts/04-tamagotchi-shop/src/lib.rs @@ -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)] @@ -18,7 +20,15 @@ pub struct Tamagotchi { pub slept: u64, pub slept_block: u64, pub approved_account: Option, + 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 = @@ -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 = None; @@ -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()) }; @@ -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 { @@ -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] diff --git a/contracts/Cargo.lock b/contracts/Cargo.lock index b112d757..65caf12a 100644 --- a/contracts/Cargo.lock +++ b/contracts/Cargo.lock @@ -5739,6 +5739,8 @@ version = "0.3.3" dependencies = [ "gear-wasm-builder", "gstd", + "sharded-fungible-token-io", + "store-io", "tamagotchi-shop-io", ] From 696e02ef7cd82cba0e4477ce9178b625ce19ab52 Mon Sep 17 00:00:00 2001 From: Jackliu-miaozi <71253778+Jackliu-miaozi@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:08:58 +0800 Subject: [PATCH 2/2] tamagotchi_shop --- contracts/04-tamagotchi-shop/src/lib.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/contracts/04-tamagotchi-shop/src/lib.rs b/contracts/04-tamagotchi-shop/src/lib.rs index 3148b541..37bec00a 100644 --- a/contracts/04-tamagotchi-shop/src/lib.rs +++ b/contracts/04-tamagotchi-shop/src/lib.rs @@ -24,10 +24,6 @@ pub struct Tamagotchi { 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 {