Skip to content

Commit

Permalink
Scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wraitii committed Aug 18, 2023
1 parent 8899b51 commit 10c74d6
Show file tree
Hide file tree
Showing 21 changed files with 802 additions and 560 deletions.
3 changes: 2 additions & 1 deletion src/attributes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mod collection;
use traits::{Into, TryInto};
use option::OptionTrait;

const COLLECTION_ID_MASK: felt252 = 0xffffffffffffffffffffffffffffffffffffffffffffffff; // 2**192 - 1;
const COLLECTION_ID_MASK: felt252 =
0xffffffffffffffffffffffffffffffffffffffffffffffff; // 2**192 - 1;

fn get_collection_id(attribute_id: felt252) -> felt252 {
let collection_id = Into::<felt252, u256>::into(attribute_id) & COLLECTION_ID_MASK.into();
Expand Down
51 changes: 32 additions & 19 deletions src/attributes/attributes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ fn assign_attributes(
shape: @Array<ShapeItem>,
fts: @Array<FTSpec>,
) {
loop
{
loop {
if (attributes.len() == 0) {
break ();
}
Expand All @@ -68,7 +67,7 @@ fn assign_attribute(
let caller = ctx.origin;
let set_addr = get!(ctx.world, (SYSTEM_CONFIG_ID), WorldConfig).set;
// TODO: Set permissions on the collection (owner / set) ?
assert (caller == set_addr, 'Bad caller');
assert(caller == set_addr, 'Bad caller');

let collection_id = get_collection_id(attribute_id);
let (admin, system) = get!(ctx.world, (collection_id), Collection).get_admin_or_system();
Expand All @@ -77,24 +76,31 @@ fn assign_attribute(
assert(0 == 1, 'TODO');
} else {
let shape_verifier = get!(ctx.world, (attribute_id), ShapeVerifier);
shape_verifier.assign_attribute(ctx.world, set_owner, set_token_id, attribute_id, shape, fts);
shape_verifier
.assign_attribute(ctx.world, set_owner, set_token_id, attribute_id, shape, fts);
}
emit!(ctx.world, AttributeAssigned { set_token_id: set_token_id.into(), attribute_id });

// Update the cumulative balance
let balance = get!(ctx.world, (CUM_BALANCE_TOKEN(), CB_ATTRIBUTES, set_token_id), ERC1155Balance).amount;
let balance = get!(
ctx.world, (CUM_BALANCE_TOKEN(), CB_ATTRIBUTES, set_token_id), ERC1155Balance
)
.amount;
assert(balance < balance + 1, 'Balance overflow');
set!(ctx.world, ERC1155Balance { token: CUM_BALANCE_TOKEN(), token_id: CB_ATTRIBUTES, account: set_token_id.try_into().unwrap(), amount: balance + 1 });
set!(
ctx.world, ERC1155Balance {
token: CUM_BALANCE_TOKEN(),
token_id: CB_ATTRIBUTES,
account: set_token_id.try_into().unwrap(),
amount: balance + 1
}
);
}

fn remove_attributes(
ctx: Context,
set_owner: ContractAddress,
set_token_id: felt252,
mut attributes: Array<felt252>
ctx: Context, set_owner: ContractAddress, set_token_id: felt252, mut attributes: Array<felt252>
) {
loop
{
loop {
if (attributes.len() == 0) {
break ();
}
Expand All @@ -103,10 +109,7 @@ fn remove_attributes(
}

fn remove_attribute(
ctx: Context,
set_owner: ContractAddress,
set_token_id: felt252,
attribute_id: felt252,
ctx: Context, set_owner: ContractAddress, set_token_id: felt252, attribute_id: felt252,
) {
assert(set_owner.is_non_zero(), 'Bad input');
assert(set_token_id != 0, 'Bad input');
Expand All @@ -115,7 +118,7 @@ fn remove_attribute(
let caller = ctx.origin;
let set_addr = get!(ctx.world, (SYSTEM_CONFIG_ID), WorldConfig).set;
// TODO: Set permissions on the collection (owner / set) ?
assert (caller == set_addr, 'Bad caller');
assert(caller == set_addr, 'Bad caller');

let collection_id = get_collection_id(attribute_id);
let (admin, system) = get!(ctx.world, (collection_id), Collection).get_admin_or_system();
Expand All @@ -130,7 +133,17 @@ fn remove_attribute(
emit!(ctx.world, AttributeRemoved { set_token_id: set_token_id.into(), attribute_id });

// Update the cumulative balance
let balance = get!(ctx.world, (CUM_BALANCE_TOKEN(), CB_ATTRIBUTES, set_token_id), ERC1155Balance).amount;
let balance = get!(
ctx.world, (CUM_BALANCE_TOKEN(), CB_ATTRIBUTES, set_token_id), ERC1155Balance
)
.amount;
assert(balance > balance - 1, 'Balance underflow');
set!(ctx.world, ERC1155Balance { token: CUM_BALANCE_TOKEN(), token_id: CB_ATTRIBUTES, account: set_token_id.try_into().unwrap(), amount: balance - 1 });
set!(
ctx.world, ERC1155Balance {
token: CUM_BALANCE_TOKEN(),
token_id: CB_ATTRIBUTES,
account: set_token_id.try_into().unwrap(),
amount: balance - 1
}
);
}
49 changes: 33 additions & 16 deletions src/attributes/collection.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use briq_protocol::felt_math::{feltBitAnd, feltOrd};
struct Collection {
#[key]
collection_id: u64,

parameters: felt252, // bitfield
admin_or_system: ContractAddress,
}

#[generate_trait]
impl CollectionImpl of CollectionTrait {
fn get_admin_or_system(self: @Collection) -> (Option<ContractAddress>, Option<ContractAddress>) {
fn get_admin_or_system(
self: @Collection
) -> (Option<ContractAddress>, Option<ContractAddress>) {
let a_o_c = self.admin_or_system;
if *self.parameters & CONTRACT_BIT == 0 {
return (Option::Some(*a_o_c), Option::None);
Expand All @@ -31,12 +32,16 @@ impl CollectionImpl of CollectionTrait {
}
}

fn new_collection(collection_id: u64, params: felt252, admin_or_system: ContractAddress) -> Collection {
fn new_collection(
collection_id: u64, params: felt252, admin_or_system: ContractAddress
) -> Collection {
let existence_bit_toggled = params & EXISTS_BIT;
// Probably indicates an error, fail.
assert(existence_bit_toggled == 0, 'Invalid bits');
assert(params < 0x400000000000000000000000000000000000000000000000000000000000000, 'Invalid bits');

assert(
params < 0x400000000000000000000000000000000000000000000000000000000000000, 'Invalid bits'
);

Collection {
collection_id: collection_id,
parameters: params + EXISTS_BIT,
Expand All @@ -45,8 +50,7 @@ fn new_collection(collection_id: u64, params: felt252, admin_or_system: Contract
}

#[derive(Clone, Drop, Serde)]
struct CreateCollectionData
{
struct CreateCollectionData {
collection_id: u64,
params: felt252,
admin_or_system: ContractAddress
Expand Down Expand Up @@ -85,28 +89,41 @@ mod create_collection {
parameters: felt252
}

fn execute(
ctx: Context,
data: CreateCollectionData,
) {
let CreateCollectionData { collection_id, params, admin_or_system } = data;
fn execute(ctx: Context, data: CreateCollectionData, ) {
let CreateCollectionData{collection_id, params, admin_or_system } = data;

assert(admin_or_system.is_non_zero(), 'Must have admin');

// TODO: check ctx.origin is actually the origin
ctx.world.only_admins(@ctx.origin);

assert(get!(ctx.world, (collection_id), Collection).parameters == 0, 'Collec already exists');
assert(
get!(ctx.world, (collection_id), Collection).parameters == 0, 'Collec already exists'
);

let collec = new_collection(collection_id, params, admin_or_system);

set!(ctx.world, (collec));

let (admin, system) = collec.get_admin_or_system();
if admin.is_some() {
emit!(ctx.world, CollectionCreated { collection_id, system: Zeroable::zero(), admin: admin.unwrap(), parameters:params });
emit!(
ctx.world, CollectionCreated {
collection_id,
system: Zeroable::zero(),
admin: admin.unwrap(),
parameters: params
}
);
} else {
emit!(ctx.world, CollectionCreated { collection_id, system: system.unwrap(), admin: Zeroable::zero(), parameters:params });
emit!(
ctx.world, CollectionCreated {
collection_id,
system: system.unwrap(),
admin: Zeroable::zero(),
parameters: params
}
);
}
}
}
2 changes: 1 addition & 1 deletion src/box_nft.cairo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod unboxing;
mod unboxing;
96 changes: 65 additions & 31 deletions src/box_nft/unboxing.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,83 @@ use briq_protocol::world_config::{get_world_config, AdminTrait};
#[derive(Drop, Copy, Serde)]
struct BoxData {
briq_1: felt252, // nb of briqs of material 0x1
shape_class_hash: felt252, // Class hash of the matching shape contract
shape_class_hash: felt252, // Class hash of the matching shape contract
}

fn get_number_of_briqs_in_box(box_id: felt252) -> u128
{
fn get_number_of_briqs_in_box(box_id: felt252) -> u128 {
// TODO: use match once that's supported
if box_id == 1 { return 434; }
if box_id == 2 { return 1252; }
if box_id == 3 { return 2636; }
if box_id == 4 { return 431; }
if box_id == 5 { return 1246; }
if box_id == 6 { return 2287; }
if box_id == 7 { return 431; }
if box_id == 8 { return 1286; }
if box_id == 9 { return 2392; }
if box_id == 10 { return 60; } // briqmas
if box_id == 1 {
return 434;
}
if box_id == 2 {
return 1252;
}
if box_id == 3 {
return 2636;
}
if box_id == 4 {
return 431;
}
if box_id == 5 {
return 1246;
}
if box_id == 6 {
return 2287;
}
if box_id == 7 {
return 431;
}
if box_id == 8 {
return 1286;
}
if box_id == 9 {
return 2392;
}
if box_id == 10 {
return 60;
} // briqmas
assert(false, 'bad box id');
0
}

fn get_booklet_id_for_box(box_id: felt252) -> felt252
{
fn get_booklet_id_for_box(box_id: felt252) -> felt252 {
// TODO: use match once that's supported
if box_id == 1 { return 0x1000000000000000000000000000000000000000000000001; }
if box_id == 2 { return 0x2000000000000000000000000000000000000000000000001; }
if box_id == 3 { return 0x3000000000000000000000000000000000000000000000001; }
if box_id == 4 { return 0x4000000000000000000000000000000000000000000000001; }
if box_id == 5 { return 0x5000000000000000000000000000000000000000000000001; }
if box_id == 6 { return 0x6000000000000000000000000000000000000000000000001; }
if box_id == 7 { return 0x7000000000000000000000000000000000000000000000001; }
if box_id == 8 { return 0x8000000000000000000000000000000000000000000000001; }
if box_id == 9 { return 0x9000000000000000000000000000000000000000000000001; }
if box_id == 1 {
return 0x1000000000000000000000000000000000000000000000001;
}
if box_id == 2 {
return 0x2000000000000000000000000000000000000000000000001;
}
if box_id == 3 {
return 0x3000000000000000000000000000000000000000000000001;
}
if box_id == 4 {
return 0x4000000000000000000000000000000000000000000000001;
}
if box_id == 5 {
return 0x5000000000000000000000000000000000000000000000001;
}
if box_id == 6 {
return 0x6000000000000000000000000000000000000000000000001;
}
if box_id == 7 {
return 0x7000000000000000000000000000000000000000000000001;
}
if box_id == 8 {
return 0x8000000000000000000000000000000000000000000000001;
}
if box_id == 9 {
return 0x9000000000000000000000000000000000000000000000001;
}
// TODO warning delta migration
if box_id == 10 { return 0x1000000000000000000000000000000000000000000000002; } // briqmas
if box_id == 10 {
return 0x1000000000000000000000000000000000000000000000002;
} // briqmas
assert(false, 'Invalid box id');
0
}

fn unbox(world: IWorldDispatcher, owner: ContractAddress, box_id: felt252)
{
fn unbox(world: IWorldDispatcher, owner: ContractAddress, box_id: felt252) {
// Burn the box
// TODO: use event-emitting variant
dojo_erc::erc1155::components::ERC1155BalanceTrait::transfer_tokens(
Expand Down Expand Up @@ -101,10 +138,7 @@ mod box_unboxing {
use zeroable::Zeroable;
use starknet::ContractAddress;

fn execute(
ctx: Context,
box_id: felt252,
) {
fn execute(ctx: Context, box_id: felt252, ) {
// Only the owner may unbox their box.
super::unbox(ctx.world, ctx.origin, box_id);
}
Expand Down
Loading

0 comments on commit 10c74d6

Please sign in to comment.