Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to latest dojo #18

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 v0.3.0-rc4
- run: |
/home/runner/.config/.dojo/bin/sozo build
/home/runner/.config/.dojo/bin/sozo test
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down
2 changes: 1 addition & 1 deletion src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod components;
mod models;

mod systems {
// example with #[system] decorator
Expand Down
20 changes: 10 additions & 10 deletions src/components.cairo → src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -28,15 +28,15 @@ impl DirectionSchemaIntrospectionImpl of SchemaIntrospection<Direction> {
#[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()
}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl DirectionIntoFelt252 of Into<Direction, felt252> {
}
}

#[derive(Component, Copy, Drop, Serde)]
#[derive(Model, Copy, Drop, Serde)]
struct Moves {
#[key]
player: ContractAddress,
Expand All @@ -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,
Expand Down
38 changes: 22 additions & 16 deletions src/systems/raw_contract.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo_examples::components::{Direction};
use dojo_examples::models::{Direction};

// trait: specify functions to implement
#[starknet::interface]
trait IPlayerActions<TContractState> {
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
Expand All @@ -14,12 +14,14 @@ trait IPlayerActions<TContractState> {
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;

#[storage]
struct Storage {}
struct Storage {
world_dispatcher: IWorldDispatcher,
}

#[event]
#[derive(Drop, starknet::Event)]
Expand All @@ -36,7 +38,8 @@ mod player_actions_external {
// impl: implement functions specified in trait
#[external(v0)]
impl PlayerActionsImpl of IPlayerActions<ContractState> {
fn spawn(self: @ContractState, world: IWorldDispatcher) {
fn spawn(self: @ContractState) {
let world = self.world_dispatcher.read();
let player = get_caller_address();
let position = get!(world, player, (Position));
set!(
Expand All @@ -48,7 +51,8 @@ mod player_actions_external {
);
}

fn move(self: @ContractState, world: IWorldDispatcher, direction: Direction) {
fn move(self: @ContractState, direction: Direction) {
let world = self.world_dispatcher.read();
let player = get_caller_address();
let (mut position, mut moves) = get!(world, player, (Position, Moves));
moves.remaining -= 1;
Expand All @@ -65,12 +69,13 @@ 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};

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,
Expand All @@ -82,18 +87,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();
Expand Down
33 changes: 18 additions & 15 deletions src/systems/with_decorator.cairo
Original file line number Diff line number Diff line change
@@ -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<TContractState> {
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;

Expand All @@ -32,7 +32,8 @@ mod player_actions {
#[external(v0)]
impl PlayerActionsImpl of IPlayerActions<ContractState> {
// ContractState is defined by system decorator expansion
fn spawn(self: @ContractState, world: IWorldDispatcher) {
fn spawn(self: @ContractState) {
let world = self.world_dispatcher.read();
let player = get_caller_address();
let position = get!(world, player, (Position));
set!(
Expand All @@ -44,7 +45,8 @@ mod player_actions {
);
}

fn move(self: @ContractState, world: IWorldDispatcher, direction: Direction) {
fn move(self: @ContractState, direction: Direction) {
let world = self.world_dispatcher.read();
let player = get_caller_address();
let (mut position, mut moves) = get!(world, player, (Position, Moves));
moves.remaining -= 1;
Expand All @@ -66,27 +68,28 @@ 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]
#[available_gas(30000000)]
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();
Expand Down
2 changes: 1 addition & 1 deletion src/utils.cairo
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Loading