Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Nov 4, 2024
1 parent 4bbc766 commit e12a930
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 128 deletions.
171 changes: 82 additions & 89 deletions onchain/src/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,8 @@ pub mod ArtPeace {
}
let member_metadata = self.users_faction_meta.read(user);
if member_metadata.member_pixels > 0 {
// TODO: If member_pixels > 0 && < allocation && enough time has passed, return allocation instead of member_pixels
// TODO: If member_pixels > 0 && < allocation && enough time has passed, return
// allocation instead of member_pixels
return member_metadata.member_pixels;
} else {
let time_since_last_pixel = now - member_metadata.member_placed_time;
Expand Down Expand Up @@ -821,11 +822,10 @@ pub mod ArtPeace {
self.daily_quests.read((day_index, 0)) == zero_address, 'Daily quests already set'
);
let mut i = 0;
while i < quests
.len() {
self.daily_quests.write((day_index, i), *quests.at(i));
i += 1;
};
while i < quests.len() {
self.daily_quests.write((day_index, i), *quests.at(i));
i += 1;
};
}

fn add_main_quests(ref self: ContractState, quests: Span<ContractAddress>) {
Expand Down Expand Up @@ -1136,29 +1136,27 @@ pub mod ArtPeace {
let canvas_width = self.canvas_width.read();
let (mut x, mut y) = (0, 0);
let mut matches = 0;
while y < template_metadata
.height {
x = 0;
while x < template_metadata
.width {
let _pos = template_pos_x + x + (template_pos_y + y) * canvas_width;
let _color = *template_image
.at((x + y * template_metadata.width).try_into().unwrap());
// TODO: Check if the color is transparent
// TODO: Add back
// if color == self.canvas.read(pos).color {
if false {
matches += 1;
}
x += 1;
};
y += 1;
while y < template_metadata.height {
x = 0;
while x < template_metadata.width {
let _pos = template_pos_x + x + (template_pos_y + y) * canvas_width;
let _color = *template_image
.at((x + y * template_metadata.width).try_into().unwrap());
// TODO: Check if the color is transparent
// TODO: Add back
// if color == self.canvas.read(pos).color {
if false {
matches += 1;
}
x += 1;
};
y += 1;
};

// TODO: Allow some threshold?
if matches == template_metadata.width * template_metadata.height {
self.templates.completed_templates.write(template_id, true);
// self.emit(Event::TemplateEvent::TemplateCompleted { template_id });
// self.emit(Event::TemplateEvent::TemplateCompleted { template_id });
}
}

Expand Down Expand Up @@ -1190,73 +1188,69 @@ pub mod ArtPeace {
let canvas_width = self.canvas_width.read();
let (mut x, mut y) = (0, 0);
let mut matches = 0;
while y < template_metadata
.height {
x = 0;
while x < template_metadata
.width {
let _pos = template_pos_x + x + (template_pos_y + y) * canvas_width;
let _color = *template_image
.at((x + y * template_metadata.width).try_into().unwrap());
// TODO: Check if the color is transparent
// TODO: Add back
// if color == self.canvas.read(pos).color {
if false {
matches += 1;

let mut pixel_owner = starknet::contract_address_const::<
0
>(); // TODO: self.canvas.read(pos).owner;
let user_index = pixel_contributors_indexes.get(pixel_owner.into());

if user_index == 0 {
let new_index = pixel_contributors.len() + 1;

pixel_contributors.append(pixel_owner);
pixel_contributors_indexes
.insert(pixel_owner.into(), new_index);
total_pixels_by_user.insert(new_index.into(), 1);
} else {
let count = total_pixels_by_user.get(user_index.into());
total_pixels_by_user.insert(user_index.into(), count + 1);
}
}
x += 1;
};
y += 1;
while y < template_metadata.height {
x = 0;
while x < template_metadata.width {
let _pos = template_pos_x + x + (template_pos_y + y) * canvas_width;
let _color = *template_image
.at((x + y * template_metadata.width).try_into().unwrap());
// TODO: Check if the color is transparent
// TODO: Add back
// if color == self.canvas.read(pos).color {
if false {
matches += 1;

let mut pixel_owner = starknet::contract_address_const::<
0
>(); // TODO: self.canvas.read(pos).owner;
let user_index = pixel_contributors_indexes.get(pixel_owner.into());

if user_index == 0 {
let new_index = pixel_contributors.len() + 1;

pixel_contributors.append(pixel_owner);
pixel_contributors_indexes.insert(pixel_owner.into(), new_index);
total_pixels_by_user.insert(new_index.into(), 1);
} else {
let count = total_pixels_by_user.get(user_index.into());
total_pixels_by_user.insert(user_index.into(), count + 1);
}
}
x += 1;
};
y += 1;
};

// TODO: Allow some threshold?
if matches == template_metadata.width * template_metadata.height {
self.templates.completed_templates.write(template_id, true);
// Distribute rewards
let mut i = 0;
while i < pixel_contributors
.len() {
let reward_token = template_metadata.reward_token;
let reward_amount = template_metadata.reward;
let total_pixels_in_template = template_metadata.width
* template_metadata.height;

let mut user = *pixel_contributors.at(i).into();
let user_index = (i + 1);
let user_total_pixels = total_pixels_by_user.get(user_index.into());

// TODO: Handle remainder of funds
let user_reward = (reward_amount * user_total_pixels.into())
/ total_pixels_in_template.into();

assert(
IERC20Dispatcher { contract_address: reward_token }
.balance_of(contract) >= user_reward,
'insufficient funds'
);
let success = IERC20Dispatcher { contract_address: reward_token }
.transfer(user, user_reward);
assert(success, 'ERC20 transfer fail!');
i += 1;
};
// self.emit(Event::TemplateEvent::TemplateCompleted { template_id });
while i < pixel_contributors.len() {
let reward_token = template_metadata.reward_token;
let reward_amount = template_metadata.reward;
let total_pixels_in_template = template_metadata.width
* template_metadata.height;

let mut user = *pixel_contributors.at(i).into();
let user_index = (i + 1);
let user_total_pixels = total_pixels_by_user.get(user_index.into());

// TODO: Handle remainder of funds
let user_reward = (reward_amount * user_total_pixels.into())
/ total_pixels_in_template.into();

assert(
IERC20Dispatcher { contract_address: reward_token }
.balance_of(contract) >= user_reward,
'insufficient funds'
);
let success = IERC20Dispatcher { contract_address: reward_token }
.transfer(user, user_reward);
assert(success, 'ERC20 transfer fail!');
i += 1;
};
// self.emit(Event::TemplateEvent::TemplateCompleted { template_id });
}
}
}
Expand Down Expand Up @@ -1296,11 +1290,10 @@ pub mod ArtPeace {
// find threshold
let mut threshold: u32 = 0;
let mut min_index = daily_new_colors_count;
while threshold == 0
&& min_index > 0 {
min_index -= 1;
threshold = max_scores.get(min_index.into());
};
while threshold == 0 && min_index > 0 {
min_index -= 1;
threshold = max_scores.get(min_index.into());
};
if threshold == 0 {
// No votes
threshold = 1;
Expand Down
4 changes: 3 additions & 1 deletion onchain/src/nfts/canvas_nft.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ mod CanvasNFT {


#[constructor]
fn constructor(ref self: ContractState, name: ByteArray, symbol: ByteArray, round_number: felt252) {
fn constructor(
ref self: ContractState, name: ByteArray, symbol: ByteArray, round_number: felt252
) {
let base_uri = format!("https://api.art-peace.net/round-{}/metadata/", round_number);
self.erc721.initializer(name, symbol, base_uri);
}
Expand Down
9 changes: 4 additions & 5 deletions onchain/src/quests/authority_quest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ pub mod AuthorityQuest {
fn mark_claimable(ref self: ContractState, calldata: Span<felt252>) {
assert(get_caller_address() == self.authority.read(), 'Only authority address allowed');
let mut i = 0;
while i < calldata
.len() {
self.claimable.write((*calldata[i]).try_into().unwrap(), true);
i += 1;
}
while i < calldata.len() {
self.claimable.write((*calldata[i]).try_into().unwrap(), true);
i += 1;
}
}
}

Expand Down
17 changes: 8 additions & 9 deletions onchain/src/quests/rainbow_quest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ pub mod RainbowQuest {

let mut result = true;
let mut i = 0;
while i < art_piece
.get_color_count() {
if (art_piece.get_user_pixels_placed_color(user, i) == 0) {
result = false;
break;
}

i += 1;
};
while i < art_piece.get_color_count() {
if (art_piece.get_user_pixels_placed_color(user, i) == 0) {
result = false;
break;
}

i += 1;
};

result
}
Expand Down
2 changes: 1 addition & 1 deletion onchain/src/templates/component.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod TemplateStoreComponent {
struct TemplateCompleted {
#[key]
id: u32,
// TODO: Users rewarded, ...
// TODO: Users rewarded, ...
}

#[embeddable_as(TemplateStoreImpl)]
Expand Down
19 changes: 9 additions & 10 deletions onchain/src/tests/art_peace.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,15 @@ pub(crate) fn deploy_with_quests_contract(
let mut i = 0;
let mut dayId = 0;
let mut days_quests: Array<ContractAddress> = array![];
while i < daily_quests
.len() {
days_quests.append(*daily_quests.at(i));
i += 1;
if i % daily_quests_count == 0 {
art_peace.add_daily_quests(dayId, days_quests.span());
dayId += 1;
days_quests = array![];
}
};
while i < daily_quests.len() {
days_quests.append(*daily_quests.at(i));
i += 1;
if i % daily_quests_count == 0 {
art_peace.add_daily_quests(dayId, days_quests.span());
dayId += 1;
days_quests = array![];
}
};
if days_quests.len() > 0 {
art_peace.add_daily_quests(dayId, days_quests.span());
}
Expand Down
22 changes: 10 additions & 12 deletions onchain/src/tests/rainbow_quest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ fn rainbow_quest_test() {
);

let mut i: u8 = 0;
while i < art_peace
.get_color_count() {
art_peace.place_pixel_blocktime(i.into(), i);
warp_to_next_available_time(art_peace);
while i < art_peace.get_color_count() {
art_peace.place_pixel_blocktime(i.into(), i);
warp_to_next_available_time(art_peace);

i += 1;
};
i += 1;
};

let calldata = array![].span();
art_peace.claim_main_quest(0, calldata);
Expand All @@ -88,13 +87,12 @@ fn rainbow_quest_color_missing_test() {
);

let mut i: u8 = 0;
while i < art_peace.get_color_count()
- 1 {
art_peace.place_pixel_blocktime(i.into(), i);
warp_to_next_available_time(art_peace);
while i < art_peace.get_color_count() - 1 {
art_peace.place_pixel_blocktime(i.into(), i);
warp_to_next_available_time(art_peace);

i += 1;
};
i += 1;
};

let calldata = array![].span();
art_peace.claim_main_quest(0, calldata);
Expand Down
2 changes: 1 addition & 1 deletion onchain/src/tests/vote_quest.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn vote_quest_double_claim_test() {
art_peace_dispatcher.claim_main_quest(0, utils::EMPTY_CALLDATA());
}

// When the `votable_colors` storage variable is not set for a particular day index, trying to
// When the `votable_colors` storage variable is not set for a particular day index, trying to
// vote for a color at that day panics with 'Color out of bounds' error
#[test]
#[should_panic(expected: 'Color out of bounds')]
Expand Down

0 comments on commit e12a930

Please sign in to comment.