From f74bee90e6551aa27aad74204c21811f43afb56e Mon Sep 17 00:00:00 2001 From: notV4l Date: Fri, 6 Oct 2023 12:49:27 +0200 Subject: [PATCH 1/5] chore: update to latest dojo --- Scarb.toml | 2 +- src/components.cairo | 20 ++++++++++---------- src/systems/raw_contract.cairo | 22 ++++++++++++++-------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Scarb.toml b/Scarb.toml index f8f0e13..b00fc9d 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -7,7 +7,7 @@ version = "0.1.0" sierra-replace-ids = true [dependencies] -dojo = { git = "https://github.com/dojoengine/dojo", tag = "nightly-97162926c52d9c1ad294267462e217c09ea01477" } +dojo = { git = "https://github.com/dojoengine/dojo", tag = "nightly-e96f08d6302b2ce1277bc13b17a0723f74cb6177" } [[target.dojo]] diff --git a/src/components.cairo b/src/components.cairo index 0fa9761..2532ead 100644 --- a/src/components.cairo +++ b/src/components.cairo @@ -2,7 +2,7 @@ use array::ArrayTrait; use core::debug::PrintTrait; use starknet::ContractAddress; use dojo::database::schema::{ - EnumMember, Member, Ty, Struct, SchemaIntrospection, serialize_member, serialize_member_type + Enum, Member, Ty, Struct, SchemaIntrospection, serialize_member, serialize_member_type }; #[derive(Serde, Copy, Drop)] @@ -28,15 +28,15 @@ impl DirectionSchemaIntrospectionImpl of SchemaIntrospection { #[inline(always)] fn ty() -> Ty { Ty::Enum( - EnumMember { + Enum { name: 'Direction', attrs: array![].span(), - values: array![ - serialize_member_type(@Ty::Simple('None')), - serialize_member_type(@Ty::Simple('Left')), - serialize_member_type(@Ty::Simple('Right')), - serialize_member_type(@Ty::Simple('Up')), - serialize_member_type(@Ty::Simple('Down')) + children: array![ + ('None', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Left', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Right', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Up', serialize_member_type(@Ty::Tuple(array![].span()))), + ('Down', serialize_member_type(@Ty::Tuple(array![].span()))), ] .span() } @@ -68,7 +68,7 @@ impl DirectionIntoFelt252 of Into { } } -#[derive(Component, Copy, Drop, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Moves { #[key] player: ContractAddress, @@ -82,7 +82,7 @@ struct Vec2 { y: u32 } -#[derive(Component, Copy, Drop, Print, Serde)] +#[derive(Model, Copy, Drop, Print, Serde)] struct Position { #[key] player: ContractAddress, diff --git a/src/systems/raw_contract.cairo b/src/systems/raw_contract.cairo index 26e7513..d31cae1 100644 --- a/src/systems/raw_contract.cairo +++ b/src/systems/raw_contract.cairo @@ -4,8 +4,8 @@ use dojo_examples::components::{Direction}; // trait: specify functions to implement #[starknet::interface] trait IPlayerActions { - fn spawn(self: @TContractState, world: IWorldDispatcher); - fn move(self: @TContractState, world: IWorldDispatcher, direction: Direction); + fn spawn(self: @TContractState); + fn move(self: @TContractState, direction: Direction); } // exact same functionality as examples/ecs/src/systems/with_decorator.cairo @@ -19,7 +19,9 @@ mod player_actions_external { use super::IPlayerActions; #[storage] - struct Storage {} + struct Storage { + world_dispatcher: ContractAddress, + } #[event] #[derive(Drop, starknet::Event)] @@ -36,7 +38,8 @@ mod player_actions_external { // impl: implement functions specified in trait #[external(v0)] impl PlayerActionsImpl of IPlayerActions { - fn spawn(self: @ContractState, world: IWorldDispatcher) { + fn spawn(self: @ContractState) { + let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -48,7 +51,8 @@ mod player_actions_external { ); } - fn move(self: @ContractState, world: IWorldDispatcher, direction: Direction) { + fn move(self: @ContractState, direction: Direction) { + let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; @@ -65,6 +69,7 @@ mod player_actions_external { mod tests { use core::traits::Into; use array::{ArrayTrait}; + use starknet::class_hash::Felt252TryIntoClassHash; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use dojo::test_utils::{spawn_test_world, deploy_contract}; @@ -88,12 +93,13 @@ mod tests { let world = spawn_test_world(components); // deploy systems contract - let contract_address = deploy_contract(player_actions::TEST_CLASS_HASH, array![].span()); + let contract_address = world + .deploy_contract('salt', player_actions::TEST_CLASS_HASH.try_into().unwrap()); let player_actions_system = IPlayerActionsDispatcher { contract_address }; // System calls - player_actions_system.spawn(world); - player_actions_system.move(world, Direction::Right(())); + player_actions_system.spawn(); + player_actions_system.move(Direction::Right(())); let moves = get!(world, caller, Moves); let right_dir_felt: felt252 = Direction::Right(()).into(); From 502f57f0dfed31d3f684084eed9e2b3e2195de48 Mon Sep 17 00:00:00 2001 From: notV4l Date: Fri, 6 Oct 2023 13:13:35 +0200 Subject: [PATCH 2/5] update test.yaml --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index cd7cc39..7ca86b3 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: steps: - uses: actions/checkout@v3 - run: curl -L https://install.dojoengine.org | bash - - run: /home/runner/.config/.dojo/bin/dojoup + - run: /home/runner/.config/.dojo/bin/dojoup -v nightly-e96f08d6302b2ce1277bc13b17a0723f74cb6177 - run: | /home/runner/.config/.dojo/bin/sozo build /home/runner/.config/.dojo/bin/sozo test \ No newline at end of file From 3b8068b1bdc9a0f2326990a2123453f1bb5ea853 Mon Sep 17 00:00:00 2001 From: notV4l Date: Sat, 7 Oct 2023 12:58:24 +0200 Subject: [PATCH 3/5] update with_decorator & components -> models --- .github/workflows/test.yaml | 2 +- src/lib.cairo | 2 +- src/{components.cairo => models.cairo} | 0 src/systems/raw_contract.cairo | 16 ++++++------- src/systems/with_decorator.cairo | 33 ++++++++++++++------------ src/utils.cairo | 2 +- 6 files changed, 29 insertions(+), 26 deletions(-) rename src/{components.cairo => models.cairo} (100%) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7ca86b3..848650f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,7 +10,7 @@ jobs: steps: - uses: actions/checkout@v3 - run: curl -L https://install.dojoengine.org | bash - - run: /home/runner/.config/.dojo/bin/dojoup -v nightly-e96f08d6302b2ce1277bc13b17a0723f74cb6177 + - run: /home/runner/.config/.dojo/bin/dojoup -v v0.3.0-rc4 - run: | /home/runner/.config/.dojo/bin/sozo build /home/runner/.config/.dojo/bin/sozo test \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo index 1a585cb..b0a80e5 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,4 +1,4 @@ -mod components; +mod models; mod systems { // example with #[system] decorator diff --git a/src/components.cairo b/src/models.cairo similarity index 100% rename from src/components.cairo rename to src/models.cairo diff --git a/src/systems/raw_contract.cairo b/src/systems/raw_contract.cairo index d31cae1..b3f7b87 100644 --- a/src/systems/raw_contract.cairo +++ b/src/systems/raw_contract.cairo @@ -1,5 +1,5 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; -use dojo_examples::components::{Direction}; +use dojo_examples::models::{Direction}; // trait: specify functions to implement #[starknet::interface] @@ -14,7 +14,7 @@ trait IPlayerActions { mod player_actions_external { use starknet::{ContractAddress, get_caller_address}; use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; use super::IPlayerActions; @@ -74,8 +74,8 @@ mod tests { use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use dojo::test_utils::{spawn_test_world, deploy_contract}; - use dojo_examples::components::{position, moves}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{position, moves}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use super::{ IPlayerActionsDispatcher, IPlayerActionsDispatcherTrait, @@ -87,10 +87,10 @@ mod tests { fn test_move() { let caller = starknet::contract_address_const::<0x0>(); - // components - let mut components = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; - // deploy world with components - let world = spawn_test_world(components); + // models + let mut models = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; + // deploy world with models + let world = spawn_test_world(models); // deploy systems contract let contract_address = world diff --git a/src/systems/with_decorator.cairo b/src/systems/with_decorator.cairo index 0bad00a..4c8cc03 100644 --- a/src/systems/with_decorator.cairo +++ b/src/systems/with_decorator.cairo @@ -1,18 +1,18 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; -use dojo_examples::components::{Position, Moves, Direction}; +use dojo_examples::models::{Position, Moves, Direction}; use starknet::{ContractAddress, ClassHash}; // trait: specify functions to implement #[starknet::interface] trait IPlayerActions { - fn spawn(self: @TContractState, world: IWorldDispatcher); - fn move(self: @TContractState, world: IWorldDispatcher, direction: Direction); + fn spawn(self: @TContractState); + fn move(self: @TContractState, direction: Direction); } #[system] mod player_actions { use starknet::{ContractAddress, get_caller_address}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; use super::IPlayerActions; @@ -32,7 +32,8 @@ mod player_actions { #[external(v0)] impl PlayerActionsImpl of IPlayerActions { // ContractState is defined by system decorator expansion - fn spawn(self: @ContractState, world: IWorldDispatcher) { + fn spawn(self: @ContractState) { + let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -44,7 +45,8 @@ mod player_actions { ); } - fn move(self: @ContractState, world: IWorldDispatcher, direction: Direction) { + fn move(self: @ContractState, direction: Direction) { + let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; @@ -66,8 +68,8 @@ mod tests { use dojo::test_utils::{spawn_test_world, deploy_contract}; - use dojo_examples::components::{position, moves}; - use dojo_examples::components::{Position, Moves, Direction, Vec2}; + use dojo_examples::models::{position, moves}; + use dojo_examples::models::{Position, Moves, Direction, Vec2}; use super::{player_actions, IPlayerActionsDispatcher, IPlayerActionsDispatcherTrait}; #[test] @@ -75,18 +77,19 @@ mod tests { fn test_move() { let caller = starknet::contract_address_const::<0x0>(); - // components - let mut components = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; - // deploy world with components - let world = spawn_test_world(components); + // models + let mut models = array![position::TEST_CLASS_HASH, moves::TEST_CLASS_HASH,]; + // deploy world with models + let world = spawn_test_world(models); // deploy systems contract - let contract_address = deploy_contract(player_actions::TEST_CLASS_HASH, array![].span()); + let contract_address = world + .deploy_contract('salt', player_actions::TEST_CLASS_HASH.try_into().unwrap()); let player_actions_system = IPlayerActionsDispatcher { contract_address }; // System calls - player_actions_system.spawn(world); - player_actions_system.move(world, Direction::Right(())); + player_actions_system.spawn(); + player_actions_system.move(Direction::Right(())); let moves = get!(world, caller, Moves); let right_dir_felt: felt252 = Direction::Right(()).into(); diff --git a/src/utils.cairo b/src/utils.cairo index 586cea4..ca5c571 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -1,4 +1,4 @@ -use dojo_examples::components::{Position, Direction}; +use dojo_examples::models::{Position, Direction}; fn next_position(mut position: Position, direction: Direction) -> Position { match direction { From 555767a8862425078fe87cea6a5cdffb5a64d356 Mon Sep 17 00:00:00 2001 From: notV4l Date: Tue, 10 Oct 2023 12:32:02 +0200 Subject: [PATCH 4/5] world_dispatcher -> world_address --- src/systems/raw_contract.cairo | 6 +++--- src/systems/with_decorator.cairo | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/systems/raw_contract.cairo b/src/systems/raw_contract.cairo index b3f7b87..652082a 100644 --- a/src/systems/raw_contract.cairo +++ b/src/systems/raw_contract.cairo @@ -20,7 +20,7 @@ mod player_actions_external { #[storage] struct Storage { - world_dispatcher: ContractAddress, + world_address: ContractAddress, } #[event] @@ -39,7 +39,7 @@ mod player_actions_external { #[external(v0)] impl PlayerActionsImpl of IPlayerActions { fn spawn(self: @ContractState) { - let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; + let world = IWorldDispatcher { contract_address: self.world_address.read() }; let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -52,7 +52,7 @@ mod player_actions_external { } fn move(self: @ContractState, direction: Direction) { - let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; + let world = IWorldDispatcher { contract_address: self.world_address.read() }; let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; diff --git a/src/systems/with_decorator.cairo b/src/systems/with_decorator.cairo index 4c8cc03..5364446 100644 --- a/src/systems/with_decorator.cairo +++ b/src/systems/with_decorator.cairo @@ -33,7 +33,7 @@ mod player_actions { impl PlayerActionsImpl of IPlayerActions { // ContractState is defined by system decorator expansion fn spawn(self: @ContractState) { - let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; + let world = IWorldDispatcher { contract_address: self.world_address.read() }; let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -46,7 +46,7 @@ mod player_actions { } fn move(self: @ContractState, direction: Direction) { - let world = IWorldDispatcher { contract_address: self.world_dispatcher.read() }; + let world = IWorldDispatcher { contract_address: self.world_address.read() }; let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; From 75d6b850750fed3236bcd718768cc7fbafc41f59 Mon Sep 17 00:00:00 2001 From: notV4l Date: Tue, 10 Oct 2023 15:54:17 +0200 Subject: [PATCH 5/5] back to world_dispatcher --- src/systems/raw_contract.cairo | 6 +++--- src/systems/with_decorator.cairo | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/systems/raw_contract.cairo b/src/systems/raw_contract.cairo index 652082a..02e6bc2 100644 --- a/src/systems/raw_contract.cairo +++ b/src/systems/raw_contract.cairo @@ -20,7 +20,7 @@ mod player_actions_external { #[storage] struct Storage { - world_address: ContractAddress, + world_dispatcher: IWorldDispatcher, } #[event] @@ -39,7 +39,7 @@ mod player_actions_external { #[external(v0)] impl PlayerActionsImpl of IPlayerActions { fn spawn(self: @ContractState) { - let world = IWorldDispatcher { contract_address: self.world_address.read() }; + let world = self.world_dispatcher.read(); let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -52,7 +52,7 @@ mod player_actions_external { } fn move(self: @ContractState, direction: Direction) { - let world = IWorldDispatcher { contract_address: self.world_address.read() }; + let world = self.world_dispatcher.read(); let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1; diff --git a/src/systems/with_decorator.cairo b/src/systems/with_decorator.cairo index 5364446..f1e51f6 100644 --- a/src/systems/with_decorator.cairo +++ b/src/systems/with_decorator.cairo @@ -33,7 +33,7 @@ mod player_actions { impl PlayerActionsImpl of IPlayerActions { // ContractState is defined by system decorator expansion fn spawn(self: @ContractState) { - let world = IWorldDispatcher { contract_address: self.world_address.read() }; + let world = self.world_dispatcher.read(); let player = get_caller_address(); let position = get!(world, player, (Position)); set!( @@ -46,7 +46,7 @@ mod player_actions { } fn move(self: @ContractState, direction: Direction) { - let world = IWorldDispatcher { contract_address: self.world_address.read() }; + let world = self.world_dispatcher.read(); let player = get_caller_address(); let (mut position, mut moves) = get!(world, player, (Position, Moves)); moves.remaining -= 1;