-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add template quest contract and check is_claimable (#61)
* 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
1 parent
e7ce8c1
commit 2dc9e5b
Showing
6 changed files
with
238 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.