From 44cba8ca23314b28b17d6b1da6d1e7c187c10251 Mon Sep 17 00:00:00 2001 From: dpinones Date: Wed, 10 Jan 2024 18:38:04 -0300 Subject: [PATCH 1/3] Update dojo example --- examples/spawn-and-move/README.md | 47 ++++++++++++++++++----- examples/spawn-and-move/Scarb.toml | 6 +-- examples/spawn-and-move/src/actions.cairo | 12 ++---- examples/spawn-and-move/src/models.cairo | 27 ++++++------- examples/spawn-and-move/src/utils.cairo | 21 +++------- 5 files changed, 60 insertions(+), 53 deletions(-) diff --git a/examples/spawn-and-move/README.md b/examples/spawn-and-move/README.md index 75e0ff346f..9420bc7d26 100644 --- a/examples/spawn-and-move/README.md +++ b/examples/spawn-and-move/README.md @@ -12,26 +12,55 @@ sozo build sozo migrate # Get the class hash of the Moves model by name -sozo model class-hash --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves -> 0x2b97f0b24be59ecf4504a27ac2301179be7df44c4c7d9482cd7b36137bc0fa4 +sozo model class-hash Moves --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +> 0x64495ca6dc1dc328972697b30468cea364bcb7452bbb6e4aaad3e4b3f190147 # Get the schema of the Moves model -sozo model schema --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves +sozo model schema Moves --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e > struct Moves { -> remaining: u8 +> #[key] +> player: ContractAddress, +> remaining: u8, +> last_direction: Direction = Invalid Option, +> } +> +> enum Direction { +> None +> Left +> Right +> Up +> Down > } # Get the value of the Moves model for an entity. (in this example, # 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 is # the calling account. -sozo model get --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 -> 0x0 +sozo model get Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +> struct Moves { +> #[key] +> player: ContractAddress = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973, +> remaining: u8 = 0, +> last_direction: Direction = None, +> } # The returned value is 0 since we haven't spawned yet. # We can spawn a player using the actions contract address -sozo execute 0x31571485922572446df9e3198a891e10d3a48e544544317dbcbb667e15848cd spawn +sozo execute 0x152dcff993befafe5001975149d2c50bd9621da7cbaed74f68e7d5e54e65abc spawn # Fetch the updated entity -sozo model get --world 0x26065106fa319c3981618e7567480a50132f23932226a51c219ffb8e47daa84 Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 -> 0xa +sozo model get Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +> struct Moves { +> #[key] +> player: ContractAddress = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973, +> remaining: u8 = 1, +> last_direction: Direction = None, +> } +> +> enum Direction { +> None +> Left +> Right +> Up +> Down +> } ``` diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index ecc01f66c9..afb2b0c9aa 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -2,9 +2,6 @@ cairo-version = "2.4.0" name = "dojo_examples" version = "0.4.4" -# Use the prelude with the less imports as possible -# from corelib. -edition = "2023_10" [cairo] sierra-replace-ids = true @@ -23,6 +20,5 @@ name = "example" rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 -account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" +account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" private_key = "0x1800000000300000180000000000030000000000003006001800006600" -world_address = "0x5010c31f127114c6198df8a5239e2b7a5151e1156fb43791e37e7385faa8138" diff --git a/examples/spawn-and-move/src/actions.cairo b/examples/spawn-and-move/src/actions.cairo index 6f56d1f928..ea94cde00f 100644 --- a/examples/spawn-and-move/src/actions.cairo +++ b/examples/spawn-and-move/src/actions.cairo @@ -1,19 +1,16 @@ -use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; -use dojo_examples::models::{Position, Moves, Direction}; -use starknet::{ContractAddress, ClassHash}; - #[starknet::interface] trait IActions { fn spawn(self: @TContractState); - fn move(self: @TContractState, direction: Direction); + fn move(self: @TContractState, direction: dojo_examples::models::Direction); } #[dojo::contract] mod actions { + use super::IActions; + use starknet::{ContractAddress, get_caller_address}; use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; - use super::IActions; #[event] #[derive(Drop, starknet::Event)] @@ -97,9 +94,8 @@ mod tests { 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::{actions, IActionsDispatcher, IActionsDispatcherTrait}; + use dojo_examples::models::{Position, position, Moves, moves, Direction, Vec2}; #[test] #[available_gas(30000000)] diff --git a/examples/spawn-and-move/src/models.cairo b/examples/spawn-and-move/src/models.cairo index 8c8a275bed..c917342ece 100644 --- a/examples/spawn-and-move/src/models.cairo +++ b/examples/spawn-and-move/src/models.cairo @@ -1,24 +1,22 @@ -use core::array::ArrayTrait; -use core::debug::PrintTrait; use starknet::ContractAddress; #[derive(Serde, Copy, Drop, Introspect)] enum Direction { - None: (), - Left: (), - Right: (), - Up: (), - Down: (), + 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, + Direction::None => 0, + Direction::Left => 1, + Direction::Right => 2, + Direction::Up => 3, + Direction::Down => 4, } } } @@ -31,13 +29,13 @@ struct Moves { last_direction: Direction } -#[derive(Copy, Drop, Serde, Print, Introspect)] +#[derive(Copy, Drop, Serde, Introspect)] struct Vec2 { x: u32, y: u32 } -#[derive(Model, Copy, Drop, Print, Serde)] +#[derive(Model, Copy, Drop, Serde)] struct Position { #[key] player: ContractAddress, @@ -64,7 +62,6 @@ impl Vec2Impl of Vec2Trait { #[cfg(test)] mod tests { - use core::debug::PrintTrait; use super::{Position, Vec2, Vec2Trait}; #[test] diff --git a/examples/spawn-and-move/src/utils.cairo b/examples/spawn-and-move/src/utils.cairo index ca5c5714f1..e14be466d8 100644 --- a/examples/spawn-and-move/src/utils.cairo +++ b/examples/spawn-and-move/src/utils.cairo @@ -2,22 +2,11 @@ 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; - }, + 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 } From 6483586af5dbb924dbbb0353e2c1d73903282496 Mon Sep 17 00:00:00 2001 From: dpinones Date: Tue, 16 Jan 2024 17:28:19 -0300 Subject: [PATCH 2/3] Update dojo v0.5.0 --- examples/spawn-and-move/Scarb.lock | 2 +- examples/spawn-and-move/Scarb.toml | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/spawn-and-move/Scarb.lock b/examples/spawn-and-move/Scarb.lock index ca51af1a48..91e1ed98cf 100644 --- a/examples/spawn-and-move/Scarb.lock +++ b/examples/spawn-and-move/Scarb.lock @@ -10,7 +10,7 @@ dependencies = [ [[package]] name = "dojo_examples" -version = "0.4.4" +version = "0.5.0" dependencies = [ "dojo", ] diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index afb2b0c9aa..29ee9409f6 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -1,7 +1,10 @@ [package] cairo-version = "2.4.0" name = "dojo_examples" -version = "0.4.4" +version = "0.5.0" +# Use the prelude with the less imports as possible +# from corelib. +edition = "2023_10" [cairo] sierra-replace-ids = true @@ -20,5 +23,6 @@ name = "example" rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 -account_address = "0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" +account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" private_key = "0x1800000000300000180000000000030000000000003006001800006600" +world_address = "0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e" \ No newline at end of file From 04f57a4d71745cfc6478fc964aaad2690f91c430 Mon Sep 17 00:00:00 2001 From: glihm Date: Tue, 16 Jan 2024 21:38:55 -0600 Subject: [PATCH 3/3] docs: minor fixes on new address --- examples/spawn-and-move/README.md | 16 ++++++++-------- examples/spawn-and-move/Scarb.toml | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/spawn-and-move/README.md b/examples/spawn-and-move/README.md index 9420bc7d26..9293592c12 100644 --- a/examples/spawn-and-move/README.md +++ b/examples/spawn-and-move/README.md @@ -33,25 +33,25 @@ sozo model schema Moves --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c > } # Get the value of the Moves model for an entity. (in this example, -# 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 is -# the calling account. -sozo model get Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +# 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03, is +# the calling account which is also the key to retrieve a Moves model) +sozo model get Moves 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e > struct Moves { > #[key] -> player: ContractAddress = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973, +> player: ContractAddress = 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03, > remaining: u8 = 0, > last_direction: Direction = None, > } # The returned value is 0 since we haven't spawned yet. -# We can spawn a player using the actions contract address +# We can spawn a player using the actions contract address. sozo execute 0x152dcff993befafe5001975149d2c50bd9621da7cbaed74f68e7d5e54e65abc spawn -# Fetch the updated entity -sozo model get Moves 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e +# Fetch the updated entity. +sozo model get Moves 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03 --world 0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e > struct Moves { > #[key] -> player: ContractAddress = 0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973, +> player: ContractAddress = 0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03, > remaining: u8 = 1, > last_direction: Direction = None, > } diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index 29ee9409f6..88e7c4153c 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -25,4 +25,4 @@ rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" private_key = "0x1800000000300000180000000000030000000000003006001800006600" -world_address = "0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e" \ No newline at end of file +world_address = "0x33ac2f528bb97cc7b79148fd1756dc368be0e95d391d8c6d6473ecb60b4560e"