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

add template quest contract and check is_claimable #61

Merged
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>,
}
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved

#[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