Skip to content

Commit

Permalink
Generate packed shape from unpacked shape (still hacky for now)
Browse files Browse the repository at this point in the history
+ Fix after moving assembly/disassembly out of the contract interface
  • Loading branch information
wraitii committed Aug 22, 2023
1 parent f5dddc3 commit 2a04d4b
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 220 deletions.
23 changes: 14 additions & 9 deletions briq_protocol/gen_shape_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from dataclasses import dataclass
from typing import Dict

from numpy import integer


def generate_shape_check(shape):
materials = {}
Expand All @@ -24,20 +26,15 @@ def mat_check(materials: Dict[int, int]):
i += 1
return "\n ".join(out)

ANY_MATERIAL = 0
ANY_COLOR = 0
ANY_MATERIAL_ANY_COLOR = 0

def item_check(item):
out = [
" let shapeItem = shape.pop_front().unwrap();",
]
if item.material != ANY_COLOR:
out.append(f"assert(shapeItem.color == '{item.color}', 'bad shape item');")
if item.material != ANY_MATERIAL:
out.append(f"assert(shapeItem.material == {item.material}, 'bad shape item');")
out.append(f"assert(shapeItem.x == {item.x}, 'bad shape item');")
out.append(f"assert(shapeItem.z == {item.y}, 'bad shape item');")
out.append(f"assert(shapeItem.y == {item.z}, 'bad shape item');")
if item.material != ANY_MATERIAL_ANY_COLOR:
out.append(f"assert(shapeItem.color_material == {item.color_material}, 'bad shape item');")
out.append(f"assert(shapeItem.x_y_z == {item.x_y_z}, 'bad shape item');")
return "\n ".join(out)

@dataclass
Expand All @@ -47,3 +44,11 @@ class ShapeItem:
z: int
color: str
material: int

@property
def color_material(self):
return int.from_bytes(self.color.encode(), 'big') * 2**64 + self.material

@property
def x_y_z(self):
return (self.x + 2**31) * 2**64 + (self.y + 2**31) * 2**32 + (self.z + 2**31)
12 changes: 1 addition & 11 deletions src/attributes/attributes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ fn assign_attribute(
assert(set_owner.is_non_zero(), 'Bad input');
assert(set_token_id != 0, 'Bad input');
assert(attribute_id != 0, 'Bad input');

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');


let collection_id = get_collection_id(attribute_id);
let (admin, system) = get!(ctx.world, (collection_id), Collection).get_admin_or_system();
if admin.is_some() {
Expand Down Expand Up @@ -115,11 +110,6 @@ fn remove_attribute(
assert(set_token_id != 0, 'Bad input');
assert(attribute_id != 0, 'Bad input');

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');

let collection_id = get_collection_id(attribute_id);
let (admin, system) = get!(ctx.world, (collection_id), Collection).get_admin_or_system();
if admin.is_some() {
Expand Down
20 changes: 10 additions & 10 deletions src/set_nft/systems.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use debug::PrintTrait;
use dojo::world::Context;

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use briq_protocol::world_config::{SYSTEM_CONFIG_ID, WorldConfig};
use briq_protocol::world_config::{SYSTEM_CONFIG_ID, WorldConfig, get_world_config};
use briq_protocol::cumulative_balance::{CUM_BALANCE_TOKEN, CB_BRIQ, CB_ATTRIBUTES};

use dojo_erc::erc1155::components::OperatorApproval;
Expand Down Expand Up @@ -66,7 +66,7 @@ fn hash_token_id(owner: ContractAddress, token_id_hint: felt252, nb_briqs: u32,
}

fn create_token(ctx: Context, recipient: ContractAddress, token_id: felt252) {
let token = ctx.origin;
let token = get_world_config(ctx.world).set;
assert(recipient.is_non_zero(), 'ERC721: mint to 0');

let token_owner = get!(ctx.world, (token, token_id), ERC721Owner);
Expand All @@ -80,7 +80,7 @@ fn create_token(ctx: Context, recipient: ContractAddress, token_id: felt252) {
}

fn destroy_token(ctx: Context, owner: ContractAddress, token_id: felt252) {
let token = ctx.origin;
let token = get_world_config(ctx.world).set;
let mut balance = get!(ctx.world, (token, owner), ERC721Balance);
balance.amount -= 1;
set!(ctx.world, (balance));
Expand Down Expand Up @@ -130,10 +130,10 @@ mod set_nft_assembly {
use debug::PrintTrait;
use super::AssemblySystemData;

fn execute(ctx: Context, data: AssemblySystemData, ) {
fn execute(ctx: Context, data: AssemblySystemData) {
let AssemblySystemData{caller, owner, token_id_hint, fts, shape, attributes } = data;

assert(ctx.origin == get!(ctx.world, (SYSTEM_CONFIG_ID), WorldConfig).set, 'Only SetNft');
assert(ctx.origin == caller, 'Only Caller');

assert(shape.len() != 0, 'Cannot mint empty set');

Expand Down Expand Up @@ -171,19 +171,20 @@ mod set_nft_disassembly {
use dojo::world::Context;
use dojo_erc::erc721::components::{ERC721Owner, ERC721TokenApproval};
use dojo_erc::erc1155::components::OperatorApproval;
use briq_protocol::world_config::{SYSTEM_CONFIG_ID, WorldConfig};
use briq_protocol::world_config::{SYSTEM_CONFIG_ID, WorldConfig, get_world_config};

use briq_protocol::types::{FTSpec, PackedShapeItem};

use briq_protocol::attributes::attributes::remove_attributes;

use super::DisassemblySystemData;

fn execute(ctx: Context, data: DisassemblySystemData, ) {
fn execute(ctx: Context, data: DisassemblySystemData) {
let DisassemblySystemData{caller, owner, token_id, fts, attributes } = data;
let token = ctx.origin;

assert(token == get!(ctx.world, (SYSTEM_CONFIG_ID), WorldConfig).set, 'Only SetNft');
assert(ctx.origin == caller, 'Only Caller');

let token = get_world_config(ctx.world).set;

let token_owner = get!(ctx.world, (token, token_id), ERC721Owner);
assert(token_owner.address.is_non_zero(), 'ERC721: invalid token_id');
Expand Down Expand Up @@ -223,7 +224,6 @@ fn assemble(
attributes: Array<felt252>
) -> felt252 {
// The name/description is unused except to have them show up in calldata.

let nb_briq = shape.len();

let mut calldata: Array<felt252> = ArrayTrait::new();
Expand Down
45 changes: 7 additions & 38 deletions src/tests/shapes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
mod test_shape_1 {
use array::{SpanTrait, ArrayTrait};
use option::OptionTrait;
use briq_protocol::types::{ShapeItem, FTSpec};
use briq_protocol::types::{PackedShapeItem, FTSpec};
use briq_protocol::check_shape::IShapeChecker;

#[storage]
Expand All @@ -13,58 +13,27 @@ mod test_shape_1 {
#[external(v0)]
impl ShapeChecker of IShapeChecker<ContractState> {
fn verify_shape(
self: @ContractState, token_id: felt252, shape: Span<ShapeItem>, fts: Span<FTSpec>
self: @ContractState, token_id: felt252, shape: Span<PackedShapeItem>, fts: Span<FTSpec>
) {
if token_id == 1 {
basic_test_shape(shape, fts);
}
}
}

fn basic_test_shape(mut shape: Span<ShapeItem>, mut fts: Span<FTSpec>, ) {
fn basic_test_shape(mut shape: Span<PackedShapeItem>, mut fts: Span<FTSpec>, ) {
assert(shape.len() == 4, 'bad shape length');
assert(fts.len() == 1, 'bad ft spec');
assert(fts.at(0).token_id == @1, 'bad ft spec');
assert(fts.at(0).qty == @4, 'bad ft spec');

let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.x == 1, 'bad shape item');
assert(shapeItem.y == 4, 'bad shape item');
assert(shapeItem.z == -2, 'bad shape item');
assert(shapeItem.x_y_z == 0x200000003fffffffe, 'bad shape item');
let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.x == 2, 'bad shape item');
assert(shapeItem.y == 4, 'bad shape item');
assert(shapeItem.z == -2, 'bad shape item');
assert(shapeItem.x_y_z == 0x300000003fffffffe, 'bad shape item');
let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.x == 3, 'bad shape item');
assert(shapeItem.y == 4, 'bad shape item');
assert(shapeItem.z == -2, 'bad shape item');
assert(shapeItem.x_y_z == 0x400000003fffffffe, 'bad shape item');
let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.x == 4, 'bad shape item');
assert(shapeItem.y == 4, 'bad shape item');
assert(shapeItem.z == -2, 'bad shape item');
}

fn check_basic_test_shape(mut shape: Span<ShapeItem>, mut fts: Span<FTSpec>, ) {
assert(shape.len() == 3, 'bad shape length');
assert(fts.len() == 2, 'bad ft spec');
assert(fts.at(0).token_id == @0, 'bad ft spec');
assert(fts.at(1).token_id == @1, 'bad ft spec');
assert(fts.at(0).qty == @2, 'bad ft spec');
assert(fts.at(1).qty == @1, 'bad ft spec');

let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.x == 0, 'bad shape item');
assert(shapeItem.y == 0, 'bad shape item');
assert(shapeItem.z == 0, 'bad shape item');
let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.x == 0, 'bad shape item');
assert(shapeItem.y == 2, 'bad shape item');
assert(shapeItem.z == 0, 'bad shape item');
let shapeItem = *shape.pop_front().unwrap();
assert(shapeItem.material == 1, 'bad shape item');
assert(shapeItem.x == 0, 'bad shape item');
assert(shapeItem.y == 2, 'bad shape item');
assert(shapeItem.z == 2, 'bad shape item');
assert(shapeItem.x_y_z == 0x500000003fffffffe, 'bad shape item');
}
}
2 changes: 0 additions & 2 deletions src/tests/test_attributes.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use briq_protocol::set_nft::ISetNftDispatcherTrait;
use traits::{Into, TryInto, Default};
use option::{Option, OptionTrait};
use result::ResultTrait;
Expand All @@ -10,7 +9,6 @@ use briq_protocol::world_config::{WorldConfig, SYSTEM_CONFIG_ID};
use briq_protocol::tests::test_utils::deploy_default_world;

use dojo_erc::erc721::interface::IERC721DispatcherTrait;
use briq_protocol::set_nft::ISetNftDispatcher;

use briq_protocol::types::{FTSpec, ShapeItem};

Expand Down
2 changes: 0 additions & 2 deletions src/tests/test_box_nft.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use briq_protocol::set_nft::ISetNftDispatcherTrait;
use traits::{Into, TryInto, Default};
use option::{Option, OptionTrait};
use result::ResultTrait;
Expand All @@ -14,7 +13,6 @@ use briq_protocol::tests::test_utils::{WORLD_ADMIN, DefaultWorld, deploy_default

use dojo_erc::erc721::interface::IERC721DispatcherTrait;
use dojo_erc::erc1155::interface::IERC1155DispatcherTrait;
use briq_protocol::set_nft::ISetNftDispatcher;

use briq_protocol::attributes::collection::CreateCollectionData;
use briq_protocol::check_shape::RegisterShapeVerifierData;
Expand Down
Loading

0 comments on commit 2a04d4b

Please sign in to comment.