From 74d3e550f8db4def6b96f90fb4ef13e779c9c59e Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Fri, 5 Jan 2024 11:15:11 +1100 Subject: [PATCH] refactor --- .github/dependabot.yml | 8 -- Scarb.lock | 12 +-- Scarb.toml | 2 +- scripts/default_auth.sh | 2 + src/lib.cairo | 14 +++- src/models/moves.cairo | 31 ++++++++ src/{models.cairo => models/position.cairo} | 35 +-------- src/{ => systems}/actions.cairo | 85 ++++----------------- src/tests/test_world.cairo | 62 +++++++++++++++ src/utils.cairo | 12 --- 10 files changed, 132 insertions(+), 131 deletions(-) delete mode 100644 .github/dependabot.yml create mode 100644 src/models/moves.cairo rename src/{models.cairo => models/position.cairo} (64%) rename src/{ => systems}/actions.cairo (58%) create mode 100644 src/tests/test_world.cairo delete mode 100644 src/utils.cairo diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 280ba65..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,8 +0,0 @@ -version: 2 -updates: - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "daily" - allow: - - dependency-name: "dojoengine/dojo" diff --git a/Scarb.lock b/Scarb.lock index fab3f21..0354c39 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -10,13 +10,13 @@ dependencies = [ ] [[package]] -name = "dojo_examples" +name = "dojo_plugin" +version = "0.3.11" +source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659" + +[[package]] +name = "dojo_starter" version = "0.4.4" dependencies = [ "dojo", ] - -[[package]] -name = "dojo_plugin" -version = "0.3.11" -source = "git+https://github.com/dojoengine/dojo?tag=v0.3.11#1e651b5d4d3b79b14a7d8aa29a92062fcb9e6659" diff --git a/Scarb.toml b/Scarb.toml index 2d81ac5..921b5a6 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,6 +1,6 @@ [package] cairo-version = "2.4.0" -name = "dojo_examples" +name = "dojo_starter" version = "0.4.4" [cairo] diff --git a/scripts/default_auth.sh b/scripts/default_auth.sh index 80fcc9d..2c3b564 100644 --- a/scripts/default_auth.sh +++ b/scripts/default_auth.sh @@ -19,6 +19,8 @@ COMPONENTS=("Position" "Moves" ) for component in ${COMPONENTS[@]}; do sozo auth writer $component $ACTIONS_ADDRESS --world $WORLD_ADDRESS --rpc-url $RPC_URL + # time out for 1 second to avoid rate limiting + sleep 1 done echo "Default authorizations have been successfully set." \ No newline at end of file diff --git a/src/lib.cairo b/src/lib.cairo index 9f927ca..deb6ae1 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,4 +1,12 @@ -mod actions; -mod models; -mod utils; +mod systems { + mod actions; +} +mod models { + mod moves; + mod position; +} + +mod tests { + mod test_world; +} diff --git a/src/models/moves.cairo b/src/models/moves.cairo new file mode 100644 index 0000000..81baa40 --- /dev/null +++ b/src/models/moves.cairo @@ -0,0 +1,31 @@ +use starknet::ContractAddress; + +#[derive(Model, Drop, Serde)] +struct Moves { + #[key] + player: ContractAddress, + remaining: u8, + last_direction: Direction +} + +#[derive(Serde, Copy, Drop, Introspect)] +enum Direction { + None, + Left, + Right, + Up, + Down, +} + +impl DirectionIntoFelt252 of Into { + fn into(self: Direction) -> felt252 { + match self { + Direction::None => 0, + Direction::Left => 1, + Direction::Right => 2, + Direction::Up => 3, + Direction::Down => 4, + } + } +} + diff --git a/src/models.cairo b/src/models/position.cairo similarity index 64% rename from src/models.cairo rename to src/models/position.cairo index 9694708..320d215 100644 --- a/src/models.cairo +++ b/src/models/position.cairo @@ -1,32 +1,10 @@ use starknet::ContractAddress; -#[derive(Serde, Copy, Drop, Introspect)] -enum Direction { - None, - Left, - Right, - Up, - Down, -} - -impl DirectionIntoFelt252 of Into { - fn into(self: Direction) -> felt252 { - match self { - Direction::None => 0, - Direction::Left => 1, - Direction::Right => 2, - Direction::Up => 3, - Direction::Down => 4, - } - } -} - -#[derive(Model, Drop, Serde)] -struct Moves { +#[derive(Model, Copy, Drop, Serde)] +struct Position { #[key] player: ContractAddress, - remaining: u8, - last_direction: Direction + vec: Vec2, } #[derive(Copy, Drop, Serde, Introspect)] @@ -35,13 +13,6 @@ struct Vec2 { y: u32 } -#[derive(Model, Copy, Drop, Serde)] -struct Position { - #[key] - player: ContractAddress, - vec: Vec2, -} - trait Vec2Trait { fn is_zero(self: Vec2) -> bool; fn is_equal(self: Vec2, b: Vec2) -> bool; diff --git a/src/actions.cairo b/src/systems/actions.cairo similarity index 58% rename from src/actions.cairo rename to src/systems/actions.cairo index 1ea774e..3353790 100644 --- a/src/actions.cairo +++ b/src/systems/actions.cairo @@ -1,20 +1,18 @@ -use dojo_examples::models::{Direction}; - // define the interface #[starknet::interface] trait IActions { fn spawn(self: @TContractState); - fn move(self: @TContractState, direction: Direction); + fn move(self: @TContractState, direction: dojo_starter::models::moves::Direction); } // dojo decorator #[dojo::contract] mod actions { - use starknet::{ContractAddress, get_caller_address}; - use dojo_examples::models::{Position, Moves, Direction, Vec2}; - use dojo_examples::utils::next_position; use super::IActions; + use starknet::{ContractAddress, get_caller_address}; + use dojo_starter::models::{position::{Position, Vec2}, moves::{Moves, Direction}}; + // declaring custom event struct #[event] #[derive(Drop, starknet::Event)] @@ -29,6 +27,18 @@ mod actions { direction: Direction } + fn next_position(mut position: Position, direction: Direction) -> Position { + match direction { + Direction::None => { return position; }, + Direction::Left => { position.vec.x -= 1; }, + Direction::Right => { position.vec.x += 1; }, + Direction::Up => { position.vec.y -= 1; }, + Direction::Down => { position.vec.y += 1; }, + }; + position + } + + // impl: implement functions specified in trait #[external(v0)] impl ActionsImpl of IActions { @@ -86,66 +96,3 @@ mod actions { } } } - -#[cfg(test)] -mod tests { - use starknet::class_hash::Felt252TryIntoClassHash; - - // import world dispatcher - use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; - - // import test utils - use dojo::test_utils::{spawn_test_world, deploy_contract}; - - // import models - use dojo_examples::models::{position, moves}; - use dojo_examples::models::{Position, Moves, Direction, Vec2}; - - // import actions - use super::{actions, IActionsDispatcher, IActionsDispatcherTrait}; - - #[test] - #[available_gas(30000000)] - fn test_move() { - // caller - let caller = starknet::contract_address_const::<0x0>(); - - // 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 - .deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap()); - let actions_system = IActionsDispatcher { contract_address }; - - // call spawn() - actions_system.spawn(); - - // call move with direction right - actions_system.move(Direction::Right); - - // Check world state - let moves = get!(world, caller, Moves); - - // casting right direction - let right_dir_felt: felt252 = Direction::Right.into(); - - // check moves - assert(moves.remaining == 99, 'moves is wrong'); - - // check last direction - assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong'); - - // get new_position - let new_position = get!(world, caller, Position); - - // check new position x - assert(new_position.vec.x == 11, 'position x is wrong'); - - // check new position y - assert(new_position.vec.y == 10, 'position y is wrong'); - } -} diff --git a/src/tests/test_world.cairo b/src/tests/test_world.cairo new file mode 100644 index 0000000..b7c8309 --- /dev/null +++ b/src/tests/test_world.cairo @@ -0,0 +1,62 @@ +#[cfg(test)] +mod tests { + use starknet::class_hash::Felt252TryIntoClassHash; + + // import world dispatcher + use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; + + // import test utils + use dojo::test_utils::{spawn_test_world, deploy_contract}; + + // import test utils + use dojo_starter::{ + systems::{actions::{actions, IActionsDispatcher, IActionsDispatcherTrait}}, + models::{position::{Position, Vec2, position}, moves::{Moves, Direction, moves}} + }; + + + #[test] + #[available_gas(30000000)] + fn test_move() { + // caller + let caller = starknet::contract_address_const::<0x0>(); + + // 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 + .deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap()); + let actions_system = IActionsDispatcher { contract_address }; + + // call spawn() + actions_system.spawn(); + + // call move with direction right + actions_system.move(Direction::Right); + + // Check world state + let moves = get!(world, caller, Moves); + + // casting right direction + let right_dir_felt: felt252 = Direction::Right.into(); + + // check moves + assert(moves.remaining == 99, 'moves is wrong'); + + // check last direction + assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong'); + + // get new_position + let new_position = get!(world, caller, Position); + + // check new position x + assert(new_position.vec.x == 11, 'position x is wrong'); + + // check new position y + assert(new_position.vec.y == 10, 'position y is wrong'); + } +} diff --git a/src/utils.cairo b/src/utils.cairo deleted file mode 100644 index e14be46..0000000 --- a/src/utils.cairo +++ /dev/null @@ -1,12 +0,0 @@ -use dojo_examples::models::{Position, Direction}; - -fn next_position(mut position: Position, direction: Direction) -> Position { - match direction { - Direction::None => { return position; }, - Direction::Left => { position.vec.x -= 1; }, - Direction::Right => { position.vec.x += 1; }, - Direction::Up => { position.vec.y -= 1; }, - Direction::Down => { position.vec.y += 1; }, - }; - position -}