From de1a1de90550dabf995c7da28812858305266055 Mon Sep 17 00:00:00 2001 From: "remy.baranx@gmail.com" Date: Wed, 17 Jul 2024 13:04:50 +0200 Subject: [PATCH 1/2] move to edition 2023_11 --- crates/dojo-core/Scarb.toml | 1 + crates/dojo-core/src/base.cairo | 2 +- crates/dojo-core/src/base_test.cairo | 15 +- crates/dojo-core/src/benchmarks.cairo | 199 ++++++------ crates/dojo-core/src/components.cairo | 2 +- .../src/components/upgradeable.cairo | 27 +- crates/dojo-core/src/config.cairo | 4 +- crates/dojo-core/src/config/component.cairo | 18 +- crates/dojo-core/src/config/interface.cairo | 2 +- crates/dojo-core/src/contract.cairo | 2 +- crates/dojo-core/src/database.cairo | 28 +- .../dojo-core/src/database/introspect.cairo | 74 ++--- crates/dojo-core/src/database/storage.cairo | 84 +++-- .../dojo-core/src/database/storage_test.cairo | 7 +- crates/dojo-core/src/database/utils.cairo | 4 +- crates/dojo-core/src/database_test.cairo | 12 +- crates/dojo-core/src/interfaces.cairo | 28 +- crates/dojo-core/src/lib.cairo | 24 +- crates/dojo-core/src/model.cairo | 10 +- crates/dojo-core/src/model_test.cairo | 2 +- crates/dojo-core/src/packing.cairo | 33 +- crates/dojo-core/src/packing_test.cairo | 40 ++- crates/dojo-core/src/resource_metadata.cairo | 58 ++-- crates/dojo-core/src/test_utils.cairo | 40 ++- crates/dojo-core/src/utils.cairo | 14 +- crates/dojo-core/src/world.cairo | 291 +++++++++--------- crates/dojo-core/src/world_test.cairo | 258 ++++++++-------- crates/dojo-lang/src/interface.rs | 4 +- .../compiler_cairo/Scarb.toml | 2 +- .../manifests/dev/base/dojo-world.toml | 4 +- crates/dojo-lang/src/model.rs | 71 ++--- crates/dojo-world/abigen/src/main.rs | 1 + examples/game-lib/armory/src/lib.cairo | 8 +- examples/game-lib/bestiary/src/lib.cairo | 10 +- examples/spawn-and-move/Scarb.toml | 4 +- .../dojo_examples-actions-40b6994c.toml | 4 +- .../manifests/dev/base/dojo-world.toml | 4 +- .../manifests/dev/deployment/manifest.json | 20 +- .../manifests/dev/deployment/manifest.toml | 20 +- .../dojo_examples-actions-40b6994c.toml | 4 +- .../manifests/release/base/dojo-world.toml | 4 +- examples/spawn-and-move/src/actions.cairo | 20 +- examples/spawn-and-move/src/dungeon.cairo | 6 +- examples/spawn-and-move/src/lib.cairo | 12 +- examples/spawn-and-move/src/mock_token.cairo | 2 +- examples/spawn-and-move/src/models.cairo | 74 ++--- examples/spawn-and-move/src/others.cairo | 2 +- examples/spawn-and-move/src/utils.cairo | 2 +- 48 files changed, 778 insertions(+), 779 deletions(-) diff --git a/crates/dojo-core/Scarb.toml b/crates/dojo-core/Scarb.toml index eddba51daa..eb670115fc 100644 --- a/crates/dojo-core/Scarb.toml +++ b/crates/dojo-core/Scarb.toml @@ -1,5 +1,6 @@ [package] cairo-version = "=2.6.4" +edition = "2023_11" description = "The Dojo Core library for autonomous worlds." name = "dojo" version = "0.7.3" diff --git a/crates/dojo-core/src/base.cairo b/crates/dojo-core/src/base.cairo index b49ff2027b..8231407583 100644 --- a/crates/dojo-core/src/base.cairo +++ b/crates/dojo-core/src/base.cairo @@ -1,7 +1,7 @@ use dojo::world::IWorldDispatcher; #[starknet::contract] -mod base { +pub mod base { use starknet::{ClassHash, get_caller_address}; use dojo::world::IWorldDispatcher; use dojo::world::IWorldProvider; diff --git a/crates/dojo-core/src/base_test.cairo b/crates/dojo-core/src/base_test.cairo index f517a85c8b..4d9ffaeee6 100644 --- a/crates/dojo-core/src/base_test.cairo +++ b/crates/dojo-core/src/base_test.cairo @@ -1,7 +1,6 @@ -use debug::PrintTrait; -use option::OptionTrait; +use core::option::OptionTrait; use starknet::ClassHash; -use traits::TryInto; +use core::traits::TryInto; use dojo::base::base; use dojo::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait}; @@ -10,14 +9,14 @@ use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; #[starknet::contract] -mod contract_upgrade { +pub mod contract_upgrade { use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, IWorldProvider}; #[storage] struct Storage {} #[starknet::interface] - trait IQuantumLeap { + pub trait IQuantumLeap { fn plz_more_tps(self: @TState) -> felt252; } @@ -25,14 +24,14 @@ mod contract_upgrade { fn constructor(ref self: ContractState) {} #[abi(embed_v0)] - impl QuantumLeap of IQuantumLeap { + pub impl QuantumLeap of IQuantumLeap { fn plz_more_tps(self: @ContractState) -> felt252 { 'daddy' } } #[abi(embed_v0)] - impl WorldProviderImpl of IWorldProvider { + pub impl WorldProviderImpl of IWorldProvider { fn world(self: @ContractState) -> IWorldDispatcher { IWorldDispatcher { contract_address: starknet::contract_address_const::<'world'>() } } @@ -40,7 +39,7 @@ mod contract_upgrade { } #[starknet::contract] -mod contract_invalid_upgrade { +pub mod contract_invalid_upgrade { #[storage] struct Storage {} } diff --git a/crates/dojo-core/src/benchmarks.cairo b/crates/dojo-core/src/benchmarks.cairo index b0e827ed25..d7312d4a09 100644 --- a/crates/dojo-core/src/benchmarks.cairo +++ b/crates/dojo-core/src/benchmarks.cairo @@ -1,8 +1,7 @@ use core::result::ResultTrait; -use array::ArrayTrait; -use array::SpanTrait; -use debug::PrintTrait; -use poseidon::poseidon_hash_span; +use core::array::ArrayTrait; +use core::array::SpanTrait; +use core::poseidon::poseidon_hash_span; use starknet::SyscallResultTrait; use starknet::{contract_address_const, ContractAddress, ClassHash, get_caller_address}; @@ -10,13 +9,13 @@ use dojo::database; use dojo::database::storage; use dojo::model::Model; use dojo::world_test::Foo; -use dojo::test_utils::GasCounterImpl; +use dojo::test_utils::GasCounterTrait; use dojo::database::introspect::{Introspect, Layout}; #[test] #[available_gas(1000000000)] fn bench_reference_offset() { - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); gas.end("bench empty"); } @@ -25,11 +24,11 @@ fn bench_reference_offset() { fn bench_storage_single() { let keys = array!['database_test', '42'].span(); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); storage::set(0, keys, 420); gas.end("storage set"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let res = storage::get(0, keys); gas.end("storage get"); @@ -43,11 +42,11 @@ fn bench_storage_many() { let values = array![1, 2].span(); let layout = array![251, 251].span(); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); storage::set_many(0, keys, values, 0, layout).unwrap(); gas.end("storage set_many"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let res = storage::get_many(0, keys, layout).unwrap(); gas.end("storage get_many"); @@ -59,18 +58,20 @@ fn bench_storage_many() { #[test] #[available_gas(1000000000)] fn bench_native_storage() { - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let keys = array![0x1337].span(); - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - let address = starknet::storage_address_from_base(base); + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + let address = starknet::storage_access::storage_address_from_base(base); gas.end("native prep"); - let gas = GasCounterImpl::start(); - starknet::storage_write_syscall(0, address, 42).unwrap_syscall(); + let gas = GasCounterTrait::start(); + starknet::syscalls::storage_write_syscall(0, address, 42).unwrap_syscall(); gas.end("native write"); - let gas = GasCounterImpl::start(); - let value = starknet::storage_read_syscall(0, address).unwrap_syscall(); + let gas = GasCounterTrait::start(); + let value = starknet::syscalls::storage_read_syscall(0, address).unwrap_syscall(); gas.end("native read"); assert(value == 42, 'read invalid'); @@ -79,18 +80,20 @@ fn bench_native_storage() { #[test] #[available_gas(1000000000)] fn bench_native_storage_offset() { - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let keys = array![0x1337].span(); - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - let address = starknet::storage_address_from_base_and_offset(base, 42); + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + let address = starknet::storage_access::storage_address_from_base_and_offset(base, 42); gas.end("native prep of"); - let gas = GasCounterImpl::start(); - starknet::storage_write_syscall(0, address, 42).unwrap_syscall(); + let gas = GasCounterTrait::start(); + starknet::syscalls::storage_write_syscall(0, address, 42).unwrap_syscall(); gas.end("native writ of"); - let gas = GasCounterImpl::start(); - let value = starknet::storage_read_syscall(0, address).unwrap_syscall(); + let gas = GasCounterTrait::start(); + let value = starknet::syscalls::storage_read_syscall(0, address).unwrap_syscall(); gas.end("native read of"); assert(value == 42, 'read invalid'); @@ -118,11 +121,11 @@ fn bench_database_array() { i += 1; }; - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::set('table', 'key', values.span(), 0, layout.span()); gas.end("db set arr"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let res = database::get('table', 'key', layout.span()); gas.end("db get arr"); @@ -142,18 +145,18 @@ fn bench_database_array() { fn bench_simple_struct() { let caller = starknet::contract_address_const::<0x42>(); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut foo = Foo { caller, a: 0x123456789abcdef, b: 0x123456789abcdef, }; gas.end("foo init"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut serialized = ArrayTrait::new(); - serde::Serde::serialize(@foo.a, ref serialized); - serde::Serde::serialize(@foo.b, ref serialized); - let serialized = array::ArrayTrait::span(@serialized); + core::serde::Serde::serialize(@foo.a, ref serialized); + core::serde::Serde::serialize(@foo.b, ref serialized); + let serialized = core::array::ArrayTrait::span(@serialized); gas.end("foo serialize"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let values: Span = foo.values(); gas.end("foo values"); @@ -181,7 +184,7 @@ struct PositionWithQuaterions { #[test] #[available_gas(1000000000)] fn test_struct_with_many_fields_fixed() { - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut pos = PositionWithQuaterions { id: 0x123456789abcdef, @@ -195,19 +198,19 @@ fn test_struct_with_many_fields_fixed() { }; gas.end("pos init"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut serialized = ArrayTrait::new(); - serde::Serde::serialize(@pos.x, ref serialized); - serde::Serde::serialize(@pos.y, ref serialized); - serde::Serde::serialize(@pos.z, ref serialized); - serde::Serde::serialize(@pos.a, ref serialized); - serde::Serde::serialize(@pos.b, ref serialized); - serde::Serde::serialize(@pos.c, ref serialized); - serde::Serde::serialize(@pos.d, ref serialized); - let serialized = array::ArrayTrait::span(@serialized); + core::serde::Serde::serialize(@pos.x, ref serialized); + core::serde::Serde::serialize(@pos.y, ref serialized); + core::serde::Serde::serialize(@pos.z, ref serialized); + core::serde::Serde::serialize(@pos.a, ref serialized); + core::serde::Serde::serialize(@pos.b, ref serialized); + core::serde::Serde::serialize(@pos.c, ref serialized); + core::serde::Serde::serialize(@pos.d, ref serialized); + let serialized = core::array::ArrayTrait::span(@serialized); gas.end("pos serialize"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let values: Span = pos.values(); gas.end("pos values"); @@ -226,28 +229,28 @@ fn test_struct_with_many_fields_fixed() { _ => panic!("expected fixed layout"), }; - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::set('positions', '42', pos.values(), 0, layout); gas.end("pos db set"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::get('positions', '42', layout); gas.end("pos db get"); } #[derive(IntrospectPacked, Copy, Drop, Serde)] -struct Sword { - swordsmith: ContractAddress, - damage: u32, +pub struct Sword { + pub swordsmith: ContractAddress, + pub damage: u32, } #[derive(IntrospectPacked, Copy, Drop, Serde)] #[dojo::model] -struct Case { +pub struct Case { #[key] - owner: ContractAddress, - sword: Sword, - material: felt252, + pub owner: ContractAddress, + pub sword: Sword, + pub material: felt252, } // TODO: this test should be adapted to benchmark the new layout system @@ -257,24 +260,20 @@ struct Case { fn bench_nested_struct_packed() { let caller = starknet::contract_address_const::<0x42>(); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut case = Case { owner: caller, sword: Sword { swordsmith: caller, damage: 0x12345678, }, material: 'wooden', }; gas.end("case init"); - // ???? - let _gas = testing::get_available_gas(); - gas::withdraw_gas().unwrap(); - - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut serialized = ArrayTrait::new(); - serde::Serde::serialize(@case.sword, ref serialized); - serde::Serde::serialize(@case.material, ref serialized); - let serialized = array::ArrayTrait::span(@serialized); + core::serde::Serde::serialize(@case.sword, ref serialized); + core::serde::Serde::serialize(@case.material, ref serialized); + let serialized = core::array::ArrayTrait::span(@serialized); gas.end("case serialize"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let values: Span = case.values(); gas.end("case values"); @@ -293,52 +292,52 @@ fn bench_nested_struct_packed() { _ => panic!("expected fixed layout"), }; - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::set('cases', '42', values, 0, layout); gas.end("case db set"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::get('cases', '42', layout); gas.end("case db get"); } #[derive(IntrospectPacked, Copy, Drop, Serde)] #[dojo::model] -struct Character { +pub struct Character { #[key] - caller: ContractAddress, - heigth: felt252, - abilities: Abilities, - stats: Stats, - weapon: Weapon, - gold: u32, + pub caller: ContractAddress, + pub heigth: felt252, + pub abilities: Abilities, + pub stats: Stats, + pub weapon: Weapon, + pub gold: u32, } #[derive(IntrospectPacked, Copy, Drop, Serde)] -struct Abilities { - strength: u8, - dexterity: u8, - constitution: u8, - intelligence: u8, - wisdom: u8, - charisma: u8, +pub struct Abilities { + pub strength: u8, + pub dexterity: u8, + pub constitution: u8, + pub intelligence: u8, + pub wisdom: u8, + pub charisma: u8, } #[derive(IntrospectPacked, Copy, Drop, Serde)] -struct Stats { - kills: u128, - deaths: u16, - rests: u32, - hits: u64, - blocks: u32, - walked: felt252, - runned: felt252, - finished: bool, - romances: u16, +pub struct Stats { + pub kills: u128, + pub deaths: u16, + pub rests: u32, + pub hits: u64, + pub blocks: u32, + pub walked: felt252, + pub runned: felt252, + pub finished: bool, + pub romances: u16, } #[derive(IntrospectPacked, Copy, Drop, Serde)] -enum Weapon { +pub enum Weapon { DualWield: (Sword, Sword), Fists: (Sword, Sword), // Introspect requires same arms } @@ -348,7 +347,7 @@ enum Weapon { #[ignore] #[available_gas(1000000000)] fn bench_complex_struct_packed() { - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let char = Character { caller: starknet::contract_address_const::<0x42>(), @@ -386,17 +385,17 @@ fn bench_complex_struct_packed() { }; gas.end("chars init"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let mut serialized = ArrayTrait::new(); - serde::Serde::serialize(@char.heigth, ref serialized); - serde::Serde::serialize(@char.abilities, ref serialized); - serde::Serde::serialize(@char.stats, ref serialized); - serde::Serde::serialize(@char.weapon, ref serialized); - serde::Serde::serialize(@char.gold, ref serialized); - let serialized = array::ArrayTrait::span(@serialized); + core::serde::Serde::serialize(@char.heigth, ref serialized); + core::serde::Serde::serialize(@char.abilities, ref serialized); + core::serde::Serde::serialize(@char.stats, ref serialized); + core::serde::Serde::serialize(@char.weapon, ref serialized); + core::serde::Serde::serialize(@char.gold, ref serialized); + let serialized = core::array::ArrayTrait::span(@serialized); gas.end("chars serialize"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let values: Span = char.values(); gas.end("chars values"); @@ -416,11 +415,11 @@ fn bench_complex_struct_packed() { _ => panic!("expected fixed layout"), }; - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::set('chars', '42', char.values(), 0, layout); gas.end("chars db set"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); database::get('chars', '42', layout); gas.end("chars db get"); } diff --git a/crates/dojo-core/src/components.cairo b/crates/dojo-core/src/components.cairo index 892aaa57e7..06a50c8591 100644 --- a/crates/dojo-core/src/components.cairo +++ b/crates/dojo-core/src/components.cairo @@ -1 +1 @@ -mod upgradeable; +pub mod upgradeable; diff --git a/crates/dojo-core/src/components/upgradeable.cairo b/crates/dojo-core/src/components/upgradeable.cairo index e8673e478c..5c4380eb88 100644 --- a/crates/dojo-core/src/components/upgradeable.cairo +++ b/crates/dojo-core/src/components/upgradeable.cairo @@ -1,42 +1,41 @@ use starknet::ClassHash; #[starknet::interface] -trait IUpgradeable { +pub trait IUpgradeable { fn upgrade(ref self: T, new_class_hash: ClassHash); } #[starknet::component] -mod upgradeable { +pub mod upgradeable { use core::starknet::SyscallResultTrait; use starknet::ClassHash; use starknet::ContractAddress; use starknet::get_caller_address; use starknet::syscalls::replace_class_syscall; use dojo::world::{IWorldProvider, IWorldProviderDispatcher, IWorldDispatcher}; + use core::num::traits::Zero; #[storage] struct Storage {} #[event] #[derive(Drop, starknet::Event)] - enum Event { + pub enum Event { Upgraded: Upgraded, } #[derive(Drop, starknet::Event)] - struct Upgraded { - class_hash: ClassHash + pub struct Upgraded { + pub class_hash: ClassHash } - mod Errors { - const INVALID_CLASS: felt252 = 'class_hash cannot be zero'; - const INVALID_CLASS_CONTENT: felt252 = 'class_hash not world provider'; - const INVALID_CALLER: felt252 = 'must be called by world'; - const INVALID_WORLD_ADDRESS: felt252 = 'invalid world address'; + pub mod Errors { + pub const INVALID_CLASS: felt252 = 'class_hash cannot be zero'; + pub const INVALID_CLASS_CONTENT: felt252 = 'class_hash not world provider'; + pub const INVALID_CALLER: felt252 = 'must be called by world'; + pub const INVALID_WORLD_ADDRESS: felt252 = 'invalid world address'; } - use debug::PrintTrait; - #[embeddable_as(UpgradableImpl)] impl Upgradable< TContractState, +HasComponent, +IWorldProvider @@ -52,14 +51,14 @@ mod upgradeable { ); assert(new_class_hash.is_non_zero(), Errors::INVALID_CLASS); - match starknet::library_call_syscall( + match starknet::syscalls::library_call_syscall( new_class_hash, selector!("world"), array![].span(), ) { Result::Ok(_) => { replace_class_syscall(new_class_hash).unwrap(); self.emit(Upgraded { class_hash: new_class_hash }); }, - Result::Err(_) => panic_with_felt252(Errors::INVALID_CLASS_CONTENT), + Result::Err(_) => core::panic_with_felt252(Errors::INVALID_CLASS_CONTENT), } } } diff --git a/crates/dojo-core/src/config.cairo b/crates/dojo-core/src/config.cairo index fa0acf00ab..fee8901c6f 100644 --- a/crates/dojo-core/src/config.cairo +++ b/crates/dojo-core/src/config.cairo @@ -1,2 +1,2 @@ -mod component; -mod interface; +pub mod component; +pub mod interface; diff --git a/crates/dojo-core/src/config/component.cairo b/crates/dojo-core/src/config/component.cairo index ce77eb5b49..f0d7974938 100644 --- a/crates/dojo-core/src/config/component.cairo +++ b/crates/dojo-core/src/config/component.cairo @@ -1,11 +1,11 @@ -mod errors { - const INVALID_CALLER: felt252 = 'Config: not owner or operator'; - const ALREADY_REGISTERED: felt252 = 'Config: already operator'; - const NOT_OPERATOR: felt252 = 'Config: not operator'; +pub mod errors { + pub const INVALID_CALLER: felt252 = 'Config: not owner or operator'; + pub const ALREADY_REGISTERED: felt252 = 'Config: already operator'; + pub const NOT_OPERATOR: felt252 = 'Config: not operator'; } #[starknet::component] -mod Config { +pub mod Config { use dojo::config::interface::IConfig; use starknet::ContractAddress; use super::errors; @@ -22,17 +22,17 @@ mod Config { #[derive(Drop, starknet::Event, Debug, PartialEq)] pub struct DifferProgramHashUpdate { - program_hash: felt252, + pub program_hash: felt252, } #[derive(Drop, starknet::Event, Debug, PartialEq)] pub struct MergerProgramHashUpdate { - program_hash: felt252, + pub program_hash: felt252, } #[derive(Drop, starknet::Event, Debug, PartialEq)] pub struct FactsRegistryUpdate { - address: ContractAddress + pub address: ContractAddress } #[storage] @@ -44,7 +44,7 @@ mod Config { } #[generate_trait] - impl InternalImpl< + pub impl InternalImpl< TContractState, +HasComponent > of InternalTrait { fn initializer(ref self: ComponentState, owner: ContractAddress) { diff --git a/crates/dojo-core/src/config/interface.cairo b/crates/dojo-core/src/config/interface.cairo index de82abcdec..cd272229de 100644 --- a/crates/dojo-core/src/config/interface.cairo +++ b/crates/dojo-core/src/config/interface.cairo @@ -1,7 +1,7 @@ use starknet::ContractAddress; #[starknet::interface] -trait IConfig { +pub trait IConfig { /// Sets the information of the program that generates the /// state transition trace (namely DojoOS). /// diff --git a/crates/dojo-core/src/contract.cairo b/crates/dojo-core/src/contract.cairo index c9aa602947..1c0c7130c8 100644 --- a/crates/dojo-core/src/contract.cairo +++ b/crates/dojo-core/src/contract.cairo @@ -1,5 +1,5 @@ #[starknet::interface] -trait IContract { +pub trait IContract { fn contract_name(self: @T) -> ByteArray; fn namespace(self: @T) -> ByteArray; fn tag(self: @T) -> ByteArray; diff --git a/crates/dojo-core/src/database.cairo b/crates/dojo-core/src/database.cairo index 14c6908902..e54ef3f0af 100644 --- a/crates/dojo-core/src/database.cairo +++ b/crates/dojo-core/src/database.cairo @@ -1,22 +1,22 @@ use core::option::OptionTrait; -use array::{ArrayTrait, SpanTrait}; -use traits::{Into, TryInto}; -use serde::Serde; -use hash::LegacyHash; -use poseidon::poseidon_hash_span; +use core::array::{ArrayTrait, SpanTrait}; +use core::traits::{Into, TryInto}; +use core::serde::Serde; +use core::hash::LegacyHash; +use core::poseidon::poseidon_hash_span; use starknet::SyscallResultTrait; const DOJO_STORAGE: felt252 = 'dojo_storage'; -const MAX_ARRAY_LENGTH: u256 = 4_294_967_295; +pub const MAX_ARRAY_LENGTH: u256 = 4_294_967_295; -mod introspect; +pub mod introspect; #[cfg(test)] mod introspect_test; -mod storage; +pub mod storage; #[cfg(test)] mod storage_test; -mod utils; +pub mod utils; /// Fill the provided array with zeroes. /// @@ -60,7 +60,7 @@ fn _get_storage_key(table: felt252, key: felt252) -> Span { /// /// # Returns /// A [`Span`] containing the raw unpacked data of the read record. -fn get(table: felt252, key: felt252, layout: Span) -> Span { +pub fn get(table: felt252, key: felt252, layout: Span) -> Span { storage::get_many(0, _get_storage_key(table, key), layout).unwrap_syscall() } @@ -71,7 +71,7 @@ fn get(table: felt252, key: felt252, layout: Span) -> Span { /// * `key` - key of the record to write /// * `value` - the new raw unpacked data value of the record /// * `layout` - the layout of the record to write. -fn set(table: felt252, key: felt252, value: Span, offset: u32, layout: Span) { +pub fn set(table: felt252, key: felt252, value: Span, offset: u32, layout: Span) { let storage_key = _get_storage_key(table, key); storage::set_many(0, storage_key, value, offset, layout).unwrap_syscall(); } @@ -82,18 +82,18 @@ fn set(table: felt252, key: felt252, value: Span, offset: u32, layout: /// * `table` - the table selector /// * `key` - key of the record to delete /// * `layout` - the layout of the record to delete -fn delete(table: felt252, key: felt252, layout: Span) { +pub fn delete(table: felt252, key: felt252, layout: Span) { let mut reset_values = array![]; _fill_with_zeroes(ref reset_values, layout.len()); set(table, key, reset_values.span(), 0, layout); } -fn set_array(table: felt252, key: felt252, value: Span, offset: u32, array_size: u32) { +pub fn set_array(table: felt252, key: felt252, value: Span, offset: u32, array_size: u32) { let storage_key = _get_storage_key(table, key); storage::set_packed_array(0, storage_key, value, offset, array_size).unwrap_syscall(); } -fn get_array(table: felt252, key: felt252, array_size: u32) -> Span { +pub fn get_array(table: felt252, key: felt252, array_size: u32) -> Span { let storage_key = _get_storage_key(table, key); storage::get_packed_array(0, storage_key, array_size).unwrap_syscall() } diff --git a/crates/dojo-core/src/database/introspect.cairo b/crates/dojo-core/src/database/introspect.cairo index 8cd2332b17..96395f5573 100644 --- a/crates/dojo-core/src/database/introspect.cairo +++ b/crates/dojo-core/src/database/introspect.cairo @@ -1,11 +1,11 @@ #[derive(Copy, Drop, Serde, Debug, PartialEq)] -struct FieldLayout { - selector: felt252, - layout: Layout +pub struct FieldLayout { + pub selector: felt252, + pub layout: Layout } #[derive(Copy, Drop, Serde, Debug, PartialEq)] -enum Layout { +pub enum Layout { Fixed: Span, Struct: Span, Tuple: Span, @@ -21,7 +21,7 @@ enum Layout { } #[derive(Copy, Drop, Serde)] -enum Ty { +pub enum Ty { Primitive: felt252, Struct: Struct, Enum: Enum, @@ -34,33 +34,33 @@ enum Ty { } #[derive(Copy, Drop, Serde)] -struct Struct { - name: felt252, - attrs: Span, - children: Span +pub struct Struct { + pub name: felt252, + pub attrs: Span, + pub children: Span } #[derive(Copy, Drop, Serde)] -struct Enum { - name: felt252, - attrs: Span, - children: Span<(felt252, Ty)> +pub struct Enum { + pub name: felt252, + pub attrs: Span, + pub children: Span<(felt252, Ty)> } #[derive(Copy, Drop, Serde)] -struct Member { - name: felt252, - attrs: Span, - ty: Ty +pub struct Member { + pub name: felt252, + pub attrs: Span, + pub ty: Ty } -trait Introspect { +pub trait Introspect { fn size() -> Option; fn layout() -> Layout; fn ty() -> Ty; } -impl Introspect_felt252 of Introspect { +pub impl Introspect_felt252 of Introspect { fn size() -> Option { Option::Some(1) } @@ -72,7 +72,7 @@ impl Introspect_felt252 of Introspect { } } -impl Introspect_bool of Introspect { +pub impl Introspect_bool of Introspect { fn size() -> Option { Option::Some(1) } @@ -84,7 +84,7 @@ impl Introspect_bool of Introspect { } } -impl Introspect_u8 of Introspect { +pub impl Introspect_u8 of Introspect { fn size() -> Option { Option::Some(1) } @@ -96,7 +96,7 @@ impl Introspect_u8 of Introspect { } } -impl Introspect_u16 of Introspect { +pub impl Introspect_u16 of Introspect { fn size() -> Option { Option::Some(1) } @@ -108,7 +108,7 @@ impl Introspect_u16 of Introspect { } } -impl Introspect_u32 of Introspect { +pub impl Introspect_u32 of Introspect { fn size() -> Option { Option::Some(1) } @@ -120,7 +120,7 @@ impl Introspect_u32 of Introspect { } } -impl Introspect_u64 of Introspect { +pub impl Introspect_u64 of Introspect { fn size() -> Option { Option::Some(1) } @@ -132,7 +132,7 @@ impl Introspect_u64 of Introspect { } } -impl Introspect_u128 of Introspect { +pub impl Introspect_u128 of Introspect { fn size() -> Option { Option::Some(1) } @@ -144,7 +144,7 @@ impl Introspect_u128 of Introspect { } } -impl Introspect_u256 of Introspect { +pub impl Introspect_u256 of Introspect { fn size() -> Option { Option::Some(2) } @@ -156,7 +156,7 @@ impl Introspect_u256 of Introspect { } } -impl Introspect_i8 of Introspect { +pub impl Introspect_i8 of Introspect { fn size() -> Option { Option::Some(1) } @@ -168,7 +168,7 @@ impl Introspect_i8 of Introspect { } } -impl Introspect_i16 of Introspect { +pub impl Introspect_i16 of Introspect { fn size() -> Option { Option::Some(1) } @@ -180,7 +180,7 @@ impl Introspect_i16 of Introspect { } } -impl Introspect_i32 of Introspect { +pub impl Introspect_i32 of Introspect { fn size() -> Option { Option::Some(1) } @@ -192,7 +192,7 @@ impl Introspect_i32 of Introspect { } } -impl Introspect_i64 of Introspect { +pub impl Introspect_i64 of Introspect { fn size() -> Option { Option::Some(1) } @@ -204,7 +204,7 @@ impl Introspect_i64 of Introspect { } } -impl Introspect_i128 of Introspect { +pub impl Introspect_i128 of Introspect { fn size() -> Option { Option::Some(1) } @@ -216,7 +216,7 @@ impl Introspect_i128 of Introspect { } } -impl Introspect_address of Introspect { +pub impl Introspect_address of Introspect { fn size() -> Option { Option::Some(1) } @@ -228,7 +228,7 @@ impl Introspect_address of Introspect { } } -impl Introspect_classhash of Introspect { +pub impl Introspect_classhash of Introspect { fn size() -> Option { Option::Some(1) } @@ -240,7 +240,7 @@ impl Introspect_classhash of Introspect { } } -impl Introspect_bytearray of Introspect { +pub impl Introspect_bytearray of Introspect { fn size() -> Option { Option::None } @@ -252,7 +252,7 @@ impl Introspect_bytearray of Introspect { } } -impl Introspect_option> of Introspect> { +pub impl Introspect_option> of Introspect> { fn size() -> Option { Option::None } @@ -283,7 +283,7 @@ impl Introspect_option> of Introspect> { } } -impl Introspect_array> of Introspect> { +pub impl Introspect_array> of Introspect> { fn size() -> Option { Option::None } @@ -296,7 +296,7 @@ impl Introspect_array> of Introspect> { } } -impl Introspect_span> of Introspect> { +pub impl Introspect_span> of Introspect> { fn size() -> Option { Option::None } diff --git a/crates/dojo-core/src/database/storage.cairo b/crates/dojo-core/src/database/storage.cairo index ca82675c68..85baada390 100644 --- a/crates/dojo-core/src/database/storage.cairo +++ b/crates/dojo-core/src/database/storage.cairo @@ -1,22 +1,29 @@ -use array::{ArrayTrait, SpanTrait}; -use option::OptionTrait; -use starknet::{SyscallResultTrait, StorageAddress, StorageBaseAddress, SyscallResult}; -use traits::Into; -use poseidon::poseidon_hash_span; -use serde::Serde; +use core::array::{ArrayTrait, SpanTrait}; +use core::option::OptionTrait; +use starknet::{SyscallResultTrait, SyscallResult}; +use starknet::storage_access::{StorageAddress, StorageBaseAddress}; +use core::traits::Into; +use core::poseidon::poseidon_hash_span; +use core::serde::Serde; use dojo::packing::{pack, unpack, calculate_packed_size}; -fn get(address_domain: u32, keys: Span) -> felt252 { - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - starknet::storage_read_syscall(address_domain, starknet::storage_address_from_base(base)) +pub fn get(address_domain: u32, keys: Span) -> felt252 { + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + starknet::syscalls::storage_read_syscall( + address_domain, starknet::storage_access::storage_address_from_base(base) + ) .unwrap_syscall() } -fn get_many( +pub fn get_many( address_domain: u32, keys: Span, mut layout: Span ) -> SyscallResult> { - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - let base_address = starknet::storage_address_from_base(base); + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + let base_address = starknet::storage_access::storage_address_from_base(base); let mut packed = ArrayTrait::new(); @@ -31,7 +38,9 @@ fn get_many( let value = match starknet::syscalls::storage_read_syscall( address_domain, - starknet::storage_address_from_base_and_offset(chunk_base, index_in_chunk) + starknet::storage_access::storage_address_from_base_and_offset( + chunk_base, index_in_chunk + ) ) { Result::Ok(value) => value, Result::Err(err) => { break SyscallResult::>::Err(err); }, @@ -64,23 +73,27 @@ fn get_many( } -fn set(address_domain: u32, keys: Span, value: felt252) { - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - starknet::storage_write_syscall( - address_domain, starknet::storage_address_from_base(base), value +pub fn set(address_domain: u32, keys: Span, value: felt252) { + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + starknet::syscalls::storage_write_syscall( + address_domain, starknet::storage_access::storage_address_from_base(base), value ) .unwrap_syscall(); } -fn set_many( +pub fn set_many( address_domain: u32, keys: Span, mut unpacked: Span, offset: u32, mut layout: Span ) -> SyscallResult<()> { - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - let base_address = starknet::storage_address_from_base(base); + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + let base_address = starknet::storage_access::storage_address_from_base(base); let mut packed = ArrayTrait::new(); pack(ref packed, ref unpacked, offset, ref layout); @@ -97,7 +110,9 @@ fn set_many( match starknet::syscalls::storage_write_syscall( address_domain, - starknet::storage_address_from_base_and_offset(chunk_base, index_in_chunk), + starknet::storage_access::storage_address_from_base_and_offset( + chunk_base, index_in_chunk + ), curr_value.into() ) { Result::Ok(_) => {}, @@ -117,12 +132,14 @@ fn set_many( } } -fn set_packed_array( +pub fn set_packed_array( address_domain: u32, keys: Span, mut data: Span, offset: u32, array_size: u32 ) -> SyscallResult<()> { // write data+offset by chunk of 256 felts - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - let base_address = starknet::storage_address_from_base(base); + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + let base_address = starknet::storage_access::storage_address_from_base(base); let mut chunk = 0; let mut chunk_base = base; @@ -138,7 +155,9 @@ fn set_packed_array( match starknet::syscalls::storage_write_syscall( address_domain, - starknet::storage_address_from_base_and_offset(chunk_base, index_in_chunk), + starknet::storage_access::storage_address_from_base_and_offset( + chunk_base, index_in_chunk + ), curr_value.into() ) { Result::Ok(_) => {}, @@ -160,15 +179,17 @@ fn set_packed_array( } } -fn get_packed_array( +pub fn get_packed_array( address_domain: u32, keys: Span, array_size: u32 ) -> SyscallResult> { if array_size == 0 { return SyscallResult::>::Ok(array![].span()); } - let base = starknet::storage_base_address_from_felt252(poseidon_hash_span(keys)); - let base_address = starknet::storage_address_from_base(base); + let base = starknet::storage_access::storage_base_address_from_felt252( + poseidon_hash_span(keys) + ); + let base_address = starknet::storage_access::storage_address_from_base(base); let mut packed = ArrayTrait::new(); @@ -180,7 +201,9 @@ fn get_packed_array( let value = match starknet::syscalls::storage_read_syscall( address_domain, - starknet::storage_address_from_base_and_offset(chunk_base, index_in_chunk) + starknet::storage_access::storage_address_from_base_and_offset( + chunk_base, index_in_chunk + ) ) { Result::Ok(value) => value, Result::Err(err) => { break SyscallResult::>::Err(err); }, @@ -209,6 +232,5 @@ fn get_packed_array( fn chunk_segment_pointer(address: StorageAddress, chunk: felt252) -> StorageBaseAddress { let p = poseidon_hash_span(array![address.into(), chunk, 'DojoStorageChunk'].span()); - //let (r, _, _) = core::poseidon::hades_permutation(address.into(), chunk, 'DojoStorageChunk'_felt252); - starknet::storage_base_address_from_felt252(p) + starknet::storage_access::storage_base_address_from_felt252(p) } diff --git a/crates/dojo-core/src/database/storage_test.cairo b/crates/dojo-core/src/database/storage_test.cairo index 36e5863e17..86eee00517 100644 --- a/crates/dojo-core/src/database/storage_test.cairo +++ b/crates/dojo-core/src/database/storage_test.cairo @@ -1,7 +1,6 @@ -use array::ArrayTrait; -use array::SpanTrait; -use traits::Into; -use debug::PrintTrait; +use core::array::ArrayTrait; +use core::array::SpanTrait; +use core::traits::Into; use dojo::database::storage; diff --git a/crates/dojo-core/src/database/utils.cairo b/crates/dojo-core/src/database/utils.cairo index 9c070c1821..89f533effd 100644 --- a/crates/dojo-core/src/database/utils.cairo +++ b/crates/dojo-core/src/database/utils.cairo @@ -1,4 +1,4 @@ -fn any_none(arr: @Array>) -> bool { +pub fn any_none(arr: @Array>) -> bool { let mut i = 0; let mut res = false; loop { @@ -15,7 +15,7 @@ fn any_none(arr: @Array>) -> bool { res } -fn sum(arr: Array>) -> u32 { +pub fn sum(arr: Array>) -> u32 { let mut i = 0; let mut res = 0; diff --git a/crates/dojo-core/src/database_test.cairo b/crates/dojo-core/src/database_test.cairo index 7e002a9cfa..7b653f59e4 100644 --- a/crates/dojo-core/src/database_test.cairo +++ b/crates/dojo-core/src/database_test.cairo @@ -1,12 +1,12 @@ use core::result::ResultTrait; -use array::ArrayTrait; -use option::OptionTrait; -use serde::Serde; -use array::SpanTrait; -use traits::{Into, TryInto}; +use core::array::ArrayTrait; +use core::option::OptionTrait; +use core::serde::Serde; +use core::array::SpanTrait; +use core::traits::{Into, TryInto}; use starknet::syscalls::deploy_syscall; -use starknet::class_hash::{Felt252TryIntoClassHash, ClassHash}; +use starknet::class_hash::ClassHash; use dojo::world::{IWorldDispatcher}; use dojo::database::{get, set, MAX_ARRAY_LENGTH}; use dojo::test_utils::assert_array; diff --git a/crates/dojo-core/src/interfaces.cairo b/crates/dojo-core/src/interfaces.cairo index 853305fc4f..045a2d70a3 100644 --- a/crates/dojo-core/src/interfaces.cairo +++ b/crates/dojo-core/src/interfaces.cairo @@ -1,25 +1,25 @@ use starknet::ContractAddress; #[derive(Drop, Serde)] -struct StorageUpdate { - key: felt252, - value: felt252, +pub struct StorageUpdate { + pub key: felt252, + pub value: felt252, } #[derive(Drop, Serde)] -struct ProgramOutput { - prev_state_root: felt252, - new_state_root: felt252, - block_number: felt252, - block_hash: felt252, - config_hash: felt252, - world_da_hash: felt252, - message_to_starknet_segment: Span, - message_to_appchain_segment: Span, +pub struct ProgramOutput { + pub prev_state_root: felt252, + pub new_state_root: felt252, + pub block_number: felt252, + pub block_hash: felt252, + pub config_hash: felt252, + pub world_da_hash: felt252, + pub message_to_starknet_segment: Span, + pub message_to_appchain_segment: Span, } #[starknet::interface] -trait IUpgradeableState { +pub trait IUpgradeableState { fn upgrade_state( ref self: TContractState, new_state: Span, @@ -29,7 +29,7 @@ trait IUpgradeableState { } #[starknet::interface] -trait IFactRegistry { +pub trait IFactRegistry { fn is_valid(self: @TContractState, fact: felt252) -> bool; } diff --git a/crates/dojo-core/src/lib.cairo b/crates/dojo-core/src/lib.cairo index 627cf21739..662cdadff4 100644 --- a/crates/dojo-core/src/lib.cairo +++ b/crates/dojo-core/src/lib.cairo @@ -1,22 +1,22 @@ -mod base; +pub mod base; #[cfg(test)] mod base_test; -mod config; -mod database; +pub mod config; +pub mod database; #[cfg(test)] mod database_test; -mod interfaces; -mod model; +pub mod interfaces; +pub mod model; #[cfg(test)] mod model_test; -mod contract; -mod packing; +pub mod contract; +pub mod packing; #[cfg(test)] mod packing_test; -mod world; +pub mod world; #[cfg(test)] mod world_test; -mod utils; +pub mod utils; #[cfg(test)] mod utils_test; @@ -30,10 +30,10 @@ mod utils_test; // Since `test_utils` is using `TEST_CLASS_HASH` to factorize some deployment // core, we place it under the test target manually. #[cfg(target: "test")] -mod test_utils; +pub mod test_utils; #[cfg(test)] mod benchmarks; -mod components; -mod resource_metadata; +pub mod components; +pub mod resource_metadata; diff --git a/crates/dojo-core/src/model.cairo b/crates/dojo-core/src/model.cairo index 3ebbd5e0bc..164eeffa1f 100644 --- a/crates/dojo-core/src/model.cairo +++ b/crates/dojo-core/src/model.cairo @@ -2,7 +2,7 @@ use dojo::world::{IWorldDispatcher, ModelIndex}; use starknet::SyscallResult; /// Trait that is implemented at Cairo level for each struct that is a model. -trait ModelEntity { +pub trait ModelEntity { fn id(self: @T) -> felt252; fn values(self: @T) -> Span; fn from_values(entity_id: felt252, values: Span) -> T; @@ -15,7 +15,7 @@ trait ModelEntity { fn set_member(self: @T, world: IWorldDispatcher, member_id: felt252, values: Span,); } -trait Model { +pub trait Model { fn get(world: IWorldDispatcher, keys: Span) -> T; // Note: `get` is implemented with a generated trait because it takes // the list of model keys as separated parameters. @@ -56,7 +56,7 @@ trait Model { } #[starknet::interface] -trait IModel { +pub trait IModel { fn name(self: @T) -> ByteArray; fn namespace(self: @T) -> ByteArray; fn tag(self: @T) -> ByteArray; @@ -78,10 +78,10 @@ trait IModel { /// /// * `salt` - A salt used to uniquely deploy the model. /// * `class_hash` - Class Hash of the model. -fn deploy_and_get_metadata( +pub fn deploy_and_get_metadata( salt: felt252, class_hash: starknet::ClassHash ) -> SyscallResult<(starknet::ContractAddress, ByteArray, felt252, ByteArray, felt252)> { - let (contract_address, _) = starknet::deploy_syscall( + let (contract_address, _) = starknet::syscalls::deploy_syscall( class_hash, salt, array![].span(), false, )?; let model = IModelDispatcher { contract_address }; diff --git a/crates/dojo-core/src/model_test.cairo b/crates/dojo-core/src/model_test.cairo index 68a17d604d..b8734934a1 100644 --- a/crates/dojo-core/src/model_test.cairo +++ b/crates/dojo-core/src/model_test.cairo @@ -6,7 +6,7 @@ fn deploy_world() -> IWorldDispatcher { spawn_test_world("dojo", array![]) } -#[derive(Copy, Drop, Serde)] +#[derive(Copy, Drop, Serde, Debug)] #[dojo::model] struct Foo { #[key] diff --git a/crates/dojo-core/src/packing.cairo b/crates/dojo-core/src/packing.cairo index 22676d53ed..39f5f3d8d7 100644 --- a/crates/dojo-core/src/packing.cairo +++ b/crates/dojo-core/src/packing.cairo @@ -1,12 +1,11 @@ use starknet::{ClassHash, ContractAddress}; -use array::{ArrayTrait, SpanTrait}; -use traits::{Into, TryInto}; -use integer::{U256BitAnd, U256BitOr, U256BitXor, upcast, downcast, BoundedInt}; -use option::OptionTrait; +use core::array::{ArrayTrait, SpanTrait}; +use core::traits::{Into, TryInto}; +use core::option::OptionTrait; const PACKING_MAX_BITS: u8 = 251; -fn pack( +pub fn pack( ref packed: Array, ref unpacked: Span, offset: u32, ref layout: Span ) { assert((unpacked.len() - offset) >= layout.len(), 'mismatched input lens'); @@ -28,7 +27,7 @@ fn pack( packed.append(packing); } -fn calculate_packed_size(ref layout: Span) -> usize { +pub fn calculate_packed_size(ref layout: Span) -> usize { let mut size = 1; let mut partial = 0_usize; @@ -49,7 +48,7 @@ fn calculate_packed_size(ref layout: Span) -> usize { size } -fn unpack(ref unpacked: Array, ref packed: Span, ref layout: Span) { +pub fn unpack(ref unpacked: Array, ref packed: Span, ref layout: Span) { let mut unpacking: felt252 = 0x0; let mut offset: u8 = PACKING_MAX_BITS; loop { @@ -60,7 +59,7 @@ fn unpack(ref unpacked: Array, ref packed: Span, ref layout: S Option::None(_) => { // Layout value was successfully poped, // we are then expecting an unpacked value. - panic_with_felt252('Unpack inner failed'); + core::panic_with_felt252('Unpack inner failed'); } } }, @@ -70,7 +69,7 @@ fn unpack(ref unpacked: Array, ref packed: Span, ref layout: S } /// Pack the proposal fields into a single felt252. -fn pack_inner( +pub fn pack_inner( self: @felt252, size: u8, ref packing: felt252, @@ -104,7 +103,7 @@ fn pack_inner( packing_offset = packing_offset + size; } -fn unpack_inner( +pub fn unpack_inner( size: u8, ref packed: Span, ref unpacking: felt252, ref unpacking_offset: u8 ) -> Option { let remaining_bits: u8 = (PACKING_MAX_BITS - unpacking_offset).into(); @@ -136,9 +135,9 @@ fn unpack_inner( return result.try_into(); } -fn fpow(x: u256, n: u8) -> u256 { - if x.is_zero() { - panic_with_felt252('base 0 not allowed in fpow'); +pub fn fpow(x: u256, n: u8) -> u256 { + if x == 0 { + core::panic_with_felt252('base 0 not allowed in fpow'); } let y = x; @@ -155,19 +154,19 @@ fn fpow(x: u256, n: u8) -> u256 { return double; } -fn shl(x: u256, n: u8) -> u256 { +pub fn shl(x: u256, n: u8) -> u256 { x * pow2_const(n) } -fn shr(x: u256, n: u8) -> u256 { +pub fn shr(x: u256, n: u8) -> u256 { x / pow2_const(n) } -fn pow2_const(n: u8) -> u256 { +pub fn pow2_const(n: u8) -> u256 { *POW_2.span().at(n.into()) } -const POW_2: [ +pub const POW_2: [ u256 ; 256] = [ 1, diff --git a/crates/dojo-core/src/packing_test.cairo b/crates/dojo-core/src/packing_test.cairo index 5a26c19e3b..b6986d2e99 100644 --- a/crates/dojo-core/src/packing_test.cairo +++ b/crates/dojo-core/src/packing_test.cairo @@ -1,12 +1,10 @@ -use array::{ArrayTrait, SpanTrait}; -use starknet::{ClassHash, ContractAddress, Felt252TryIntoContractAddress, Felt252TryIntoClassHash}; +use core::array::{ArrayTrait, SpanTrait}; +use starknet::{ClassHash, ContractAddress}; use dojo::packing::{ shl, shr, fpow, pack, unpack, pack_inner, unpack_inner, calculate_packed_size, pow2_const }; -use integer::U256BitAnd; -use option::OptionTrait; -use debug::PrintTrait; -use traits::{Into, TryInto}; +use core::option::OptionTrait; +use core::traits::{Into, TryInto}; use dojo::database::introspect::Introspect; #[test] @@ -46,7 +44,7 @@ fn test_bit_shift() { #[test] #[available_gas(9000000)] fn test_pack_unpack_single() { - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; pack_inner(@18, 251, ref packing, ref offset, ref packed); @@ -65,7 +63,7 @@ fn test_pack_unpack_single() { #[test] #[available_gas(9000000)] fn test_pack_unpack_felt252_u128() { - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; pack_inner(@1337, 128, ref packing, ref offset, ref packed); @@ -88,7 +86,7 @@ fn test_pack_unpack_felt252_u128() { #[test] #[available_gas(100000000)] fn test_pack_multiple() { - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; @@ -114,7 +112,7 @@ fn test_pack_multiple() { #[test] #[available_gas(500000000)] fn test_pack_unpack_multiple() { - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; @@ -157,7 +155,7 @@ fn test_pack_unpack_multiple() { #[test] #[available_gas(500000000)] fn test_pack_unpack_types() { - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; @@ -169,9 +167,9 @@ fn test_pack_unpack_types() { pack_inner(@58, 251, ref packing, ref offset, ref packed); pack_inner(@false.into(), 1, ref packing, ref offset, ref packed); - let contract_address = Felt252TryIntoContractAddress::try_into(3).unwrap(); + let contract_address = starknet::contract_address_const::<3>(); pack_inner(@contract_address.into(), 251, ref packing, ref offset, ref packed); - let class_hash = Felt252TryIntoClassHash::try_into(1337).unwrap(); + let class_hash = starknet::class_hash::class_hash_const::<1337>(); pack_inner(@class_hash.into(), 251, ref packing, ref offset, ref packed); packed.append(packing); @@ -243,7 +241,7 @@ fn test_pack_unpack_types() { #[available_gas(9000000)] fn test_inner_pack_unpack_u256_single() { let input: u256 = 2000; - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; pack_inner(@input.low.into(), 128, ref packing, ref offset, ref packed); @@ -278,7 +276,7 @@ fn test_pack_unpack_u256_single() { let mut unpacked_span = unpacked.span(); - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); pack(ref packed, ref unpacked_span, 0, ref layout_span); let mut layout = ArrayTrait::new(); @@ -286,11 +284,11 @@ fn test_pack_unpack_u256_single() { layout.append(128); let mut layout_span = layout.span(); - let mut unpacked = array::ArrayTrait::new(); + let mut unpacked = core::array::ArrayTrait::new(); let mut packed_span = packed.span(); unpack(ref unpacked, ref packed_span, ref layout_span); let mut unpacked_span = unpacked.span(); - let output = serde::Serde::::deserialize(ref unpacked_span).unwrap(); + let output = core::serde::Serde::::deserialize(ref unpacked_span).unwrap(); assert(input == output, 'invalid output'); } @@ -298,7 +296,7 @@ fn test_pack_unpack_u256_single() { #[available_gas(9000000)] fn test_pack_unpack_max_felt252() { let MAX: felt252 = 3618502788666131213697322783095070105623107215331596699973092056135872020480; - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); let mut packing: felt252 = 0; let mut offset = 0; pack_inner(@MAX, 251, ref packing, ref offset, ref packed); @@ -324,18 +322,18 @@ fn test_pack_unpack_felt252_single() { let mut unpacked_span = unpacked.span(); - let mut packed = array::ArrayTrait::new(); + let mut packed = core::array::ArrayTrait::new(); pack(ref packed, ref unpacked_span, 0, ref layout_span); let mut layout = ArrayTrait::new(); layout.append(251); let mut layout_span = layout.span(); - let mut unpacked = array::ArrayTrait::new(); + let mut unpacked = core::array::ArrayTrait::new(); let mut packed_span = packed.span(); unpack(ref unpacked, ref packed_span, ref layout_span); let mut unpacked_span = unpacked.span(); - let output = serde::Serde::::deserialize(ref unpacked_span).unwrap(); + let output = core::serde::Serde::::deserialize(ref unpacked_span).unwrap(); assert(input == output, 'invalid output'); } diff --git a/crates/dojo-core/src/resource_metadata.cairo b/crates/dojo-core/src/resource_metadata.cairo index dde35752e1..7f891c2a15 100644 --- a/crates/dojo-core/src/resource_metadata.cairo +++ b/crates/dojo-core/src/resource_metadata.cairo @@ -7,39 +7,43 @@ use dojo::world::{IWorldDispatcherTrait, ModelIndex}; use dojo::model::Model; use dojo::utils; -fn initial_address() -> starknet::ContractAddress { +pub fn initial_address() -> starknet::ContractAddress { starknet::contract_address_const::<0>() } -fn initial_class_hash() -> starknet::ClassHash { - starknet::class_hash_const::< +pub fn initial_class_hash() -> starknet::ClassHash { + starknet::class_hash::class_hash_const::< 0x03f75587469e8101729b3b02a46150a3d99315bc9c5026d64f2e8a061e413255 >() } #[derive(Drop, Serde, PartialEq, Clone, Debug)] -struct ResourceMetadata { +pub struct ResourceMetadata { // #[key] - resource_id: felt252, - metadata_uri: ByteArray, + pub resource_id: felt252, + pub metadata_uri: ByteArray, } -impl ResourceMetadataModel of dojo::model::Model { - fn get(world: dojo::world::IWorldDispatcher, keys: Span) -> ResourceMetadata { - let values = world.entity(Self::selector(), ModelIndex::Keys(keys), Self::layout()); - let mut serialized = core::array::ArrayTrait::new(); - core::array::serialize_array_helper(keys, ref serialized); - core::array::serialize_array_helper(values, ref serialized); - let mut serialized = core::array::ArrayTrait::span(@serialized); - let entity = core::serde::Serde::::deserialize(ref serialized); - - if core::option::OptionTrait::::is_none(@entity) { - panic!( - "Model `ResourceMetadata`: deserialization failed. Ensure the length of the keys tuple is matching the number of #[key] fields in the model struct." - ); +#[generate_trait] +pub impl ResourceMetadataImpl of ResourceMetadataTrait { + fn from_values(resource_id: felt252, ref values: Span) -> ResourceMetadata { + let metadata_uri = core::serde::Serde::::deserialize(ref values); + if metadata_uri.is_none() { + panic!("Model `ResourceMetadata`: metadata_uri deserialization failed."); } - core::option::OptionTrait::::unwrap(entity) + ResourceMetadata { resource_id, metadata_uri: metadata_uri.unwrap() } + } +} + +pub impl ResourceMetadataModel of dojo::model::Model { + fn get(world: dojo::world::IWorldDispatcher, keys: Span) -> ResourceMetadata { + if keys.len() != 1 { + panic!("Model `ResourceMetadata`: bad keys length."); + }; + + let mut values = world.entity(Self::selector(), ModelIndex::Keys(keys), Self::layout()); + ResourceMetadataTrait::from_values(*keys.at(0), ref values) } fn set(self: @ResourceMetadata, world: dojo::world::IWorldDispatcher,) { @@ -63,7 +67,7 @@ impl ResourceMetadataModel of dojo::model::Model { Self::selector(), ModelIndex::MemberId((entity_id, member_id)), field_layout ) }, - Option::None => panic_with_felt252('bad member id') + Option::None => core::panic_with_felt252('bad member id') } } @@ -83,7 +87,7 @@ impl ResourceMetadataModel of dojo::model::Model { field_layout ) }, - Option::None => panic_with_felt252('bad member id') + Option::None => core::panic_with_felt252('bad member id') } } @@ -107,7 +111,7 @@ impl ResourceMetadataModel of dojo::model::Model { #[inline(always)] fn selector() -> felt252 { - poseidon::poseidon_hash_span(array![Self::namespace_hash(), Self::name_hash()].span()) + core::poseidon::poseidon_hash_span(array![Self::namespace_hash(), Self::name_hash()].span()) } #[inline(always)] @@ -125,7 +129,7 @@ impl ResourceMetadataModel of dojo::model::Model { #[inline(always)] fn entity_id(self: @ResourceMetadata) -> felt252 { - poseidon::poseidon_hash_span(self.keys()) + core::poseidon::poseidon_hash_span(self.keys()) } #[inline(always)] @@ -158,7 +162,9 @@ impl ResourceMetadataModel of dojo::model::Model { } } -impl ResourceMetadataIntrospect<> of dojo::database::introspect::Introspect> { +pub impl ResourceMetadataIntrospect<> of dojo::database::introspect::Introspect< + ResourceMetadata<> +> { #[inline(always)] fn size() -> Option { Option::None @@ -202,7 +208,7 @@ impl ResourceMetadataIntrospect<> of dojo::database::introspect::Introspect) -> ContractAddress { - let (contract, _) = starknet::deploy_syscall(class_hash.try_into().unwrap(), 0, calldata, false) +pub fn deploy_contract(class_hash: felt252, calldata: Span) -> ContractAddress { + let (contract, _) = starknet::syscalls::deploy_syscall( + class_hash.try_into().unwrap(), 0, calldata, false + ) .unwrap(); contract } @@ -36,12 +34,12 @@ fn deploy_contract(class_hash: felt252, calldata: Span) -> ContractAddr /// /// # Returns /// * address of contract deployed -fn deploy_with_world_address(class_hash: felt252, world: IWorldDispatcher) -> ContractAddress { +pub fn deploy_with_world_address(class_hash: felt252, world: IWorldDispatcher) -> ContractAddress { deploy_contract(class_hash, array![world.contract_address.into()].span()) } -fn spawn_test_world(namespace: ByteArray, models: Array) -> IWorldDispatcher { - let salt = testing::get_available_gas(); +pub fn spawn_test_world(namespace: ByteArray, models: Array) -> IWorldDispatcher { + let salt = core::testing::get_available_gas(); // deploy world let (world_address, _) = deploy_syscall( @@ -72,24 +70,24 @@ fn spawn_test_world(namespace: ByteArray, models: Array) -> IWorldDispa #[derive(Drop)] -struct GasCounter { - start: u128, +pub struct GasCounter { + pub start: u128, } #[generate_trait] -impl GasCounterImpl of GasCounterTrait { +pub impl GasCounterImpl of GasCounterTrait { fn start() -> GasCounter { - let start = testing::get_available_gas(); - gas::withdraw_gas().unwrap(); + let start = core::testing::get_available_gas(); + core::gas::withdraw_gas().unwrap(); GasCounter { start } } fn end(self: GasCounter, name: ByteArray) { - let end = testing::get_available_gas(); + let end = core::testing::get_available_gas(); let gas_used = self.start - end; println!("# GAS # {}: {}", Self::pad_start(name, 18), gas_used); - gas::withdraw_gas().unwrap(); + core::gas::withdraw_gas().unwrap(); } fn pad_start(str: ByteArray, len: u32) -> ByteArray { @@ -108,7 +106,7 @@ impl GasCounterImpl of GasCounterTrait { } // assert that `value` and `expected` have the same size and the same content -fn assert_array(value: Span, expected: Span) { +pub fn assert_array(value: Span, expected: Span) { assert!(value.len() == expected.len(), "Bad array length"); let mut i = 0; diff --git a/crates/dojo-core/src/utils.cairo b/crates/dojo-core/src/utils.cairo index 860bbe5759..ba9fb3f01b 100644 --- a/crates/dojo-core/src/utils.cairo +++ b/crates/dojo-core/src/utils.cairo @@ -1,8 +1,8 @@ /// Compute the poseidon hash of a serialized ByteArray -fn hash(data: @ByteArray) -> felt252 { +pub fn hash(data: @ByteArray) -> felt252 { let mut serialized = ArrayTrait::new(); - Serde::serialize(data, ref serialized); - poseidon::poseidon_hash_span(serialized.span()) + core::serde::Serde::serialize(data, ref serialized); + core::poseidon::poseidon_hash_span(serialized.span()) } /// Computes the entity id from the keys. @@ -14,8 +14,8 @@ fn hash(data: @ByteArray) -> felt252 { /// # Returns /// /// The entity id. -fn entity_id_from_keys(keys: Span) -> felt252 { - poseidon::poseidon_hash_span(keys) +pub fn entity_id_from_keys(keys: Span) -> felt252 { + core::poseidon::poseidon_hash_span(keys) } /// Find the layout of a model field based on its selector. @@ -27,7 +27,7 @@ fn entity_id_from_keys(keys: Span) -> felt252 { /// /// # Returns /// Some(Layout) if the field layout has been found, None otherwise. -fn find_model_field_layout( +pub fn find_model_field_layout( model_layout: dojo::database::introspect::Layout, member_selector: felt252 ) -> Option { match model_layout { @@ -49,7 +49,7 @@ fn find_model_field_layout( }, _ => { // should never happen as model layouts are always struct layouts. - panic_with_felt252('Unexpected model layout'); + core::panic_with_felt252('Unexpected model layout'); Option::None } } diff --git a/crates/dojo-core/src/world.cairo b/crates/dojo-core/src/world.cairo index 0c88c9ac15..0a6fa9c775 100644 --- a/crates/dojo-core/src/world.cairo +++ b/crates/dojo-core/src/world.cairo @@ -1,10 +1,10 @@ -use starknet::{ContractAddress, ClassHash, StorageBaseAddress, SyscallResult}; -use traits::{Into, TryInto}; -use option::OptionTrait; +use starknet::{ContractAddress, ClassHash, storage_access::StorageBaseAddress, SyscallResult}; +use core::traits::{Into, TryInto}; +use core::option::OptionTrait; use dojo::resource_metadata::ResourceMetadata; #[derive(Copy, Drop, Serde, Debug, PartialEq)] -enum ModelIndex { +pub enum ModelIndex { Keys: Span, Id: felt252, // (entity_id, member_id) @@ -12,7 +12,7 @@ enum ModelIndex { } #[starknet::interface] -trait IWorld { +pub trait IWorld { fn metadata(self: @T, resource_id: felt252) -> ResourceMetadata; fn set_metadata(ref self: T, metadata: ResourceMetadata); fn model(self: @T, selector: felt252) -> (ClassHash, ContractAddress); @@ -66,67 +66,67 @@ trait IWorld { } #[starknet::interface] -trait IUpgradeableWorld { +pub trait IUpgradeableWorld { fn upgrade(ref self: T, new_class_hash: ClassHash); } #[starknet::interface] -trait IWorldProvider { +pub trait IWorldProvider { fn world(self: @T) -> IWorldDispatcher; } -mod Errors { - const METADATA_DESER: felt252 = 'metadata deser error'; - const NOT_OWNER: felt252 = 'not owner'; - const NOT_OWNER_WRITER: felt252 = 'not owner or writer'; - const NO_WRITE_ACCESS: felt252 = 'no write access'; - const NO_MODEL_WRITE_ACCESS: felt252 = 'no model write access'; - const NO_NAMESPACE_WRITE_ACCESS: felt252 = 'no namespace write access'; - const NAMESPACE_NOT_REGISTERED: felt252 = 'namespace not registered'; - const NOT_REGISTERED: felt252 = 'resource not registered'; - const INVALID_MODEL_NAME: felt252 = 'invalid model name'; - const INVALID_NAMESPACE_NAME: felt252 = 'invalid namespace name'; - const INVALID_RESOURCE_SELECTOR: felt252 = 'invalid resource selector'; - const OWNER_ONLY_UPGRADE: felt252 = 'only owner can upgrade'; - const OWNER_ONLY_UPDATE: felt252 = 'only owner can update'; - const NAMESPACE_ALREADY_REGISTERED: felt252 = 'namespace already registered'; - const DELETE_ENTITY_MEMBER: felt252 = 'cannot delete entity member'; - const UNEXPECTED_ERROR: felt252 = 'unexpected error'; +pub mod Errors { + pub const METADATA_DESER: felt252 = 'metadata deser error'; + pub const NOT_OWNER: felt252 = 'not owner'; + pub const NOT_OWNER_WRITER: felt252 = 'not owner or writer'; + pub const NO_WRITE_ACCESS: felt252 = 'no write access'; + pub const NO_MODEL_WRITE_ACCESS: felt252 = 'no model write access'; + pub const NO_NAMESPACE_WRITE_ACCESS: felt252 = 'no namespace write access'; + pub const NAMESPACE_NOT_REGISTERED: felt252 = 'namespace not registered'; + pub const NOT_REGISTERED: felt252 = 'resource not registered'; + pub const INVALID_MODEL_NAME: felt252 = 'invalid model name'; + pub const INVALID_NAMESPACE_NAME: felt252 = 'invalid namespace name'; + pub const INVALID_RESOURCE_SELECTOR: felt252 = 'invalid resource selector'; + pub const OWNER_ONLY_UPGRADE: felt252 = 'only owner can upgrade'; + pub const OWNER_ONLY_UPDATE: felt252 = 'only owner can update'; + pub const NAMESPACE_ALREADY_REGISTERED: felt252 = 'namespace already registered'; + pub const DELETE_ENTITY_MEMBER: felt252 = 'cannot delete entity member'; + pub const UNEXPECTED_ERROR: felt252 = 'unexpected error'; } #[starknet::contract] -mod world { +pub mod world { use dojo::config::interface::IConfig; use core::to_byte_array::FormatAsByteArray; use core::traits::TryInto; - use array::{ArrayTrait, SpanTrait}; - use traits::Into; - use option::OptionTrait; - use box::BoxTrait; + use core::array::{ArrayTrait, SpanTrait}; + use core::traits::Into; + use core::option::OptionTrait; + use core::box::BoxTrait; use starknet::event::EventEmitter; - use serde::Serde; + use core::serde::Serde; use core::hash::{HashStateExTrait, HashStateTrait}; - use pedersen::{PedersenTrait, HashStateImpl, PedersenImpl}; + use core::pedersen::PedersenTrait; use starknet::{ - contract_address_const, get_caller_address, get_contract_address, get_tx_info, - contract_address::ContractAddressIntoFelt252, ClassHash, Zeroable, ContractAddress, - syscalls::{deploy_syscall, emit_event_syscall, replace_class_syscall}, SyscallResult, - SyscallResultTrait, SyscallResultTraitImpl, storage::Map, + contract_address_const, get_caller_address, get_contract_address, get_tx_info, ClassHash, + ContractAddress, syscalls::{deploy_syscall, emit_event_syscall, replace_class_syscall}, + SyscallResult, SyscallResultTrait, storage::Map, }; + use core::num::traits::Zero; use dojo::database; use dojo::database::introspect::{Introspect, Layout, FieldLayout}; use dojo::components::upgradeable::{IUpgradeableDispatcher, IUpgradeableDispatcherTrait}; use dojo::contract::{IContractDispatcher, IContractDispatcherTrait}; use dojo::config::component::Config; - use dojo::model::{Model, IModelDispatcher, IModelDispatcherImpl}; + use dojo::model::{Model, IModelDispatcher, IModelDispatcherTrait}; use dojo::interfaces::{ - IUpgradeableState, IFactRegistryDispatcher, IFactRegistryDispatcherImpl, StorageUpdate, + IUpgradeableState, IFactRegistryDispatcher, IFactRegistryDispatcherTrait, StorageUpdate, ProgramOutput }; use dojo::world::{IWorldDispatcher, IWorld, IUpgradeableWorld}; use dojo::resource_metadata; - use dojo::resource_metadata::ResourceMetadata; + use dojo::resource_metadata::{ResourceMetadata, ResourceMetadataTrait}; use dojo::utils::entity_id_from_keys; use super::{Errors, ModelIndex}; @@ -164,90 +164,90 @@ mod world { } #[derive(Drop, starknet::Event)] - struct StateUpdated { - da_hash: felt252, + pub struct StateUpdated { + pub da_hash: felt252, } #[derive(Drop, starknet::Event)] - struct WorldSpawned { - address: ContractAddress, - creator: ContractAddress + pub struct WorldSpawned { + pub address: ContractAddress, + pub creator: ContractAddress } #[derive(Drop, starknet::Event)] - struct WorldUpgraded { - class_hash: ClassHash, + pub struct WorldUpgraded { + pub class_hash: ClassHash, } #[derive(Drop, starknet::Event)] - struct ContractDeployed { - salt: felt252, - class_hash: ClassHash, - address: ContractAddress, - namespace: ByteArray, - name: ByteArray + pub struct ContractDeployed { + pub salt: felt252, + pub class_hash: ClassHash, + pub address: ContractAddress, + pub namespace: ByteArray, + pub name: ByteArray } #[derive(Drop, starknet::Event)] - struct ContractUpgraded { - class_hash: ClassHash, - address: ContractAddress, + pub struct ContractUpgraded { + pub class_hash: ClassHash, + pub address: ContractAddress, } #[derive(Drop, starknet::Event)] - struct MetadataUpdate { - resource: felt252, - uri: ByteArray + pub struct MetadataUpdate { + pub resource: felt252, + pub uri: ByteArray } #[derive(Drop, starknet::Event, Debug, PartialEq)] pub struct NamespaceRegistered { - namespace: ByteArray, - hash: felt252 + pub namespace: ByteArray, + pub hash: felt252 } #[derive(Drop, starknet::Event)] - struct ModelRegistered { - name: ByteArray, - namespace: ByteArray, - class_hash: ClassHash, - prev_class_hash: ClassHash, - address: ContractAddress, - prev_address: ContractAddress, + pub struct ModelRegistered { + pub name: ByteArray, + pub namespace: ByteArray, + pub class_hash: ClassHash, + pub prev_class_hash: ClassHash, + pub address: ContractAddress, + pub prev_address: ContractAddress, } #[derive(Drop, starknet::Event)] - struct StoreSetRecord { - table: felt252, - keys: Span, - values: Span, + pub struct StoreSetRecord { + pub table: felt252, + pub keys: Span, + pub values: Span, } #[derive(Drop, starknet::Event)] - struct StoreUpdateRecord { - table: felt252, - entity_id: felt252, - values: Span, + pub struct StoreUpdateRecord { + pub table: felt252, + pub entity_id: felt252, + pub values: Span, } #[derive(Drop, starknet::Event)] - struct StoreDelRecord { - table: felt252, - entity_id: felt252, + pub struct StoreDelRecord { + pub table: felt252, + pub entity_id: felt252, } #[derive(Drop, starknet::Event)] - struct WriterUpdated { - resource: felt252, - contract: ContractAddress, - value: bool + pub struct WriterUpdated { + pub resource: felt252, + pub contract: ContractAddress, + pub value: bool } #[derive(Drop, starknet::Event)] - struct OwnerUpdated { - address: ContractAddress, - resource: felt252, - value: bool, + pub struct OwnerUpdated { + pub address: ContractAddress, + pub resource: felt252, + pub value: bool, } #[storage] @@ -264,7 +264,7 @@ mod world { } #[derive(Drop, starknet::Store, Default, Debug)] - enum ResourceData { + pub enum ResourceData { Model: (ClassHash, ContractAddress), Contract: (ClassHash, ContractAddress), Namespace, @@ -317,20 +317,13 @@ mod world { /// /// `resource_id` - The resource id. fn metadata(self: @ContractState, resource_id: felt252) -> ResourceMetadata { - let mut model = array![resource_id]; - let entity_id = entity_id_from_keys(model.span()); - let mut data = self - ._read_model_entity( + let mut values = self + .entity( dojo::model::Model::::selector(), - entity_id, - Model::::layout() + ModelIndex::Keys(array![resource_id].span()), + dojo::model::Model::::layout() ); - - core::array::serialize_array_helper(data, ref model); - - let mut model_span = model.span(); - - Serde::::deserialize(ref model_span).expect(Errors::METADATA_DESER) + ResourceMetadataTrait::from_values(resource_id, ref values) } /// Sets the metadata of the resource. @@ -344,12 +337,13 @@ mod world { Errors::NO_WRITE_ACCESS ); - let model = Model::::selector(); - let entity_id = Model::::entity_id(@metadata); - let values = Model::::values(@metadata); - let layout = Model::::layout(); - - self._write_model_entity(model, entity_id, values, layout); + self + ._write_model_entity( + metadata.instance_selector(), + metadata.entity_id(), + metadata.values(), + metadata.instance_layout() + ); EventEmitter::emit( ref self, @@ -484,16 +478,14 @@ mod world { fn can_write_resource( self: @ContractState, resource_id: felt252, contract: ContractAddress ) -> bool { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(resource_id); - match resource { + match self.resources.read(resource_id) { ResourceData::Model((_, model_address)) => self ._check_model_write_access(resource_id, model_address, contract), ResourceData::Contract((_, contract_address)) => self ._check_contract_write_access(resource_id, contract_address, contract), ResourceData::Namespace => self._check_basic_write_access(resource_id, contract), ResourceData::World => self._check_basic_write_access(WORLD, contract), - ResourceData::None => panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) + ResourceData::None => core::panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) } } @@ -514,12 +506,10 @@ mod world { fn can_write_model( self: @ContractState, selector: felt252, contract: ContractAddress ) -> bool { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(selector); - match resource { + match self.resources.read(selector) { ResourceData::Model((_, model_address)) => self ._check_model_write_access(selector, model_address, contract), - _ => panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) + _ => core::panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) } } @@ -540,12 +530,10 @@ mod world { fn can_write_contract( self: @ContractState, selector: felt252, contract: ContractAddress ) -> bool { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(selector); - match resource { + match self.resources.read(selector) { ResourceData::Contract((_, contract_address)) => self ._check_contract_write_access(selector, contract_address, contract), - _ => panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) + _ => core::panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) } } @@ -566,11 +554,9 @@ mod world { fn can_write_namespace( self: @ContractState, selector: felt252, contract: ContractAddress ) -> bool { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(selector); - match resource { + match self.resources.read(selector) { ResourceData::Namespace => self._check_basic_write_access(selector, contract), - _ => panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) + _ => core::panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) } } @@ -592,8 +578,8 @@ mod world { self.models_count.write(salt + 1); let (mut prev_class_hash, mut prev_address) = ( - starknet::class_hash::ClassHashZeroable::zero(), - starknet::contract_address::ContractAddressZeroable::zero(), + core::num::traits::Zero::::zero(), + core::num::traits::Zero::::zero(), ); assert(self._is_namespace_registered(namespace_hash), Errors::NAMESPACE_NOT_REGISTERED); @@ -603,13 +589,10 @@ mod world { ); if selector.is_zero() { - panic_with_felt252(Errors::INVALID_MODEL_NAME); + core::panic_with_felt252(Errors::INVALID_MODEL_NAME); } - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(selector); - - match resource { + match self.resources.read(selector) { // If model is already registered, validate permission to update. ResourceData::Model(( model_hash, model_address @@ -622,7 +605,7 @@ mod world { ResourceData::None => { self.owners.write((selector, caller), true); }, // Avoids a model name to conflict with already registered resource, // which can cause ACL issue with current ACL implementation. - _ => panic_with_felt252(Errors::INVALID_MODEL_NAME) + _ => core::panic_with_felt252(Errors::INVALID_MODEL_NAME) }; self.resources.write(selector, ResourceData::Model((class_hash, address))); @@ -644,12 +627,10 @@ mod world { let hash = dojo::utils::hash(@namespace); - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(hash); - match resource { + match self.resources.read(hash) { ResourceData::Namespace => { if !self.is_account_owner(hash) { - panic_with_felt252(Errors::NAMESPACE_ALREADY_REGISTERED); + core::panic_with_felt252(Errors::NAMESPACE_ALREADY_REGISTERED); } }, ResourceData::None => { @@ -658,7 +639,7 @@ mod world { EventEmitter::emit(ref self, NamespaceRegistered { namespace, hash }); }, - _ => { panic_with_felt252(Errors::INVALID_NAMESPACE_NAME); } + _ => { core::panic_with_felt252(Errors::INVALID_NAMESPACE_NAME); } }; } @@ -673,20 +654,16 @@ mod world { /// /// * (`ClassHash`, `ContractAddress`) - The class hash and the contract address of the model. fn model(self: @ContractState, selector: felt252) -> (ClassHash, ContractAddress) { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(selector); - match resource { + match self.resources.read(selector) { ResourceData::Model(m) => m, - _ => panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) + _ => core::panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) } } fn contract(self: @ContractState, selector: felt252) -> (ClassHash, ContractAddress) { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(selector); - match resource { + match self.resources.read(selector) { ResourceData::Contract(c) => c, - _ => panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) + _ => core::panic_with_felt252(Errors::INVALID_RESOURCE_SELECTOR) } } @@ -730,7 +707,9 @@ mod world { if self.initialized_contract.read(selector) { panic!("Contract has already been initialized"); } else { - starknet::call_contract_syscall(contract_address, DOJO_INIT_SELECTOR, init_calldata) + starknet::syscalls::call_contract_syscall( + contract_address, DOJO_INIT_SELECTOR, init_calldata + ) .unwrap_syscall(); self.initialized_contract.write(selector, true); } @@ -901,7 +880,9 @@ mod world { ref self, StoreDelRecord { table: model_selector, entity_id } ); }, - ModelIndex::MemberId(_) => { panic_with_felt252(Errors::DELETE_ENTITY_MEMBER); } + ModelIndex::MemberId(_) => { + core::panic_with_felt252(Errors::DELETE_ENTITY_MEMBER); + } } } @@ -943,7 +924,7 @@ mod world { program_output: ProgramOutput, program_hash: felt252 ) { - let mut da_hasher = PedersenImpl::new(0); + let mut da_hasher = PedersenTrait::new(0); let mut i = 0; loop { if i == new_state.len() { @@ -964,9 +945,11 @@ mod world { let mut program_output_array = array![]; program_output.serialize(ref program_output_array); - let program_output_hash = poseidon::poseidon_hash_span(program_output_array.span()); + let program_output_hash = core::poseidon::poseidon_hash_span( + program_output_array.span() + ); - let fact = poseidon::PoseidonImpl::new() + let fact = core::poseidon::PoseidonImpl::new() .update(program_hash) .update(program_output_hash) .finalize(); @@ -980,9 +963,13 @@ mod world { if i >= new_state.len() { break; } - let base = starknet::storage_base_address_from_felt252(*new_state.at(i).key); - starknet::storage_write_syscall( - 0, starknet::storage_address_from_base(base), *new_state.at(i).value + let base = starknet::storage_access::storage_base_address_from_felt252( + *new_state.at(i).key + ); + starknet::syscalls::storage_write_syscall( + 0, + starknet::storage_access::storage_address_from_base(base), + *new_state.at(i).value ) .unwrap_syscall(); i += 1; @@ -1043,9 +1030,7 @@ mod world { /// Indicates if the provided namespace is already registered #[inline(always)] fn _is_namespace_registered(self: @ContractState, namespace_hash: felt252) -> bool { - // TODO: use match self.resources... directly when https://github.com/starkware-libs/cairo/pull/5743 fixed - let resource: ResourceData = self.resources.read(namespace_hash); - match resource { + match self.resources.read(namespace_hash) { ResourceData::Namespace => true, _ => false } @@ -1260,7 +1245,7 @@ mod world { /// Combine parent and child keys to build one full key. fn _combine_key(parent_key: felt252, child_key: felt252) -> felt252 { - poseidon::poseidon_hash_span(array![parent_key, child_key].span()) + core::poseidon::poseidon_hash_span(array![parent_key, child_key].span()) } /// Append some values to an array. diff --git a/crates/dojo-core/src/world_test.cairo b/crates/dojo-core/src/world_test.cairo index 952a0d5bb6..fbfd779f32 100644 --- a/crates/dojo-core/src/world_test.cairo +++ b/crates/dojo-core/src/world_test.cairo @@ -1,17 +1,16 @@ -use array::{ArrayTrait, SpanTrait}; -use clone::Clone; +use core::array::{ArrayTrait, SpanTrait}; +use core::clone::Clone; use core::result::ResultTrait; -use traits::{Into, TryInto}; -use option::OptionTrait; -use starknet::class_hash::Felt252TryIntoClassHash; +use core::traits::{Into, TryInto}; +use core::option::OptionTrait; use starknet::{contract_address_const, ContractAddress, ClassHash, get_caller_address}; use starknet::syscalls::deploy_syscall; use dojo::benchmarks; -use dojo::config::interface::{IConfigDispatcher, IConfigDispatcherImpl}; +use dojo::config::interface::{IConfigDispatcher, IConfigDispatcherTrait}; use dojo::world::{ IWorldDispatcher, IWorldDispatcherTrait, world, IUpgradeableWorld, IUpgradeableWorldDispatcher, - IUpgradeableWorldDispatcherTrait, ResourceMetadata, + IUpgradeableWorldDispatcherTrait, }; use dojo::world::world::NamespaceRegistered; use dojo::database::introspect::{Introspect, Layout, FieldLayout}; @@ -21,8 +20,10 @@ use dojo::config::component::Config::{ DifferProgramHashUpdate, MergerProgramHashUpdate, FactsRegistryUpdate }; use dojo::model::Model; -use dojo::benchmarks::{Character, GasCounterImpl}; +use dojo::benchmarks::Character; +use dojo::test_utils::GasCounterTrait; use dojo::utils::entity_id_from_keys; +use dojo::resource_metadata::ResourceMetadata; #[derive(Introspect, Copy, Drop, Serde)] enum OneEnum { @@ -38,20 +39,20 @@ enum AnotherEnum { #[derive(Copy, Drop, Serde)] #[dojo::model] -struct Foo { +pub struct Foo { #[key] - caller: ContractAddress, - a: felt252, - b: u128, + pub caller: ContractAddress, + pub a: felt252, + pub b: u128, } #[derive(Copy, Drop, Serde)] #[dojo::model(namespace: "another_namespace", nomapping: true)] -struct Buzz { +pub struct Buzz { #[key] - caller: ContractAddress, - a: felt252, - b: u128, + pub caller: ContractAddress, + pub a: felt252, + pub b: u128, } @@ -61,19 +62,19 @@ fn create_foo() -> Span { #[derive(Copy, Drop, Serde)] #[dojo::model] -struct Fizz { +pub struct Fizz { #[key] - caller: ContractAddress, - a: felt252 + pub caller: ContractAddress, + pub a: felt252 } #[derive(Copy, Drop, Serde)] #[dojo::model] -struct StructSimpleModel { +pub struct StructSimpleModel { #[key] - caller: ContractAddress, - a: felt252, - b: u128, + pub caller: ContractAddress, + pub a: felt252, + pub b: u128, } fn create_struct_simple_model() -> Span { @@ -82,10 +83,10 @@ fn create_struct_simple_model() -> Span { #[derive(Copy, Drop, Serde)] #[dojo::model] -struct StructWithTuple { +pub struct StructWithTuple { #[key] - caller: ContractAddress, - a: (u8, u64) + pub caller: ContractAddress, + pub a: (u8, u64) } fn create_struct_with_tuple() -> Span { @@ -94,10 +95,10 @@ fn create_struct_with_tuple() -> Span { #[derive(Copy, Drop, Serde)] #[dojo::model] -struct StructWithEnum { +pub struct StructWithEnum { #[key] - caller: ContractAddress, - a: OneEnum, + pub caller: ContractAddress, + pub a: OneEnum, } fn create_struct_with_enum_first_variant() -> Span { @@ -110,12 +111,12 @@ fn create_struct_with_enum_second_variant() -> Span { #[derive(Copy, Drop, Serde)] #[dojo::model] -struct StructSimpleArrayModel { +pub struct StructSimpleArrayModel { #[key] - caller: ContractAddress, - a: felt252, - b: Array, - c: u128, + pub caller: ContractAddress, + pub a: felt252, + pub b: Array, + pub c: u128, } impl ArrayU64Copy of core::traits::Copy>; @@ -126,11 +127,11 @@ fn create_struct_simple_array_model() -> Span { #[derive(Drop, Serde)] #[dojo::model] -struct StructByteArrayModel { +pub struct StructByteArrayModel { #[key] - caller: ContractAddress, - a: felt252, - b: ByteArray, + pub caller: ContractAddress, + pub a: felt252, + pub b: ByteArray, } fn create_struct_byte_array_model() -> Span { @@ -138,20 +139,20 @@ fn create_struct_byte_array_model() -> Span { } #[derive(Introspect, Copy, Drop, Serde)] -struct ModelData { - x: u256, - y: u32, - z: felt252 +pub struct ModelData { + pub x: u256, + pub y: u32, + pub z: felt252 } #[derive(Drop, Serde)] #[dojo::model] -struct StructComplexArrayModel { +pub struct StructComplexArrayModel { #[key] - caller: ContractAddress, - a: felt252, - b: Array, - c: AnotherEnum, + pub caller: ContractAddress, + pub a: felt252, + pub b: Array, + pub c: AnotherEnum, } fn create_struct_complex_array_model() -> Span { @@ -181,11 +182,11 @@ fn create_struct_complex_array_model() -> Span { #[derive(Drop, Serde)] #[dojo::model] -struct StructNestedModel { +pub struct StructNestedModel { #[key] - caller: ContractAddress, - x: (u8, u16, (u32, ByteArray, u8), Array<(u8, u16)>), - y: Array> + pub caller: ContractAddress, + pub x: (u8, u16, (u32, ByteArray, u8), Array<(u8, u16)>), + pub y: Array> } fn create_struct_nested_model() -> Span { @@ -231,17 +232,17 @@ fn create_struct_nested_model() -> Span { } #[derive(Introspect, Copy, Drop, Serde)] -enum EnumGeneric { +pub enum EnumGeneric { One: T, Two: U } #[derive(Drop, Serde)] #[dojo::model] -struct StructWithGeneric { +pub struct StructWithGeneric { #[key] - caller: ContractAddress, - x: EnumGeneric, + pub caller: ContractAddress, + pub x: EnumGeneric, } fn create_struct_generic_first_variant() -> Span { @@ -301,7 +302,7 @@ trait Ibar { mod bar { use super::{Foo, IWorldDispatcher, IWorldDispatcherTrait, Introspect}; use super::benchmarks::{Character, Abilities, Stats, Weapon, Sword}; - use traits::Into; + use core::traits::Into; use starknet::{get_caller_address, ContractAddress}; #[storage] @@ -325,7 +326,7 @@ mod bar { .read() .delete_entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![get_caller_address().into()].span()), + dojo::world::ModelIndex::Keys(array![get_caller_address().into()].span()), dojo::model::Model::::layout() ); } @@ -422,7 +423,6 @@ fn test_delete() { assert(deleted.a == 0, 'data not deleted'); assert(deleted.b == 0, 'data not deleted'); } -use core::debug::PrintTrait; #[test] #[available_gas(6000000)] @@ -805,12 +805,12 @@ fn bench_execute() { let alice = starknet::contract_address_const::<0x1337>(); starknet::testing::set_contract_address(alice); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); bar_contract.set_foo(1337, 1337); gas.end("foo set call"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let data = get!(world, alice, Foo); gas.end("foo get macro"); @@ -827,12 +827,12 @@ fn bench_execute_complex() { let alice = starknet::contract_address_const::<0x1337>(); starknet::testing::set_contract_address(alice); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); bar_contract.set_char(1337, 1337); gas.end("char set call"); - let gas = GasCounterImpl::start(); + let gas = GasCounterTrait::start(); let data = get!(world, alice, Character); gas.end("char get macro"); @@ -914,8 +914,8 @@ fn test_upgradeable_world_from_non_owner() { fn drop_all_events(address: ContractAddress) { loop { match starknet::testing::pop_log_raw(address) { - option::Option::Some(_) => {}, - option::Option::None => { break; }, + core::option::Option::Some(_) => {}, + core::option::Option::None => { break; }, }; } } @@ -984,8 +984,8 @@ fn test_set_entity_by_id() { let values = create_foo(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Id(entity_id), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Id(entity_id), layout); + world.set_entity(selector, dojo::world::ModelIndex::Id(entity_id), values, layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Id(entity_id), layout); assert_array(read_values, values); } @@ -998,8 +998,8 @@ fn test_set_entity_with_fixed_layout() { let values = create_foo(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(get_key_test()), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(get_key_test()), values, layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1013,9 +1013,9 @@ fn test_set_entity_with_struct_layout() { let values = create_struct_simple_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1029,9 +1029,9 @@ fn test_set_entity_with_struct_tuple_layout() { let values = create_struct_with_tuple(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1046,16 +1046,16 @@ fn test_set_entity_with_struct_enum_layout() { let layout = dojo::model::Model::::layout(); // test with the first variant - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); // then override with the second variant let values = create_struct_with_enum_second_variant(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1069,9 +1069,9 @@ fn test_set_entity_with_struct_simple_array_layout() { let values = create_struct_simple_array_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1085,9 +1085,9 @@ fn test_set_entity_with_struct_complex_array_layout() { let values = create_struct_complex_array_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1101,9 +1101,9 @@ fn test_set_entity_with_struct_layout_and_byte_array() { let values = create_struct_byte_array_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1117,9 +1117,9 @@ fn test_set_entity_with_nested_elements() { let values = create_struct_nested_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1145,16 +1145,16 @@ fn test_set_entity_with_struct_generics_enum_layout() { let layout = dojo::model::Model::::layout(); // test with the first variant - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); // then override with the second variant let values = create_struct_generic_second_variant(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert_array(read_values, values); } @@ -1167,11 +1167,11 @@ fn test_delete_entity_by_id() { let values = create_foo(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Id(entity_id), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Id(entity_id), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Id(entity_id), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Id(entity_id), layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Id(entity_id), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Id(entity_id), layout); assert!(read_values.len() == values.len()); assert_empty_array(read_values); @@ -1186,11 +1186,11 @@ fn test_delete_entity_with_fixed_layout() { let values = create_foo(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(get_key_test()), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(get_key_test()), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == values.len()); assert_empty_array(read_values); @@ -1206,11 +1206,11 @@ fn test_delete_entity_with_simple_struct_layout() { let values = create_struct_simple_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == values.len()); assert_empty_array(read_values); @@ -1226,11 +1226,11 @@ fn test_delete_entity_with_struct_simple_array_layout() { let values = create_struct_simple_array_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); // array length set to 0, so the expected value span is shorter than the initial values let expected_values = array![0, 0, 0].span(); @@ -1250,11 +1250,11 @@ fn test_delete_entity_with_complex_array_struct_layout() { let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); // array length set to 0, so the expected value span is shorter than the initial values let expected_values = array![0, 0, 0, 0, 0, 0, 0, 0, 0, 0].span(); @@ -1273,12 +1273,12 @@ fn test_delete_entity_with_struct_tuple_layout() { let values = create_struct_with_tuple(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); let expected_values = array![0, 0].span(); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == expected_values.len()); assert_empty_array(read_values); @@ -1295,12 +1295,12 @@ fn test_delete_entity_with_struct_enum_layout() { let layout = dojo::model::Model::::layout(); // test with the first variant - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); let expected_values = array![0, 0, 0].span(); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == expected_values.len()); assert_empty_array(read_values); @@ -1316,12 +1316,12 @@ fn test_delete_entity_with_struct_layout_and_byte_array() { let values = create_struct_byte_array_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); let expected_values = array![0, 0, 0, 0].span(); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == expected_values.len()); assert_empty_array(read_values); @@ -1337,12 +1337,12 @@ fn test_delete_entity_with_nested_elements() { let values = create_struct_nested_model(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); let expected_values = array![0, 0, 0, 0, 0, 0, 0, 0, 0].span(); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == expected_values.len()); assert_empty_array(read_values); @@ -1358,12 +1358,12 @@ fn test_delete_entity_with_struct_generics_enum_layout() { let values = create_struct_generic_first_variant(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); - world.delete_entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + world.delete_entity(selector, dojo::world::ModelIndex::Keys(keys), layout); let expected_values = array![0, 0].span(); - let read_values = world.entity(selector, dojo::model::ModelIndex::Keys(keys), layout); + let read_values = world.entity(selector, dojo::world::ModelIndex::Keys(keys), layout); assert!(read_values.len() == expected_values.len()); assert_empty_array(read_values); @@ -1382,7 +1382,7 @@ fn test_set_entity_with_unexpected_array_model_layout() { world .set_entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![].span()), + dojo::world::ModelIndex::Keys(array![].span()), array![].span(), layout ); @@ -1401,7 +1401,7 @@ fn test_set_entity_with_unexpected_tuple_model_layout() { world .set_entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![].span()), + dojo::world::ModelIndex::Keys(array![].span()), array![].span(), layout ); @@ -1420,7 +1420,7 @@ fn test_delete_entity_with_unexpected_array_model_layout() { world .delete_entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![].span()), + dojo::world::ModelIndex::Keys(array![].span()), layout ); } @@ -1438,7 +1438,7 @@ fn test_delete_entity_with_unexpected_tuple_model_layout() { world .delete_entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![].span()), + dojo::world::ModelIndex::Keys(array![].span()), layout ); } @@ -1456,7 +1456,7 @@ fn test_get_entity_with_unexpected_array_model_layout() { world .entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![].span()), + dojo::world::ModelIndex::Keys(array![].span()), layout ); } @@ -1474,7 +1474,7 @@ fn test_get_entity_with_unexpected_tuple_model_layout() { world .entity( dojo::model::Model::::selector(), - dojo::model::ModelIndex::Keys(array![].span()), + dojo::world::ModelIndex::Keys(array![].span()), layout ); } @@ -1490,7 +1490,7 @@ fn test_set_entity_with_bad_values_length_error_for_array_layout() { let keys = get_key_test(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), array![1].span(), layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), array![1].span(), layout); } #[test] @@ -1507,7 +1507,7 @@ fn test_set_entity_with_too_big_array_length() { .span(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); } #[test] @@ -1524,7 +1524,7 @@ fn test_set_entity_with_struct_layout_and_bad_byte_array_length() { .span(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); } #[test] @@ -1538,7 +1538,7 @@ fn test_set_entity_with_struct_layout_and_bad_value_length_for_byte_array() { let values: Span = array![1, 3, 'first', 'second', 'third', 'pending'].span(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(keys), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(keys), values, layout); } fn write_foo_record(world: dojo::world::IWorldDispatcher) { @@ -1546,7 +1546,7 @@ fn write_foo_record(world: dojo::world::IWorldDispatcher) { let values = create_foo(); let layout = dojo::model::Model::::layout(); - world.set_entity(selector, dojo::model::ModelIndex::Keys(get_key_test()), values, layout); + world.set_entity(selector, dojo::world::ModelIndex::Keys(get_key_test()), values, layout); } #[test] diff --git a/crates/dojo-lang/src/interface.rs b/crates/dojo-lang/src/interface.rs index c6a2294043..6c7afd39d3 100644 --- a/crates/dojo-lang/src/interface.rs +++ b/crates/dojo-lang/src/interface.rs @@ -50,7 +50,7 @@ impl DojoInterface { node: Box::new(RewriteNode::interpolate_patched( " #[starknet::interface] - trait $name$ { + pub trait $name$ { $body$ } ", @@ -67,7 +67,7 @@ impl DojoInterface { node: Box::new(RewriteNode::interpolate_patched( " #[starknet::interface] - trait $name$ {} + pub trait $name$ {} ", &UnorderedHashMap::from([( "name".to_string(), diff --git a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/Scarb.toml b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/Scarb.toml index da1668baab..ad25c11094 100644 --- a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/Scarb.toml +++ b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/Scarb.toml @@ -1,7 +1,7 @@ [package] name = "compiler_cairo" version = "0.1.0" -edition = "2023_10" +edition = "2023_11" cairo-version = "2.6.3" # See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html diff --git a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml index e79e888f02..b3ab13fadf 100644 --- a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml +++ b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" -original_class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" +class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" abi = "manifests/dev/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/crates/dojo-lang/src/model.rs b/crates/dojo-lang/src/model.rs index 82128144ca..52145f5d3d 100644 --- a/crates/dojo-lang/src/model.rs +++ b/crates/dojo-lang/src/model.rs @@ -276,7 +276,8 @@ pub fn handle_model_struct( param_keys.push(format!("{}: {}", member.name, member.ty)); } else { serialized_values.push(serialize_member_ty(&member, true)); - members_values.push(RewriteNode::Text(format!("{}: {},\n", member.name, member.ty))); + members_values + .push(RewriteNode::Text(format!("pub {}: {},\n", member.name, member.ty))); } members.push(member); @@ -325,7 +326,7 @@ pub struct $type_name$Entity { } #[generate_trait] -impl $type_name$EntityImpl of $type_name$EntityTrait { +pub impl $type_name$EntityImpl of $type_name$EntityTrait { fn get(world: dojo::world::IWorldDispatcher, entity_id: felt252) -> $type_name$Entity { $type_name$ModelEntityImpl::get(world, entity_id) } @@ -334,13 +335,31 @@ impl $type_name$EntityImpl of $type_name$EntityTrait { } #[generate_trait] -impl $type_name$Impl of $type_name$Trait { +pub impl $type_name$Impl of $type_name$Trait { fn entity_id_from_keys($param_keys$) -> felt252 { let mut serialized = core::array::ArrayTrait::new(); $serialized_param_keys$ core::poseidon::poseidon_hash_span(serialized.span()) } + fn from_values(ref keys: Span, ref values: Span) -> $type_name$ { + let mut serialized = core::array::ArrayTrait::new(); + serialized.append_span(keys); + serialized.append_span(values); + let mut serialized = core::array::ArrayTrait::span(@serialized); + + let entity = core::serde::Serde::<$type_name$>::deserialize(ref serialized); + + if core::option::OptionTrait::<$type_name$>::is_none(@entity) { + panic!( + \"Model `$type_name$`: deserialization failed. Ensure the length of the keys tuple \ + is matching the number of #[key] fields in the model struct.\" + ); + } + + core::option::OptionTrait::<$type_name$>::unwrap(entity) + } + fn get(world: dojo::world::IWorldDispatcher, $param_keys$) -> $type_name$ { let mut serialized = core::array::ArrayTrait::new(); $serialized_param_keys$ @@ -351,7 +370,7 @@ impl $type_name$Impl of $type_name$Trait { $field_accessors$ } -impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entity> { +pub impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entity> { fn id(self: @$type_name$Entity) -> felt252 { *self.__id } @@ -451,32 +470,17 @@ impl $type_name$ModelEntityImpl of dojo::model::ModelEntity<$type_name$Entity> { } } -impl $type_name$ModelImpl of dojo::model::Model<$type_name$> { +pub impl $type_name$ModelImpl of dojo::model::Model<$type_name$> { fn get(world: dojo::world::IWorldDispatcher, keys: Span) -> $type_name$ { - let values = dojo::world::IWorldDispatcherTrait::entity( + let mut values = dojo::world::IWorldDispatcherTrait::entity( world, Self::selector(), dojo::world::ModelIndex::Keys(keys), Self::layout() ); + let mut _keys = keys; - // TODO: Generate method to deserialize from keys / values directly to avoid - // serializing to intermediate array. - let mut serialized = core::array::ArrayTrait::new(); - core::array::serialize_array_helper(keys, ref serialized); - core::array::serialize_array_helper(values, ref serialized); - let mut serialized = core::array::ArrayTrait::span(@serialized); - - let entity = core::serde::Serde::<$type_name$>::deserialize(ref serialized); - - if core::option::OptionTrait::<$type_name$>::is_none(@entity) { - panic!( - \"Model `$type_name$`: deserialization failed. Ensure the length of the keys tuple \ - is matching the number of #[key] fields in the model struct.\" - ); - } - - core::option::OptionTrait::<$type_name$>::unwrap(entity) + $type_name$Trait::from_values(ref _keys, ref values) } fn set( @@ -631,12 +635,12 @@ impl $type_name$ModelImpl of dojo::model::Model<$type_name$> { } #[starknet::interface] -trait I$contract_name$ { +pub trait I$contract_name$ { fn ensure_abi(self: @T, model: $type_name$); } #[starknet::contract] -mod $contract_name$ { +pub mod $contract_name$ { use super::$type_name$; use super::I$contract_name$; @@ -795,17 +799,13 @@ fn generate_field_accessors( let mut serialized = core::array::ArrayTrait::new(); $serialized_param_keys$ - let values = dojo::model::Model::<$model_name$>::get_member( + let mut values = dojo::model::Model::<$model_name$>::get_member( world, serialized.span(), $field_selector$ ); - let mut serialized = core::array::ArrayTrait::new(); - core::array::serialize_array_helper(values, ref serialized); - let mut serialized = core::array::ArrayTrait::span(@serialized); - - let field_value = core::serde::Serde::<$field_type$>::deserialize(ref serialized); + let field_value = core::serde::Serde::<$field_type$>::deserialize(ref values); if core::option::OptionTrait::<$field_type$>::is_none(@field_value) { panic!( @@ -859,17 +859,12 @@ fn generate_entity_field_accessors(model_name: String, member: &Member) -> Rewri " fn get_$field_name$(world: dojo::world::IWorldDispatcher, entity_id: felt252) -> $field_type$ \ { - let values = dojo::model::ModelEntity::<$model_name$Entity>::get_member( + let mut values = dojo::model::ModelEntity::<$model_name$Entity>::get_member( world, entity_id, $field_selector$ ); - - let mut serialized = core::array::ArrayTrait::new(); - core::array::serialize_array_helper(values, ref serialized); - let mut serialized = core::array::ArrayTrait::span(@serialized); - - let field_value = core::serde::Serde::<$field_type$>::deserialize(ref serialized); + let field_value = core::serde::Serde::<$field_type$>::deserialize(ref values); if core::option::OptionTrait::<$field_type$>::is_none(@field_value) { panic!( diff --git a/crates/dojo-world/abigen/src/main.rs b/crates/dojo-world/abigen/src/main.rs index 3b052b91e2..f676a765b9 100644 --- a/crates/dojo-world/abigen/src/main.rs +++ b/crates/dojo-world/abigen/src/main.rs @@ -105,6 +105,7 @@ fn compile_dojo_core() { r#" [package] cairo-version = "=2.6.4" +edition = "2023_11" name = "dojo" version = "0.7.3" diff --git a/examples/game-lib/armory/src/lib.cairo b/examples/game-lib/armory/src/lib.cairo index a283a97df1..a62c9b300c 100644 --- a/examples/game-lib/armory/src/lib.cairo +++ b/examples/game-lib/armory/src/lib.cairo @@ -1,8 +1,8 @@ #[derive(Drop, Serde)] #[dojo::model(namespace: "armory")] -struct Flatbow { +pub struct Flatbow { #[key] - id: u32, - atk_speek: u32, - range: u32, + pub id: u32, + pub atk_speek: u32, + pub range: u32, } diff --git a/examples/game-lib/bestiary/src/lib.cairo b/examples/game-lib/bestiary/src/lib.cairo index 86478ee6a8..d193ab7dbc 100644 --- a/examples/game-lib/bestiary/src/lib.cairo +++ b/examples/game-lib/bestiary/src/lib.cairo @@ -1,9 +1,9 @@ #[derive(Drop, Serde)] #[dojo::model(namespace: "bestiary")] -struct RiverSkale { +pub struct RiverSkale { #[key] - id: u32, - health: u32, - armor: u32, - attack: u32, + pub id: u32, + pub health: u32, + pub armor: u32, + pub attack: u32, } diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index 39f63432ab..25a5853233 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -4,7 +4,7 @@ name = "dojo_examples" version = "0.7.3" # Use the prelude with the less imports as possible # from corelib. -edition = "2023_10" +edition = "2023_11" [cairo] sierra-replace-ids = true @@ -43,7 +43,7 @@ rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" private_key = "0x1800000000300000180000000000030000000000003006001800006600" -world_address = "0x5377ecb8b7b6ce3f17daf9064a5f5ee7f6642c3e72579b45478a828007db355" +world_address = "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6" [profile.release.tool.dojo] # for more info on how `merge-strategy` works see: diff --git a/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml b/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml index fb28175143..835a169ea6 100644 --- a/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml +++ b/examples/spawn-and-move/manifests/dev/base/contracts/dojo_examples-actions-40b6994c.toml @@ -1,6 +1,6 @@ kind = "DojoContract" -class_hash = "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33" -original_class_hash = "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33" +class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" +original_class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" base_class_hash = "0x0" abi = "manifests/dev/base/abis/contracts/dojo_examples-actions-40b6994c.json" reads = [] diff --git a/examples/spawn-and-move/manifests/dev/base/dojo-world.toml b/examples/spawn-and-move/manifests/dev/base/dojo-world.toml index e79e888f02..b3ab13fadf 100644 --- a/examples/spawn-and-move/manifests/dev/base/dojo-world.toml +++ b/examples/spawn-and-move/manifests/dev/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" -original_class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" +class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" abi = "manifests/dev/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/examples/spawn-and-move/manifests/dev/deployment/manifest.json b/examples/spawn-and-move/manifests/dev/deployment/manifest.json index ce9072be7e..9c2b5edc83 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/manifest.json +++ b/examples/spawn-and-move/manifests/dev/deployment/manifest.json @@ -1,8 +1,8 @@ { "world": { "kind": "WorldContract", - "class_hash": "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557", - "original_class_hash": "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557", + "class_hash": "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb", + "original_class_hash": "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb", "abi": [ { "type": "impl", @@ -1189,8 +1189,8 @@ ] } ], - "address": "0x5377ecb8b7b6ce3f17daf9064a5f5ee7f6642c3e72579b45478a828007db355", - "transaction_hash": "0x59c745b80871acdab7ad1c29703cac9aa949f4326b32037943512adc0a19ab2", + "address": "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6", + "transaction_hash": "0x1e4e0d098436df78bfd2f61cfd24273bd2f9de70b1ad372aa5d122f2ad7cc75", "block_number": 3, "seed": "dojo_examples", "metadata": { @@ -1210,9 +1210,9 @@ "contracts": [ { "kind": "DojoContract", - "address": "0x5c92c8995272a6ae073392c6878fe80cd71ae0be4931bce75f3dbfe24c208a8", - "class_hash": "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33", - "original_class_hash": "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33", + "address": "0x5718df0ef4ac58def63459069d160887ad637f979cf425d0ddb6fd77f3cd121", + "class_hash": "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5", + "original_class_hash": "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", "abi": [ { @@ -1641,7 +1641,7 @@ }, { "kind": "DojoContract", - "address": "0x44e1b43ee34f816374612ae2df4a668e76a1aa723de8d7b67166e10a7225112", + "address": "0x3e629315ea75d37711b4146af01efa2f0218e8aefda2ceb42bb0fd310cb2840", "class_hash": "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674", "original_class_hash": "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", @@ -1878,7 +1878,7 @@ }, { "kind": "DojoContract", - "address": "0x6840cf5bebab9c134ddc4d34f6b021684610d7b83d663fc46e2e10d3569ea15", + "address": "0x22edd1c0f399a12c2f9789b1a36bb02e22c3300aabfeac4bfe6b7dc2a95b70d", "class_hash": "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028", "original_class_hash": "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", @@ -2097,7 +2097,7 @@ }, { "kind": "DojoContract", - "address": "0x512bcb4058b044f689269d8a1c23e653c04baee2029f7e5d3f4ec053e6c5c91", + "address": "0x7d38ad1bf17abfd26749f4780b95c525cc562c8ffba994bbe8e09de36b25404", "class_hash": "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7", "original_class_hash": "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", diff --git a/examples/spawn-and-move/manifests/dev/deployment/manifest.toml b/examples/spawn-and-move/manifests/dev/deployment/manifest.toml index f6bacb4b73..44183e4072 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/manifest.toml +++ b/examples/spawn-and-move/manifests/dev/deployment/manifest.toml @@ -1,10 +1,10 @@ [world] kind = "WorldContract" -class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" -original_class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" +class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" abi = "manifests/dev/deployment/abis/dojo-world.json" -address = "0x5377ecb8b7b6ce3f17daf9064a5f5ee7f6642c3e72579b45478a828007db355" -transaction_hash = "0x59c745b80871acdab7ad1c29703cac9aa949f4326b32037943512adc0a19ab2" +address = "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6" +transaction_hash = "0x1e4e0d098436df78bfd2f61cfd24273bd2f9de70b1ad372aa5d122f2ad7cc75" block_number = 3 seed = "dojo_examples" manifest_name = "dojo-world" @@ -23,9 +23,9 @@ manifest_name = "dojo-base" [[contracts]] kind = "DojoContract" -address = "0x5c92c8995272a6ae073392c6878fe80cd71ae0be4931bce75f3dbfe24c208a8" -class_hash = "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33" -original_class_hash = "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33" +address = "0x5718df0ef4ac58def63459069d160887ad637f979cf425d0ddb6fd77f3cd121" +class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" +original_class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" abi = "manifests/dev/deployment/abis/contracts/dojo_examples-actions-40b6994c.json" reads = [] @@ -40,7 +40,7 @@ manifest_name = "dojo_examples-actions-40b6994c" [[contracts]] kind = "DojoContract" -address = "0x44e1b43ee34f816374612ae2df4a668e76a1aa723de8d7b67166e10a7225112" +address = "0x3e629315ea75d37711b4146af01efa2f0218e8aefda2ceb42bb0fd310cb2840" class_hash = "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674" original_class_hash = "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" @@ -54,7 +54,7 @@ manifest_name = "dojo_examples-dungeon-6620e0e6" [[contracts]] kind = "DojoContract" -address = "0x6840cf5bebab9c134ddc4d34f6b021684610d7b83d663fc46e2e10d3569ea15" +address = "0x22edd1c0f399a12c2f9789b1a36bb02e22c3300aabfeac4bfe6b7dc2a95b70d" class_hash = "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028" original_class_hash = "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" @@ -68,7 +68,7 @@ manifest_name = "dojo_examples-mock_token-31599eb2" [[contracts]] kind = "DojoContract" -address = "0x512bcb4058b044f689269d8a1c23e653c04baee2029f7e5d3f4ec053e6c5c91" +address = "0x7d38ad1bf17abfd26749f4780b95c525cc562c8ffba994bbe8e09de36b25404" class_hash = "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7" original_class_hash = "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" diff --git a/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml b/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml index 445640874c..08f70f4804 100644 --- a/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml +++ b/examples/spawn-and-move/manifests/release/base/contracts/dojo_examples-actions-40b6994c.toml @@ -1,6 +1,6 @@ kind = "DojoContract" -class_hash = "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33" -original_class_hash = "0x676eed1c5c6c5f1ddf488914d137e07761d1a320df8a50fbf75ecf209624a33" +class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" +original_class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" base_class_hash = "0x0" abi = "manifests/release/base/abis/contracts/dojo_examples-actions-40b6994c.json" reads = [] diff --git a/examples/spawn-and-move/manifests/release/base/dojo-world.toml b/examples/spawn-and-move/manifests/release/base/dojo-world.toml index 5891e40ff4..63d67ac31f 100644 --- a/examples/spawn-and-move/manifests/release/base/dojo-world.toml +++ b/examples/spawn-and-move/manifests/release/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" -original_class_hash = "0x26a8f65e0b0ba2a6d2f9e93d4f72f75eabebb853ea7413e134b3902ea352557" +class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" abi = "manifests/release/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/examples/spawn-and-move/src/actions.cairo b/examples/spawn-and-move/src/actions.cairo index 18350b28a0..d073bc36f8 100644 --- a/examples/spawn-and-move/src/actions.cairo +++ b/examples/spawn-and-move/src/actions.cairo @@ -1,7 +1,7 @@ use dojo_examples::models::{Direction, Position, Vec2}; #[dojo::interface] -trait IActions { +pub trait IActions { fn spawn(ref world: IWorldDispatcher); fn move(ref world: IWorldDispatcher, direction: Direction); fn set_player_config(ref world: IWorldDispatcher, name: ByteArray); @@ -15,13 +15,13 @@ trait IActions { } #[dojo::interface] -trait IActionsComputed { +pub trait IActionsComputed { fn tile_terrain(vec: Vec2) -> felt252; fn quadrant(pos: Position) -> u8; } #[dojo::contract] -mod actions { +pub mod actions { use super::IActions; use super::IActionsComputed; @@ -32,7 +32,7 @@ mod actions { }; use dojo_examples::utils::next_position; - // Featues can be used on modules, structs, trait and `use`. Not inside + // Features can be used on modules, structs, trait and `use`. Not inside // a function. #[cfg(feature: 'dungeon')] use dojo_examples::dungeon::{IDungeonDispatcher, IDungeonDispatcherTrait}; @@ -44,10 +44,10 @@ mod actions { #[derive(Copy, Drop, Serde)] #[dojo::event] #[dojo::model] - struct Moved { + pub struct Moved { #[key] - player: ContractAddress, - direction: Direction, + pub player: ContractAddress, + pub direction: Direction, } #[abi(embed_v0)] @@ -164,8 +164,7 @@ mod actions { let river_skale = RiverSkale { id: 1, health: 5, armor: 3, attack: 2 }; set!(world, (flatbow, river_skale)); - - IDungeonDispatcher { contract_address: dungeon_address }.enter(); + // IDungeonDispatcher { contract_address: dungeon_address }.enter(); } fn update_player_name(ref world: IWorldDispatcher, name: ByteArray) { @@ -212,8 +211,6 @@ mod actions { #[cfg(test)] mod tests { - use starknet::class_hash::Felt252TryIntoClassHash; - use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait}; use dojo::test_utils::{spawn_test_world, deploy_contract}; @@ -241,6 +238,7 @@ mod tests { let initial_moves = get!(world, caller, Moves); let initial_position = get!(world, caller, Position); + println!("initial_position: {:?}", initial_position); assert( initial_position.vec.x == 10 && initial_position.vec.y == 10, 'wrong initial position' ); diff --git a/examples/spawn-and-move/src/dungeon.cairo b/examples/spawn-and-move/src/dungeon.cairo index ce7e40af9c..474f88b818 100644 --- a/examples/spawn-and-move/src/dungeon.cairo +++ b/examples/spawn-and-move/src/dungeon.cairo @@ -1,12 +1,12 @@ #[dojo::interface] -trait IDungeon { +pub trait IDungeon { fn enter(); } #[dojo::contract] -mod dungeon { +pub mod dungeon { #[abi(embed_v0)] - impl IDungeonImpl of super::IDungeon { + pub impl IDungeonImpl of super::IDungeon { fn enter() {} } } diff --git a/examples/spawn-and-move/src/lib.cairo b/examples/spawn-and-move/src/lib.cairo index 508962290e..c3f2f56028 100644 --- a/examples/spawn-and-move/src/lib.cairo +++ b/examples/spawn-and-move/src/lib.cairo @@ -1,7 +1,7 @@ -mod actions; -mod models; -mod utils; -mod others; -mod mock_token; +pub mod actions; +pub mod models; +pub mod utils; +pub mod others; +pub mod mock_token; #[cfg(feature: 'dungeon')] -mod dungeon; +pub mod dungeon; diff --git a/examples/spawn-and-move/src/mock_token.cairo b/examples/spawn-and-move/src/mock_token.cairo index bd34916233..64481bd76b 100644 --- a/examples/spawn-and-move/src/mock_token.cairo +++ b/examples/spawn-and-move/src/mock_token.cairo @@ -1,5 +1,5 @@ #[dojo::contract] -mod mock_token { +pub mod mock_token { use dojo_examples::models::{MockToken}; use starknet::{ContractAddress, get_caller_address}; diff --git a/examples/spawn-and-move/src/models.cairo b/examples/spawn-and-move/src/models.cairo index 4882eb9ddf..ddd61efb92 100644 --- a/examples/spawn-and-move/src/models.cairo +++ b/examples/spawn-and-move/src/models.cairo @@ -1,7 +1,7 @@ use starknet::ContractAddress; -#[derive(Serde, Copy, Drop, Introspect, PartialEq)] -enum Direction { +#[derive(Serde, Copy, Drop, Introspect, PartialEq, Debug)] +pub enum Direction { None, Left, Right, @@ -23,78 +23,78 @@ impl DirectionIntoFelt252 of Into { #[derive(Drop, Serde)] #[dojo::model] -struct Message { +pub struct Message { #[key] - identity: ContractAddress, + pub identity: ContractAddress, #[key] - channel: felt252, - message: ByteArray, + pub channel: felt252, + pub message: ByteArray, #[key] - salt: felt252 + pub salt: felt252 } -#[derive(Copy, Drop, Serde)] +#[derive(Copy, Drop, Serde, Debug)] #[dojo::model] -struct Moves { +pub struct Moves { #[key] - player: ContractAddress, - remaining: u8, - last_direction: Direction + pub player: ContractAddress, + pub remaining: u8, + pub last_direction: Direction } -#[derive(Copy, Drop, Serde)] +#[derive(Copy, Drop, Serde, Debug)] #[dojo::model] -struct MockToken { +pub struct MockToken { #[key] - account: ContractAddress, - amount: u128, + pub account: ContractAddress, + pub amount: u128, } -#[derive(Copy, Drop, Serde, IntrospectPacked)] -struct Vec2 { - x: u32, - y: u32 +#[derive(Copy, Drop, Serde, IntrospectPacked, Debug)] +pub struct Vec2 { + pub x: u32, + pub y: u32 } // If `Vec2` wasn't packed, the `Position` would be invalid, // and a runtime error would be thrown. // Any field that is a custom type into a `IntrospectPacked` type // must be packed. -#[derive(Copy, Drop, Serde, IntrospectPacked)] +#[derive(Copy, Drop, Serde, IntrospectPacked, Debug)] #[dojo::model] -struct Position { +pub struct Position { #[key] - player: ContractAddress, - vec: Vec2, + pub player: ContractAddress, + pub vec: Vec2, } // Every field inside a model must derive `Introspect` or `IntrospectPacked`. // `IntrospectPacked` can also be used into models that are only using `Introspect`. #[derive(Copy, Drop, Serde, Introspect)] -struct PlayerItem { - item_id: u32, - quantity: u32, - score: i32, +pub struct PlayerItem { + pub item_id: u32, + pub quantity: u32, + pub score: i32, } #[derive(Drop, Serde)] #[dojo::model] -struct PlayerConfig { +pub struct PlayerConfig { #[key] - player: ContractAddress, - name: ByteArray, - items: Array, - favorite_item: Option, + pub player: ContractAddress, + pub name: ByteArray, + pub items: Array, + pub favorite_item: Option, } #[derive(Drop, Serde)] #[dojo::model] -struct ServerProfile { +pub struct ServerProfile { #[key] - player: ContractAddress, + pub player: ContractAddress, #[key] - server_id: u32, - name: ByteArray, + pub server_id: u32, + pub name: ByteArray, } trait Vec2Trait { diff --git a/examples/spawn-and-move/src/others.cairo b/examples/spawn-and-move/src/others.cairo index 8b64df6a23..8b9c1a9c67 100644 --- a/examples/spawn-and-move/src/others.cairo +++ b/examples/spawn-and-move/src/others.cairo @@ -1,5 +1,5 @@ #[dojo::contract] -mod others { +pub mod others { use starknet::{ContractAddress, ClassHash, get_caller_address}; use dojo_examples::models::{Position, Moves, Direction, Vec2}; use dojo_examples::utils::next_position; diff --git a/examples/spawn-and-move/src/utils.cairo b/examples/spawn-and-move/src/utils.cairo index e14be466d8..60d2bb8562 100644 --- a/examples/spawn-and-move/src/utils.cairo +++ b/examples/spawn-and-move/src/utils.cairo @@ -1,6 +1,6 @@ use dojo_examples::models::{Position, Direction}; -fn next_position(mut position: Position, direction: Direction) -> Position { +pub fn next_position(mut position: Position, direction: Direction) -> Position { match direction { Direction::None => { return position; }, Direction::Left => { position.vec.x -= 1; }, From 3424011d7d5a4ddfae7dbf07f51376a39a02f162 Mon Sep 17 00:00:00 2001 From: "remy.baranx@gmail.com" Date: Wed, 17 Jul 2024 14:22:54 +0200 Subject: [PATCH 2/2] add edition to Scarb.toml of Torii tests --- crates/dojo-core/src/world.cairo | 5 +- .../manifests/dev/base/dojo-world.toml | 4 +- crates/torii/types-test/Scarb.toml | 1 + crates/torii/types-test/src/contracts.cairo | 7 +- crates/torii/types-test/src/models.cairo | 97 ++++++++++--------- examples/spawn-and-move/Scarb.toml | 2 +- .../manifests/dev/base/dojo-world.toml | 4 +- .../manifests/dev/deployment/manifest.json | 16 +-- .../manifests/dev/deployment/manifest.toml | 16 +-- .../manifests/release/base/dojo-world.toml | 4 +- 10 files changed, 80 insertions(+), 76 deletions(-) diff --git a/crates/dojo-core/src/world.cairo b/crates/dojo-core/src/world.cairo index 0a6fa9c775..7929bd59b4 100644 --- a/crates/dojo-core/src/world.cairo +++ b/crates/dojo-core/src/world.cairo @@ -318,11 +318,12 @@ pub mod world { /// `resource_id` - The resource id. fn metadata(self: @ContractState, resource_id: felt252) -> ResourceMetadata { let mut values = self - .entity( + ._read_model_entity( dojo::model::Model::::selector(), - ModelIndex::Keys(array![resource_id].span()), + entity_id_from_keys(array![resource_id].span()), dojo::model::Model::::layout() ); + ResourceMetadataTrait::from_values(resource_id, ref values) } diff --git a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml index b3ab13fadf..f403ca018f 100644 --- a/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml +++ b/crates/dojo-lang/src/manifest_test_data/compiler_cairo/manifests/dev/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" -original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" +original_class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" abi = "manifests/dev/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/crates/torii/types-test/Scarb.toml b/crates/torii/types-test/Scarb.toml index 1428cbe432..cb9e136633 100644 --- a/crates/torii/types-test/Scarb.toml +++ b/crates/torii/types-test/Scarb.toml @@ -1,5 +1,6 @@ [package] cairo-version = "=2.6.4" +edition = "2023_11" name = "types_test" version = "0.7.3" diff --git a/crates/torii/types-test/src/contracts.cairo b/crates/torii/types-test/src/contracts.cairo index 49fbd967ea..efb6271343 100644 --- a/crates/torii/types-test/src/contracts.cairo +++ b/crates/torii/types-test/src/contracts.cairo @@ -10,7 +10,8 @@ trait IRecords { mod records { use starknet::{ContractAddress, get_caller_address}; use types_test::models::{ - Record, RecordTrait, RecordSibling, RecordSiblingTrait, Subrecord, SubrecordTrait, Nested, NestedMore, NestedMost, Depth + Record, RecordTrait, RecordSibling, RecordSiblingTrait, Subrecord, SubrecordTrait, Nested, + NestedMore, NestedMost, Depth }; use types_test::{seed, random}; use super::IRecords; @@ -42,11 +43,11 @@ mod records { } let type_felt: felt252 = record_idx.into(); - let random_u8 = random(pedersen::pedersen(seed(), record_idx.into()), 0, 100) + let random_u8 = random(core::pedersen::pedersen(seed(), record_idx.into()), 0, 100) .try_into() .unwrap(); let random_u128 = random( - pedersen::pedersen(seed(), record_idx.into()), + core::pedersen::pedersen(seed(), record_idx.into()), 0, 0xffffffffffffffffffffffffffffffff_u128 ); diff --git a/crates/torii/types-test/src/models.cairo b/crates/torii/types-test/src/models.cairo index 489cefd6b1..0fe9a7e105 100644 --- a/crates/torii/types-test/src/models.cairo +++ b/crates/torii/types-test/src/models.cairo @@ -1,79 +1,79 @@ -use array::ArrayTrait; +use core::array::ArrayTrait; use starknet::{ContractAddress, ClassHash}; #[derive(Introspect, Drop, Serde)] #[dojo::model] -struct Record { +pub struct Record { #[key] - record_id: u32, - depth: Depth, - type_i8: i8, - type_i16: i16, - type_i32: i32, - type_i64: i64, - type_i128: i128, - type_u8: u8, - type_u16: u16, - type_u32: u32, - type_u64: u64, - type_u128: u128, - type_u256: u256, - type_bool: bool, - type_felt: felt252, - type_class_hash: ClassHash, - type_contract_address: ContractAddress, - type_deeply_nested: Nested, - type_nested_one: NestedMost, - type_nested_two: NestedMost, - random_u8: u8, - random_u128: u128, - composite_u256: u256, + pub record_id: u32, + pub depth: Depth, + pub type_i8: i8, + pub type_i16: i16, + pub type_i32: i32, + pub type_i64: i64, + pub type_i128: i128, + pub type_u8: u8, + pub type_u16: u16, + pub type_u32: u32, + pub type_u64: u64, + pub type_u128: u128, + pub type_u256: u256, + pub type_bool: bool, + pub type_felt: felt252, + pub type_class_hash: ClassHash, + pub type_contract_address: ContractAddress, + pub type_deeply_nested: Nested, + pub type_nested_one: NestedMost, + pub type_nested_two: NestedMost, + pub random_u8: u8, + pub random_u128: u128, + pub composite_u256: u256, } #[derive(Introspect, Copy, Drop, Serde)] #[dojo::model] -struct RecordSibling { +pub struct RecordSibling { #[key] - record_id: u32, - random_u8: u8 + pub record_id: u32, + pub random_u8: u8 } #[derive(Copy, Drop, Serde, Introspect)] -struct Nested { - depth: Depth, - type_number: u8, - type_string: felt252, - type_nested_more: NestedMore, +pub struct Nested { + pub depth: Depth, + pub type_number: u8, + pub type_string: felt252, + pub type_nested_more: NestedMore, } #[derive(Copy, Drop, Serde, Introspect)] -struct NestedMore { - depth: Depth, - type_number: u8, - type_string: felt252, - type_nested_most: NestedMost, +pub struct NestedMore { + pub depth: Depth, + pub type_number: u8, + pub type_string: felt252, + pub type_nested_most: NestedMost, } #[derive(Copy, Drop, Serde, Introspect)] -struct NestedMost { - depth: Depth, - type_number: u8, - type_string: felt252, +pub struct NestedMost { + pub depth: Depth, + pub type_number: u8, + pub type_string: felt252, } #[derive(Introspect, Copy, Drop, Serde)] #[dojo::model] -struct Subrecord { +pub struct Subrecord { #[key] - record_id: u32, + pub record_id: u32, #[key] - subrecord_id: u32, - type_u8: u8, - random_u8: u8, + pub subrecord_id: u32, + pub type_u8: u8, + pub random_u8: u8, } #[derive(Serde, Copy, Drop, Introspect)] -enum Depth { +pub enum Depth { Zero: (), One: (), Two: (), @@ -354,3 +354,4 @@ impl DepthIntoFelt252 of Into { // a255: u256, // } + diff --git a/examples/spawn-and-move/Scarb.toml b/examples/spawn-and-move/Scarb.toml index 25a5853233..fa83fe8e62 100644 --- a/examples/spawn-and-move/Scarb.toml +++ b/examples/spawn-and-move/Scarb.toml @@ -43,7 +43,7 @@ rpc_url = "http://localhost:5050/" # Default account for katana with seed = 0 account_address = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03" private_key = "0x1800000000300000180000000000030000000000003006001800006600" -world_address = "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6" +world_address = "0x62c5e1e2ceab297dfaca3ef17ca6549635272f363bd5373c888bbacdc3d8548" [profile.release.tool.dojo] # for more info on how `merge-strategy` works see: diff --git a/examples/spawn-and-move/manifests/dev/base/dojo-world.toml b/examples/spawn-and-move/manifests/dev/base/dojo-world.toml index b3ab13fadf..f403ca018f 100644 --- a/examples/spawn-and-move/manifests/dev/base/dojo-world.toml +++ b/examples/spawn-and-move/manifests/dev/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" -original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" +original_class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" abi = "manifests/dev/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world" diff --git a/examples/spawn-and-move/manifests/dev/deployment/manifest.json b/examples/spawn-and-move/manifests/dev/deployment/manifest.json index 9c2b5edc83..6f2486e866 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/manifest.json +++ b/examples/spawn-and-move/manifests/dev/deployment/manifest.json @@ -1,8 +1,8 @@ { "world": { "kind": "WorldContract", - "class_hash": "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb", - "original_class_hash": "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb", + "class_hash": "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd", + "original_class_hash": "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd", "abi": [ { "type": "impl", @@ -1189,8 +1189,8 @@ ] } ], - "address": "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6", - "transaction_hash": "0x1e4e0d098436df78bfd2f61cfd24273bd2f9de70b1ad372aa5d122f2ad7cc75", + "address": "0x62c5e1e2ceab297dfaca3ef17ca6549635272f363bd5373c888bbacdc3d8548", + "transaction_hash": "0x30d768fa29fd2ba80734946a339118c1adfea82629458b054a4d4a35103db47", "block_number": 3, "seed": "dojo_examples", "metadata": { @@ -1210,7 +1210,7 @@ "contracts": [ { "kind": "DojoContract", - "address": "0x5718df0ef4ac58def63459069d160887ad637f979cf425d0ddb6fd77f3cd121", + "address": "0x604bd94012befb403ad70472bcbae1504211b17c804373ea835aec77eb1ee1", "class_hash": "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5", "original_class_hash": "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", @@ -1641,7 +1641,7 @@ }, { "kind": "DojoContract", - "address": "0x3e629315ea75d37711b4146af01efa2f0218e8aefda2ceb42bb0fd310cb2840", + "address": "0x7e7462000391d3084e3eabd1c28f027c06ad683c787cd8173ca33d906e9a082", "class_hash": "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674", "original_class_hash": "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", @@ -1878,7 +1878,7 @@ }, { "kind": "DojoContract", - "address": "0x22edd1c0f399a12c2f9789b1a36bb02e22c3300aabfeac4bfe6b7dc2a95b70d", + "address": "0xcc6decf700ca5870865e95307c74cb531728f6d50670449fa33518e4a9c3c8", "class_hash": "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028", "original_class_hash": "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", @@ -2097,7 +2097,7 @@ }, { "kind": "DojoContract", - "address": "0x7d38ad1bf17abfd26749f4780b95c525cc562c8ffba994bbe8e09de36b25404", + "address": "0x6cf6ece03c80fcddd4107270a52eab493534b8ccf8346ff17d451362a4e99d0", "class_hash": "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7", "original_class_hash": "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7", "base_class_hash": "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4", diff --git a/examples/spawn-and-move/manifests/dev/deployment/manifest.toml b/examples/spawn-and-move/manifests/dev/deployment/manifest.toml index 44183e4072..a3f19d5d63 100644 --- a/examples/spawn-and-move/manifests/dev/deployment/manifest.toml +++ b/examples/spawn-and-move/manifests/dev/deployment/manifest.toml @@ -1,10 +1,10 @@ [world] kind = "WorldContract" -class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" -original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" +original_class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" abi = "manifests/dev/deployment/abis/dojo-world.json" -address = "0x2bab2a15d4c71bd8e45c84ac3b1518aeca39d3818e8e556c4a56202c1679ba6" -transaction_hash = "0x1e4e0d098436df78bfd2f61cfd24273bd2f9de70b1ad372aa5d122f2ad7cc75" +address = "0x62c5e1e2ceab297dfaca3ef17ca6549635272f363bd5373c888bbacdc3d8548" +transaction_hash = "0x30d768fa29fd2ba80734946a339118c1adfea82629458b054a4d4a35103db47" block_number = 3 seed = "dojo_examples" manifest_name = "dojo-world" @@ -23,7 +23,7 @@ manifest_name = "dojo-base" [[contracts]] kind = "DojoContract" -address = "0x5718df0ef4ac58def63459069d160887ad637f979cf425d0ddb6fd77f3cd121" +address = "0x604bd94012befb403ad70472bcbae1504211b17c804373ea835aec77eb1ee1" class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" original_class_hash = "0xb3f81618df8f1170864da1c68f565aafe192c30f9b73436af4823b293a7ea5" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" @@ -40,7 +40,7 @@ manifest_name = "dojo_examples-actions-40b6994c" [[contracts]] kind = "DojoContract" -address = "0x3e629315ea75d37711b4146af01efa2f0218e8aefda2ceb42bb0fd310cb2840" +address = "0x7e7462000391d3084e3eabd1c28f027c06ad683c787cd8173ca33d906e9a082" class_hash = "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674" original_class_hash = "0x14b3096b82a761f63dd47277c2b5ac18925dea43586418483939a2f1f57f674" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" @@ -54,7 +54,7 @@ manifest_name = "dojo_examples-dungeon-6620e0e6" [[contracts]] kind = "DojoContract" -address = "0x22edd1c0f399a12c2f9789b1a36bb02e22c3300aabfeac4bfe6b7dc2a95b70d" +address = "0xcc6decf700ca5870865e95307c74cb531728f6d50670449fa33518e4a9c3c8" class_hash = "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028" original_class_hash = "0x761d18a3557d98b3962ebb2c9ddae89ad586ce81de7e86c5fd1e1b4f9d0028" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" @@ -68,7 +68,7 @@ manifest_name = "dojo_examples-mock_token-31599eb2" [[contracts]] kind = "DojoContract" -address = "0x7d38ad1bf17abfd26749f4780b95c525cc562c8ffba994bbe8e09de36b25404" +address = "0x6cf6ece03c80fcddd4107270a52eab493534b8ccf8346ff17d451362a4e99d0" class_hash = "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7" original_class_hash = "0x479bfb12dcba5398d77303e7a665fc3fedb16f2d7f9cb1f5d7e2beb3b7e2ba7" base_class_hash = "0x26a4f5d2d9638877a2648297339275df5eaab0adb3cdf0010887c2dbf2be4" diff --git a/examples/spawn-and-move/manifests/release/base/dojo-world.toml b/examples/spawn-and-move/manifests/release/base/dojo-world.toml index 63d67ac31f..da20975053 100644 --- a/examples/spawn-and-move/manifests/release/base/dojo-world.toml +++ b/examples/spawn-and-move/manifests/release/base/dojo-world.toml @@ -1,6 +1,6 @@ kind = "Class" -class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" -original_class_hash = "0x6990b621c2db6fbffa4cbbe5c0d91103721c7246adc83a4b27715e2dba79eb" +class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" +original_class_hash = "0x71819175fb1971b65368cdbc486b7044ea1a15fe499296181c574e90d91adcd" abi = "manifests/release/base/abis/dojo-world.json" tag = "dojo-world" manifest_name = "dojo-world"