Skip to content

Commit

Permalink
add template quest contract and check is_claimable (#61)
Browse files Browse the repository at this point in the history
* add template quest contract and check is_claimable

* scrab format

* move dispachers and reward into a constructor

* remove template_store

* make art_peace in storage of type contract address

* use component decleration

* test for test_get_reward

* test for is_claimable

* test claim function

* fix scrap format

* Patch tests and template quest storage to remove template store, moved some test functions to utils

---------

Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
mubarak23 and b-j-roberts authored Apr 23, 2024
1 parent e7ce8c1 commit 2dc9e5b
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 79 deletions.
2 changes: 2 additions & 0 deletions onchain/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use interfaces::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait, Pixel
mod quests {
pub mod interfaces;
pub mod pixel_quest;
pub mod template_quest;
pub mod unruggable_quest;

use interfaces::{
Expand Down Expand Up @@ -56,6 +57,7 @@ mod mocks {
mod tests {
mod art_peace;
mod username_store;
mod template_quest;
pub(crate) mod utils;
}

77 changes: 77 additions & 0 deletions onchain/src/quests/template_quest.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#[starknet::contract]
pub mod TemplateQuest {
use starknet::{ContractAddress, get_caller_address};
use art_peace::templates::interfaces::{ITemplateStoreDispatcher, ITemplateStoreDispatcherTrait};
use art_peace::quests::{IQuest, QuestClaimed};

#[storage]
struct Storage {
art_peace: ContractAddress,
reward: u32,
claimed: LegacyMap<ContractAddress, bool>,
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
QuestClaimed: QuestClaimed,
}


#[derive(Drop, Serde)]
pub struct TemplateQuestInitParams {
pub art_peace: ContractAddress,
pub reward: u32,
}

#[constructor]
fn constructor(ref self: ContractState, init_params: TemplateQuestInitParams) {
self.art_peace.write(init_params.art_peace);
self.reward.write(init_params.reward);
}

#[abi(embed_v0)]
impl TemplateQuest of IQuest<ContractState> {
fn get_reward(self: @ContractState) -> u32 {
self.reward.read()
}

fn is_claimable(
self: @ContractState, user: ContractAddress, calldata: Span<felt252>
) -> bool {
if self.claimed.read(user) {
return false;
}

let template_id_felt = *calldata.at(0);
let template_id: u32 = template_id_felt.try_into().unwrap();

let template_store = ITemplateStoreDispatcher {
contract_address: self.art_peace.read()
};
let template = template_store.get_template(template_id);

if template.creator != user {
return false;
}

return true;
}

fn claim(ref self: ContractState, user: ContractAddress, calldata: Span<felt252>) -> u32 {
if get_caller_address() != self.art_peace.read() {
return 0;
}

if !self.is_claimable(user, calldata) {
return 0;
}

self.claimed.write(user, true);
let reward = self.reward.read();
self.emit(QuestClaimed { user, reward, calldata });

reward
}
}
}
3 changes: 2 additions & 1 deletion onchain/src/templates/interfaces.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub struct TemplateMetadata {
pub width: u128,
pub height: u128,
pub reward: u256,
pub reward_token: ContractAddress
pub reward_token: ContractAddress,
pub creator: ContractAddress
}

#[starknet::interface]
Expand Down
Loading

0 comments on commit 2dc9e5b

Please sign in to comment.