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

Update dojo-starter to better reflect new pattern #24

Merged
merged 2 commits into from
Oct 17, 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
3 changes: 1 addition & 2 deletions 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 = "v0.3.0-rc8" }
dojo = { git = "https://github.com/dojoengine/dojo", rev = "4e820e29feb2fe076ef64c592b2f19fbd13b9e38" }

[[target.dojo]]

Expand All @@ -19,7 +19,6 @@ rpc_url = "http://localhost:5050/"
# Default account for katana with seed = 0
account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973"
private_key = "0x1800000000300000180000000000030000000000003006001800006600"
# world_address = "0x7d1f066a910bd86f532fa9ca66766722c20d47462fb99fb2fb0e1030262f9c5"

# Madara testnet
# rpc_url = "https://api.cartridge.gg/x/shinai/madara"
Expand Down
34 changes: 19 additions & 15 deletions src/systems/with_decorator.cairo → src/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use dojo_examples::models::{Position, Moves, Direction};
use starknet::{ContractAddress, ClassHash};

// trait: specify functions to implement
#[starknet::interface]
trait IPlayerActions<TContractState> {
trait IActions<TContractState> {
fn spawn(self: @TContractState);
fn move(self: @TContractState, direction: Direction);
}

#[dojo::contract]
mod player_actions {
mod actions {
use starknet::{ContractAddress, get_caller_address};
use dojo_examples::models::{Position, Moves, Direction, Vec2};
use dojo_examples::utils::next_position;
use super::IPlayerActions;
use super::IActions;

#[event]
#[derive(Drop, starknet::Event)]
Expand All @@ -30,17 +29,23 @@ mod player_actions {

// impl: implement functions specified in trait
#[external(v0)]
impl PlayerActionsImpl of IPlayerActions<ContractState> {
impl ActionsImpl of IActions<ContractState> {
// ContractState is defined by system decorator expansion
fn spawn(self: @ContractState) {
let world = self.world_dispatcher.read();
let player = get_caller_address();
let position = get!(world, player, (Position));
let moves = get!(world, player, (Moves));

set!(
world,
(
Moves { player, remaining: 10, last_direction: Direction::None(()) },
Position { player, vec: Vec2 { x: 10, y: 10 } },
Moves {
player, remaining: moves.remaining + 1, last_direction: Direction::None(())
},
Position {
player, vec: Vec2 { x: position.vec.x + 10, y: position.vec.y + 10 }
},
)
);
}
Expand All @@ -61,16 +66,15 @@ mod player_actions {

#[cfg(test)]
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::models::{position, moves};
use dojo_examples::models::{Position, Moves, Direction, Vec2};
use super::{player_actions, IPlayerActionsDispatcher, IPlayerActionsDispatcherTrait};
use super::{actions, IActionsDispatcher, IActionsDispatcherTrait};

#[test]
#[available_gas(30000000)]
Expand All @@ -84,17 +88,17 @@ mod tests {

// deploy systems contract
let contract_address = world
.deploy_contract('salt', player_actions::TEST_CLASS_HASH.try_into().unwrap());
let player_actions_system = IPlayerActionsDispatcher { contract_address };
.deploy_contract('salt', actions::TEST_CLASS_HASH.try_into().unwrap());
let actions_system = IActionsDispatcher { contract_address };

// System calls
player_actions_system.spawn();
player_actions_system.move(Direction::Right(()));
actions_system.spawn();
actions_system.move(Direction::Right(()));

let moves = get!(world, caller, Moves);
let right_dir_felt: felt252 = Direction::Right(()).into();

assert(moves.remaining == 9, 'moves is wrong');
assert(moves.remaining == 0, 'moves is wrong');
assert(moves.last_direction.into() == right_dir_felt, 'last direction is wrong');

let new_position = get!(world, caller, Position);
Expand Down
10 changes: 1 addition & 9 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
mod actions;
mod models;

mod systems {
// example with #[system] decorator
mod with_decorator;

// raw example with #[starknet::contract] decorator
mod raw_contract;
}

mod utils;
114 changes: 0 additions & 114 deletions src/systems/raw_contract.cairo

This file was deleted.

Loading