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

Various improvements of the Scarb package #26

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.6.3
starknet-foundry 0.20.0
starknet-foundry 0.21.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update the Dockerfiles to reflect this change, like here

RUN /bin/bash /root/.local/bin/snfoundryup --version 0.20.0

Either manually change them to 0.21.0 or set up a way for the Dockerfile to properly import the changed value if you'd find that interesting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b-j-roberts will change manually for now if this is ok for you

2 changes: 1 addition & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/instal
RUN curl -L https://raw.githubusercontent.com/foundry-rs/starknet-foundry/master/scripts/install.sh | bash
# TODO: Source not working properly & requiring manual /root/.local/bin/ paths
RUN source /root/.profile
RUN /bin/bash /root/.local/bin/snfoundryup --version 0.20.0
RUN /bin/bash /root/.local/bin/snfoundryup --version 0.21.0

# Copy over the configs
WORKDIR /configs
Expand Down
1 change: 1 addition & 0 deletions onchain/Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "art_peace"
version = "0.1.0"
edition = "2023_11"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

Expand Down
25 changes: 17 additions & 8 deletions onchain/src/art_peace.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#[starknet::contract]
pub mod ArtPeace {
use starknet::ContractAddress;

use art_peace::{IArtPeace, Pixel};
use art_peace::quests::{IQuestDispatcher, IQuestDispatcherTrait};
use art_peace::quests::interfaces::{IQuestDispatcher, IQuestDispatcherTrait};
use art_peace::templates::component::TemplateStoreComponent;
use art_peace::templates::{ITemplateVerifier, TemplateMetadata};
use art_peace::templates::interfaces::{ITemplateVerifier, TemplateMetadata};

component!(path: TemplateStoreComponent, storage: templates, event: TemplateEvent);

Expand Down Expand Up @@ -242,6 +243,7 @@ pub mod ArtPeace {
colors.append(self.color_palette.read(i));
i += 1;
};

colors
}

Expand Down Expand Up @@ -285,6 +287,7 @@ pub mod ArtPeace {
quests.append(self.daily_quests.read((day_index, i)));
i += 1;
};

quests.span()
}

Expand All @@ -297,6 +300,7 @@ pub mod ArtPeace {
quests.append(self.daily_quests.read((day, i)));
i += 1;
};

quests.span()
}

Expand All @@ -316,16 +320,19 @@ pub mod ArtPeace {
quests.append(self.main_quests.read(i));
i += 1;
};

quests.span()
}

fn claim_daily_quest(ref self: ContractState, day_index: u32, quest_id: u32) {
fn claim_daily_quest(
ref self: ContractState, day_index: u32, quest_id: u32, calldata: Span<felt252>
) {
let now = starknet::get_block_timestamp();
assert!(now <= self.end_time.read());
// TODO: Only allow to claim the quest of the current day
let quest = self.daily_quests.read((day_index, quest_id));
let user = starknet::get_caller_address();
let reward = IQuestDispatcher { contract_address: quest }.claim(user);
let reward = IQuestDispatcher { contract_address: quest }.claim(user, calldata);
if reward > 0 {
self
.extra_pixels
Expand All @@ -336,12 +343,12 @@ pub mod ArtPeace {
}
}

fn claim_today_quest(ref self: ContractState, quest_id: u32) {
fn claim_today_quest(ref self: ContractState, quest_id: u32, calldata: Span<felt252>) {
let now = starknet::get_block_timestamp();
assert!(now <= self.end_time.read());
let quest = self.daily_quests.read((self.day_index.read(), quest_id));
let user = starknet::get_caller_address();
let reward = IQuestDispatcher { contract_address: quest }.claim(user);
let reward = IQuestDispatcher { contract_address: quest }.claim(user, calldata);
if reward > 0 {
self
.extra_pixels
Expand All @@ -352,12 +359,12 @@ pub mod ArtPeace {
}
}

fn claim_main_quest(ref self: ContractState, quest_id: u32) {
fn claim_main_quest(ref self: ContractState, quest_id: u32, calldata: Span<felt252>) {
let now = starknet::get_block_timestamp();
assert!(now <= self.end_time.read());
let quest = self.main_quests.read(quest_id);
let user = starknet::get_caller_address();
let reward = IQuestDispatcher { contract_address: quest }.claim(user);
let reward = IQuestDispatcher { contract_address: quest }.claim(user, calldata);
if reward > 0 {
self
.extra_pixels
Expand All @@ -381,6 +388,7 @@ pub mod ArtPeace {
};
i += 1;
};

total
}

Expand All @@ -394,6 +402,7 @@ pub mod ArtPeace {
total += self.user_pixels_placed.read((day, user, i));
i += 1;
};

total
}

Expand Down
12 changes: 7 additions & 5 deletions onchain/src/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[derive(Drop, Serde, starknet::Store)]
pub struct Pixel {
// Color index in the palette
color: u8,
pub color: u8,
// The person that placed the pixel
owner: starknet::ContractAddress,
pub owner: starknet::ContractAddress,
}

// TODO: Tests for all
Expand Down Expand Up @@ -56,9 +56,11 @@ pub trait IArtPeace<TContractState> {
fn get_main_quests(self: @TContractState) -> Span<starknet::ContractAddress>;

// Claim quests
fn claim_daily_quest(ref self: TContractState, day_index: u32, quest_id: u32);
fn claim_today_quest(ref self: TContractState, quest_id: u32);
fn claim_main_quest(ref self: TContractState, quest_id: u32);
fn claim_daily_quest(
ref self: TContractState, day_index: u32, quest_id: u32, calldata: Span<felt252>
);
fn claim_today_quest(ref self: TContractState, quest_id: u32, calldata: Span<felt252>);
fn claim_main_quest(ref self: TContractState, quest_id: u32, calldata: Span<felt252>);

// Stats
fn get_user_pixels_placed(self: @TContractState, user: starknet::ContractAddress) -> u32;
Expand Down
10 changes: 5 additions & 5 deletions onchain/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ use art_peace::ArtPeace;
use interface::{IArtPeace, IArtPeaceDispatcher, IArtPeaceDispatcherTrait, Pixel};

mod quests {
pub mod interface;
pub mod interfaces;
mod pixel_quest;

use interface::{IQuest, QuestClaimed, IQuestDispatcher, IQuestDispatcherTrait};
use interfaces::{IQuest, IQuestDispatcher, IQuestDispatcherTrait, QuestClaimed};
}

mod templates {
pub mod interface;
mod component;
pub mod interfaces;
pub mod component;

use interface::{
use interfaces::{
TemplateMetadata, ITemplateVerifier, ITemplateStoreDispatcher,
ITemplateStoreDispatcherTrait, ITemplateVerifierDispatcher, ITemplateVerifierDispatcherTrait
};
Expand Down
17 changes: 0 additions & 17 deletions onchain/src/quests/interface.cairo

This file was deleted.

26 changes: 26 additions & 0 deletions onchain/src/quests/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use starknet::ContractAddress;

#[derive(Drop, starknet::Event)]
pub struct QuestClaimed {
pub user: ContractAddress,
pub reward: u32,
pub calldata: Span<felt252>,
}

#[starknet::interface]
pub trait IQuest<TContractState> {
// Return the reward for the quest.
fn get_reward(self: @TContractState) -> u32;
// Return if the user can claim the quest.
fn is_claimable(self: @TContractState, user: ContractAddress, calldata: Span<felt252>) -> bool;
// Claim the quest.
fn claim(ref self: TContractState, user: ContractAddress, calldata: Span<felt252>) -> u32;
}

#[starknet::interface]
pub trait IPixelQuest<TContractState> {
fn is_claimed(self: @TContractState, user: starknet::ContractAddress) -> bool;
fn get_pixels_needed(self: @TContractState) -> u32;
fn is_daily(self: @TContractState) -> bool;
fn claim_day(self: @TContractState) -> u32;
}
43 changes: 20 additions & 23 deletions onchain/src/quests/pixel_quest.cairo
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
#[starknet::interface]
trait IPixelQuest<TContractState> {
fn is_claimed(self: @TContractState, user: starknet::ContractAddress) -> bool;
fn get_pixels_needed(self: @TContractState) -> u32;
fn is_daily(self: @TContractState) -> bool;
fn claim_day(self: @TContractState) -> u32;
}

#[starknet::contract]
mod PixelQuest {
pub mod PixelQuest {
use starknet::{ContractAddress, get_caller_address};
use art_peace::{IArtPeaceDispatcher, IArtPeaceDispatcherTrait};
use art_peace::quests::{IQuest, QuestClaimed};
use super::IPixelQuest;
use art_peace::quests::interfaces::{IQuest, IPixelQuest, QuestClaimed};

#[storage]
struct Storage {
Expand Down Expand Up @@ -49,62 +40,68 @@ mod PixelQuest {
#[abi(embed_v0)]
impl PixelQuestImpl of IPixelQuest<ContractState> {
fn is_claimed(self: @ContractState, user: ContractAddress) -> bool {
return self.claimed.read(user);
self.claimed.read(user)
}

fn get_pixels_needed(self: @ContractState) -> u32 {
return self.pixels_needed.read();
self.pixels_needed.read()
}

fn is_daily(self: @ContractState) -> bool {
return self.is_daily.read();
self.is_daily.read()
}

fn claim_day(self: @ContractState) -> u32 {
return self.claim_day.read();
self.claim_day.read()
}
}

// TODO: Test all
#[abi(embed_v0)]
impl PixelQuest of IQuest<ContractState> {
fn get_reward(self: @ContractState) -> u32 {
return self.reward.read();
self.reward.read()
}

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

if self.is_daily.read() {
// Daily Pixel Quest
let day = art_peace.get_day();
if day != self.claim_day.read() {
return false;
}
let placement_count = art_peace.get_user_pixels_placed_day(user, day);
return placement_count >= self.pixels_needed.read();

placement_count >= self.pixels_needed.read()
} else {
// Main Pixel Quest
let placement_count = art_peace.get_user_pixels_placed(user);
return placement_count >= self.pixels_needed.read();

placement_count >= self.pixels_needed.read()
}
}

fn claim(ref self: ContractState, user: ContractAddress) -> u32 {
fn claim(ref self: ContractState, user: ContractAddress, calldata: Span<felt252>) -> u32 {
assert(
get_caller_address() == self.art_peace.read().contract_address,
'Only ArtPeace can claim quests'
);
if !self.is_claimable(user) {
if !self.is_claimable(user, calldata) {
return 0;
}

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

reward
}
}
}
15 changes: 8 additions & 7 deletions onchain/src/templates/component.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[starknet::component]
mod TemplateStoreComponent {
use art_peace::templates::interface::{ITemplateStore, TemplateMetadata};
pub mod TemplateStoreComponent {
use art_peace::templates::interfaces::{ITemplateStore, TemplateMetadata};

#[storage]
struct Storage {
Expand All @@ -13,7 +13,7 @@ mod TemplateStoreComponent {

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
pub enum Event {
TemplateAdded: TemplateAdded,
TemplateCompleted: TemplateCompleted,
}
Expand All @@ -37,18 +37,19 @@ mod TemplateStoreComponent {
TContractState, +HasComponent<TContractState>
> of ITemplateStore<ComponentState<TContractState>> {
fn get_templates_count(self: @ComponentState<TContractState>) -> u32 {
return self.templates_count.read();
self.templates_count.read()
}

fn get_template(
self: @ComponentState<TContractState>, template_id: u32
) -> TemplateMetadata {
return self.templates.read(template_id);
self.templates.read(template_id)
}

fn get_template_hash(self: @ComponentState<TContractState>, template_id: u32) -> felt252 {
let metadata: TemplateMetadata = self.templates.read(template_id);
return metadata.hash;

metadata.hash
}

// TODO: Return idx of the template?
Expand All @@ -62,7 +63,7 @@ mod TemplateStoreComponent {
}

fn is_template_complete(self: @ComponentState<TContractState>, template_id: u32) -> bool {
return self.completed_templates.read(template_id);
self.completed_templates.read(template_id)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[derive(Drop, Copy, Serde, starknet::Store)]
pub struct TemplateMetadata {
hash: felt252,
position: u128,
width: u128,
height: u128,
reward: u256,
reward_token: starknet::ContractAddress
pub hash: felt252,
pub position: u128,
pub width: u128,
pub height: u128,
pub reward: u256,
pub reward_token: starknet::ContractAddress
}

#[starknet::interface]
Expand Down
Loading