From 342a2c3f3fd73d927922335451c924e51f17d40e Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Wed, 14 Jun 2023 12:08:49 -0400 Subject: [PATCH] Remove u250 type --- crates/dojo-core/src/auth/components.cairo | 4 +- crates/dojo-core/src/auth/systems.cairo | 42 ++-- crates/dojo-core/src/executor.cairo | 1 - crates/dojo-core/src/integer.cairo | 179 ------------------ crates/dojo-core/src/interfaces.cairo | 8 +- crates/dojo-core/src/lib.cairo | 1 - crates/dojo-core/src/storage/db.cairo | 17 +- crates/dojo-core/src/storage/index.cairo | 16 +- crates/dojo-core/src/storage/kv.cairo | 8 +- crates/dojo-core/src/storage/query.cairo | 44 ++--- crates/dojo-core/src/storage/utils.cairo | 14 +- crates/dojo-core/src/world.cairo | 13 +- crates/dojo-core/tests/src/integer.cairo | 62 ------ crates/dojo-core/tests/src/lib.cairo | 1 - .../dojo-core/tests/src/storage/index.cairo | 1 - .../dojo-core/tests/src/storage/query.cairo | 3 - .../dojo-core/tests/src/storage/utils.cairo | 63 +++--- crates/dojo-core/tests/src/world.cairo | 4 - .../src/constant_product_market/systems.cairo | 20 +- crates/dojo-erc/src/erc20/erc20.cairo | 2 +- crates/dojo-lang/src/commands/entities.rs | 5 +- crates/dojo-lang/src/plugin_test_data/system | 24 ++- crates/torii/src/graphql/object/storage.rs | 1 - crates/torii/src/graphql/types.rs | 2 - 24 files changed, 130 insertions(+), 405 deletions(-) delete mode 100644 crates/dojo-core/src/integer.cairo delete mode 100644 crates/dojo-core/tests/src/integer.cairo diff --git a/crates/dojo-core/src/auth/components.cairo b/crates/dojo-core/src/auth/components.cairo index f20e064070..f31aaac1f1 100644 --- a/crates/dojo-core/src/auth/components.cairo +++ b/crates/dojo-core/src/auth/components.cairo @@ -1,5 +1,3 @@ -use dojo_core::integer::u250; - #[derive(Component, Copy, Drop, Serde)] struct AuthStatus { is_authorized: bool @@ -7,5 +5,5 @@ struct AuthStatus { #[derive(Component, Copy, Drop, Serde)] struct AuthRole { - id: u250 + id: felt252 } diff --git a/crates/dojo-core/src/auth/systems.cairo b/crates/dojo-core/src/auth/systems.cairo index 93d2098754..26c0ea9bfc 100644 --- a/crates/dojo-core/src/auth/systems.cairo +++ b/crates/dojo-core/src/auth/systems.cairo @@ -1,18 +1,16 @@ -use dojo_core::integer::u250; - #[derive(Drop, Serde)] struct Route { - target_id: u250, - role_id: u250, - resource_id: u250, + target_id: felt252, + role_id: felt252, + resource_id: felt252, } trait RouteTrait { - fn new(target_id: u250, role_id: u250, resource_id: u250) -> Route; + fn new(target_id: felt252, role_id: felt252, resource_id: felt252) -> Route; } impl RouteImpl of RouteTrait { - fn new(target_id: u250, role_id: u250, resource_id: u250) -> Route { + fn new(target_id: felt252, role_id: felt252, resource_id: felt252) -> Route { Route { target_id, role_id, resource_id, } } } @@ -43,7 +41,7 @@ mod RouteAuth { mod IsAccountAdmin { use traits::Into; use box::BoxTrait; - use dojo_core::{auth::components::{AuthStatus, AuthRole}, integer::u250}; + use dojo_core::auth::components::{AuthStatus, AuthRole}; use dojo_core::world::World; fn execute(ctx: Context) -> bool { @@ -58,10 +56,10 @@ mod IsAccountAdmin { #[system] mod IsAuthorized { use traits::Into; - use dojo_core::{auth::components::{AuthStatus, AuthRole}, integer::u250}; + use dojo_core::auth::components::{AuthStatus, AuthRole}; use dojo_core::world::World; - fn execute(ctx: Context, target_id: u250, resource_id: u250) -> bool { + fn execute(ctx: Context, target_id: felt252, resource_id: felt252) -> bool { // Check if execution role is not set let scoped_role = if ctx.execution_role.id == 0.into() { // Use default component-scoped role @@ -110,9 +108,9 @@ mod IsAuthorized { mod GrantAuthRole { use traits::Into; use array::ArrayTrait; - use dojo_core::{auth::components::AuthRole, integer::u250}; + use dojo_core::auth::components::AuthRole; - fn execute(target_id: u250, role_id: u250) { + fn execute(target_id: felt252, role_id: felt252) { commands::set_entity(target_id.into(), (AuthRole { id: role_id })); } } @@ -121,10 +119,10 @@ mod GrantAuthRole { mod GrantScopedAuthRole { use traits::Into; use array::ArrayTrait; - use dojo_core::{auth::components::AuthRole, integer::u250}; + use dojo_core::auth::components::AuthRole; - fn execute(target_id: u250, role_id: u250, resource_id: u250) { + fn execute(target_id: felt252, role_id: felt252, resource_id: felt252) { commands::set_entity((target_id, resource_id).into(), (AuthRole { id: role_id })); } } @@ -132,9 +130,9 @@ mod GrantScopedAuthRole { #[system] mod GrantResource { use traits::Into; - use dojo_core::{auth::components::AuthStatus, integer::u250}; + use dojo_core::auth::components::AuthStatus; - fn execute(role_id: u250, resource_id: u250) { + fn execute(role_id: felt252, resource_id: felt252) { commands::set_entity((role_id, resource_id).into(), (AuthStatus { is_authorized: true })); } } @@ -143,9 +141,9 @@ mod GrantResource { mod RevokeAuthRole { use traits::Into; use array::ArrayTrait; - use dojo_core::{auth::components::AuthRole, integer::u250}; + use dojo_core::auth::components::AuthRole; - fn execute(target_id: u250) { + fn execute(target_id: felt252) { commands::set_entity(target_id.into(), (AuthRole { id: 0.into() })); } } @@ -154,9 +152,9 @@ mod RevokeAuthRole { mod RevokeScopedAuthRole { use traits::Into; use array::ArrayTrait; - use dojo_core::{auth::components::AuthRole, integer::u250}; + use dojo_core::auth::components::AuthRole; - fn execute(target_id: u250, resource_id: u250) { + fn execute(target_id: felt252, resource_id: felt252) { commands::set_entity((target_id, resource_id).into(), (AuthRole { id: 0.into() })); } } @@ -164,9 +162,9 @@ mod RevokeScopedAuthRole { #[system] mod RevokeResource { use traits::Into; - use dojo_core::{auth::components::AuthStatus, integer::u250}; + use dojo_core::auth::components::AuthStatus; - fn execute(role_id: u250, resource_id: u250) { + fn execute(role_id: felt252, resource_id: felt252) { commands::set_entity((role_id, resource_id).into(), (AuthStatus { is_authorized: false })); } } diff --git a/crates/dojo-core/src/executor.cairo b/crates/dojo-core/src/executor.cairo index a0e6b912cb..0bd939fad7 100644 --- a/crates/dojo-core/src/executor.cairo +++ b/crates/dojo-core/src/executor.cairo @@ -7,7 +7,6 @@ mod Executor { use box::BoxTrait; use traits::Into; use dojo_core::execution_context::Context; - use dojo_core::integer::u250; use dojo_core::interfaces::{IWorldDispatcher, ISystemLibraryDispatcher, ISystemDispatcherTrait}; use dojo_core::auth::components::AuthRole; use starknet::contract_address::ContractAddressIntoFelt252; diff --git a/crates/dojo-core/src/integer.cairo b/crates/dojo-core/src/integer.cairo deleted file mode 100644 index 0f1762dc6e..0000000000 --- a/crates/dojo-core/src/integer.cairo +++ /dev/null @@ -1,179 +0,0 @@ -use hash::LegacyHash; -use integer::BoundedInt; -use option::OptionTrait; -use traits::{Into, TryInto}; - -use starknet::{ - ContractAddress, SyscallResult, contract_address::ContractAddressIntoFelt252, - storage_access::{StorageAccess, StorageBaseAddress} -}; - -// max value of u256's high part when u250::max is converted into u256 -const HIGH_BOUND: u128 = 0x3ffffffffffffffffffffffffffffff; - -#[derive(Copy, Drop, Serde)] -struct u250 { - inner: felt252 -} - -trait u250Trait { - fn new(inner: felt252) -> u250; -} - -impl U250Impl of u250Trait { - fn new(inner: felt252) -> u250 { - u250 { inner } - } -} - -// Implements the PartialEq trait for u250. -impl U250PartialEq of PartialEq { - fn eq(lhs: u250, rhs: u250) -> bool { - lhs.inner == rhs.inner - } - - fn ne(lhs: u250, rhs: u250) -> bool { - lhs.inner != rhs.inner - } -} - -impl Felt252IntoU250 of Into { - fn into(self: felt252) -> u250 { - u250 { inner: self } - } -} - -impl Felt252TryIntoU250 of TryInto { - fn try_into(self: felt252) -> Option { - let v: u256 = self.into(); - if v.high > HIGH_BOUND { - return Option::None(()); - } - Option::Some(u250 { inner: self }) - } -} - -impl ContractAddressIntoU250 of Into { - fn into(self: ContractAddress) -> u250 { - u250 { inner: self.into() } - } -} - -impl U8IntoU250 of Into { - fn into(self: u8) -> u250 { - u250 { inner: self.into() } - } -} - -impl U16IntoU250 of Into { - fn into(self: u16) -> u250 { - u250 { inner: self.into() } - } -} - -impl U32IntoU250 of Into { - fn into(self: u32) -> u250 { - u250 { inner: self.into() } - } -} - -impl U64IntoU250 of Into { - fn into(self: u64) -> u250 { - u250 { inner: self.into() } - } -} - -impl U128IntoU250 of Into { - fn into(self: u128) -> u250 { - u250 { inner: self.into() } - } -} - -impl U250IntoFelt252 of Into { - fn into(self: u250) -> felt252 { - self.inner - } -} - -impl LegacyHashU250 of LegacyHash { - fn hash(state: felt252, value: u250) -> felt252 { - LegacyHash::hash(state, U250IntoFelt252::into(value)) - } -} - -impl StorageAccessU250 of StorageAccess { - fn read(address_domain: u32, base: StorageBaseAddress) -> SyscallResult { - Result::Ok( - Felt252TryIntoU250::try_into(StorageAccess::read(address_domain, base)?) - .expect('StorageAccessU250 - non u250') - ) - } - #[inline(always)] - fn write(address_domain: u32, base: StorageBaseAddress, value: u250) -> SyscallResult<()> { - StorageAccess::write(address_domain, base, U250IntoFelt252::into(value)) - } -} - -impl U250Zeroable of Zeroable { - #[inline(always)] - fn zero() -> u250 { - u250 { inner: 0 } - } - - #[inline(always)] - fn is_zero(self: u250) -> bool { - self.inner == 0 - } - - #[inline(always)] - fn is_non_zero(self: u250) -> bool { - self.inner != 0 - } -} - -impl U250BoundedInt of BoundedInt { - #[inline(always)] - fn min() -> u250 nopanic { - u250 { inner: 0 } - } - - #[inline(always)] - fn max() -> u250 nopanic { - // 2^250 - 1 - u250 { inner: 0x3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff } - } -} - -impl U250Add of Add { - #[inline(always)] - fn add(lhs: u250, rhs: u250) -> u250 { - let r = u250 { inner: lhs.inner + rhs.inner }; - let r256: u256 = r.inner.into(); - assert(r256.high <= HIGH_BOUND, 'u250 overflow'); - r - } -} - -impl U250AddEq of AddEq { - #[inline(always)] - fn add_eq(ref self: u250, other: u250) { - self = self + other; - } -} - -impl U250Sub of Sub { - #[inline(always)] - fn sub(lhs: u250, rhs: u250) -> u250 { - let lhs256: u256 = lhs.inner.into(); - let rhs256: u256 = rhs.inner.into(); - assert(lhs256 >= rhs256, 'u250 underflow'); - u250 { inner: (lhs256 - rhs256).try_into().unwrap() } - } -} - -impl U250SubEq of SubEq { - #[inline(always)] - fn sub_eq(ref self: u250, other: u250) { - self = self - other; - } -} diff --git a/crates/dojo-core/src/interfaces.cairo b/crates/dojo-core/src/interfaces.cairo index 194294fc50..008f489f6c 100644 --- a/crates/dojo-core/src/interfaces.cairo +++ b/crates/dojo-core/src/interfaces.cairo @@ -1,5 +1,5 @@ use dojo_core::{ - integer::u250, serde::SpanSerde, storage::query::Query, + serde::SpanSerde, storage::query::Query, auth::systems::Route, auth::components::AuthRole, execution_context::Context }; use starknet::{ClassHash, ContractAddress}; @@ -23,15 +23,15 @@ trait IWorld { fn set_entity( context: Context, component: felt252, key: Query, offset: u8, value: Span ); - fn entities(component: felt252, partition: u250) -> (Span, Span>); + fn entities(component: felt252, partition: felt252) -> (Span, Span>); fn set_executor(contract_address: ContractAddress); fn is_authorized(system: felt252, component: felt252, execution_role: AuthRole) -> bool; fn is_account_admin() -> bool; fn is_system_for_execution(system: felt252) -> bool; fn delete_entity(context: Context, component: felt252, query: Query); - fn assume_role(role_id: u250, systems: Array); + fn assume_role(role_id: felt252, systems: Array); fn clear_role(systems: Array); - fn execution_role() -> u250; + fn execution_role() -> felt252; fn system_components(system: felt252) -> Array<(felt252, bool)>; } diff --git a/crates/dojo-core/src/lib.cairo b/crates/dojo-core/src/lib.cairo index 112b896cf9..b4731236f3 100644 --- a/crates/dojo-core/src/lib.cairo +++ b/crates/dojo-core/src/lib.cairo @@ -1,7 +1,6 @@ mod auth; mod executor; mod execution_context; -mod integer; mod interfaces; mod serde; mod storage; diff --git a/crates/dojo-core/src/storage/db.cairo b/crates/dojo-core/src/storage/db.cairo index b5ba263773..87546566ba 100644 --- a/crates/dojo-core/src/storage/db.cairo +++ b/crates/dojo-core/src/storage/db.cairo @@ -8,20 +8,19 @@ mod Database { use dojo_core::serde::SpanSerde; use dojo_core::storage::{index::Index, kv::KeyValueStore, query::{Query, QueryTrait}}; - use dojo_core::integer::{u250, Felt252IntoU250}; use dojo_core::interfaces::{IComponentLibraryDispatcher, IComponentDispatcherTrait}; #[event] - fn StoreSetRecord(table_id: u250, keys: Span, value: Span) {} + fn StoreSetRecord(table_id: felt252, keys: Span, value: Span) {} #[event] - fn StoreSetField(table_id: u250, keys: Span, offset: u8, value: Span) {} + fn StoreSetField(table_id: felt252, keys: Span, offset: u8, value: Span) {} #[event] - fn StoreDeleteRecord(table_id: u250, keys: Span) {} + fn StoreDeleteRecord(table_id: felt252, keys: Span) {} fn get( - class_hash: starknet::ClassHash, table: u250, query: Query, offset: u8, length: usize + class_hash: starknet::ClassHash, table: felt252, query: Query, offset: u8, length: usize ) -> Option> { let mut length = length; if length == 0 { @@ -36,7 +35,7 @@ mod Database { } fn set( - class_hash: starknet::ClassHash, table: u250, query: Query, offset: u8, value: Span + class_hash: starknet::ClassHash, table: felt252, query: Query, offset: u8, value: Span ) { let keys = query.keys(); let id = query.hash(); @@ -51,7 +50,7 @@ mod Database { StoreSetField(table, keys, offset, value); } - fn del(class_hash: starknet::ClassHash, table: u250, query: Query) { + fn del(class_hash: starknet::ClassHash, table: felt252, query: Query) { Index::delete(table, query.hash()); StoreDeleteRecord(table, query.keys()); } @@ -59,8 +58,8 @@ mod Database { // returns a tuple of spans, first contains the entity IDs, // second the deserialized entities themselves fn all( - class_hash: starknet::ClassHash, component: u250, partition: u250 - ) -> (Span, Span>) { + class_hash: starknet::ClassHash, component: felt252, partition: felt252 + ) -> (Span, Span>) { let table = { if partition == 0.into() { component diff --git a/crates/dojo-core/src/storage/index.cairo b/crates/dojo-core/src/storage/index.cairo index 43f926dbde..f74b25c8c4 100644 --- a/crates/dojo-core/src/storage/index.cairo +++ b/crates/dojo-core/src/storage/index.cairo @@ -4,18 +4,16 @@ mod Index { use traits::Into; use option::OptionTrait; - use dojo_core::integer::u250; - struct Storage { // Maps id to its position in the table. // NOTE: ids is 1-indexed to allow for 0 // to be used as a sentinel value. - ids: LegacyMap::<(u250, u250), usize>, - table_lens: LegacyMap::, - tables: LegacyMap::<(u250, usize), u250>, + ids: LegacyMap::<(felt252, felt252), usize>, + table_lens: LegacyMap::, + tables: LegacyMap::<(felt252, usize), felt252>, } - fn create(table: u250, id: u250) { + fn create(table: felt252, id: felt252) { if exists(table, id) { return (); } @@ -26,7 +24,7 @@ mod Index { tables::write((table, table_len), id); } - fn delete(table: u250, id: u250) { + fn delete(table: felt252, id: felt252) { if !exists(table, id) { return (); } @@ -41,11 +39,11 @@ mod Index { tables::write((table, table_idx), tables::read((table, table_len - 1))); } - fn exists(table: u250, id: u250) -> bool { + fn exists(table: felt252, id: felt252) -> bool { ids::read((table, id)) != 0 } - fn query(table: u250) -> Array { + fn query(table: felt252) -> Array { let mut res = ArrayTrait::new(); let table_len = table_lens::read(table); let mut idx: usize = 0; diff --git a/crates/dojo-core/src/storage/kv.cairo b/crates/dojo-core/src/storage/kv.cairo index 16884ceaa7..924d57af49 100644 --- a/crates/dojo-core/src/storage/kv.cairo +++ b/crates/dojo-core/src/storage/kv.cairo @@ -4,14 +4,14 @@ mod KeyValueStore { use starknet::SyscallResultTrait; use option::OptionTrait; - use dojo_core::{integer::u250, serde::SpanSerde}; + use dojo_core::serde::SpanSerde; - fn address(table: u250, key: u250) -> starknet::StorageBaseAddress { + fn address(table: felt252, key: felt252) -> starknet::StorageBaseAddress { starknet::storage_base_address_from_felt252(hash::LegacyHash::hash(table.into(), key)) } #[view] - fn get(table: u250, key: u250, offset: u8, length: usize) -> Span { + fn get(table: felt252, key: felt252, offset: u8, length: usize) -> Span { let address_domain = 0; let base = address(table, key); let mut value = ArrayTrait::new(); @@ -42,7 +42,7 @@ mod KeyValueStore { } #[external] - fn set(table: u250, query: u250, offset: u8, value: Span) { + fn set(table: felt252, query: felt252, offset: u8, value: Span) { let address_domain = 0; let base = address(table, query); _set(address_domain, base, value, offset: offset); diff --git a/crates/dojo-core/src/storage/query.cairo b/crates/dojo-core/src/storage/query.cairo index c90f402da0..59a9108010 100644 --- a/crates/dojo-core/src/storage/query.cairo +++ b/crates/dojo-core/src/storage/query.cairo @@ -6,37 +6,35 @@ use traits::Into; use zeroable::IsZeroResult; use starknet::ClassHashIntoFelt252; use poseidon::poseidon_hash_span; -use dojo_core::{ - serde::SpanSerde, integer::{u250, Felt252IntoU250, U250IntoFelt252} -}; +use dojo_core::serde::SpanSerde; #[derive(Copy, Drop, Serde)] struct Query { address_domain: u32, - partition: u250, - keys: Span, + partition: felt252, + keys: Span, } trait QueryTrait { - fn new(address_domain: u32, partition: u250, keys: Span) -> Query; - fn new_from_id(id: u250) -> Query; - fn hash(self: @Query) -> u250; - fn table(self: @Query, component: felt252) -> u250; - fn keys(self: @Query) -> Span; + fn new(address_domain: u32, partition: felt252, keys: Span) -> Query; + fn new_from_id(id: felt252) -> Query; + fn hash(self: @Query) -> felt252; + fn table(self: @Query, component: felt252) -> felt252; + fn keys(self: @Query) -> Span; } impl QueryImpl of QueryTrait { - fn new(address_domain: u32, partition: u250, keys: Span) -> Query { + fn new(address_domain: u32, partition: felt252, keys: Span) -> Query { Query { address_domain, keys, partition } } - fn new_from_id(id: u250) -> Query { + fn new_from_id(id: felt252) -> Query { let mut keys = ArrayTrait::new(); keys.append(id); QueryTrait::new(0, 0.into(), keys.span()) } - fn hash(self: @Query) -> u250 { + fn hash(self: @Query) -> felt252 { let keys = *self.keys; if keys.len() == 1 { return *keys.at(0); @@ -47,7 +45,7 @@ impl QueryImpl of QueryTrait { poseidon_hash_span(serialized.span()).into() } - fn table(self: @Query, component: felt252) -> u250 { + fn table(self: @Query, component: felt252) -> felt252 { if *self.partition == 0.into() { return component.into(); } @@ -59,12 +57,12 @@ impl QueryImpl of QueryTrait { hash.into() } - fn keys(self: @Query) -> Span { + fn keys(self: @Query) -> Span { *self.keys } } -impl LiteralIntoQuery, impl E0Drop: Drop> of Into { +impl LiteralIntoQuery, impl E0Drop: Drop> of Into { fn into(self: E0) -> Query { let mut keys = ArrayTrait::new(); keys.append(E0Into::into(self)); @@ -73,7 +71,7 @@ impl LiteralIntoQuery, impl E0Drop: Drop> of } impl TupleSize1IntoQuery< - E0, impl E0Into: Into, impl E0Drop: Drop + E0, impl E0Into: Into, impl E0Drop: Drop > of Into<(E0, ), Query> { fn into(self: (E0, )) -> Query { let (first) = self; @@ -86,9 +84,9 @@ impl TupleSize1IntoQuery< impl TupleSize2IntoQuery< E0, E1, - impl E0Into: Into, + impl E0Into: Into, impl E0Drop: Drop, - impl E1Into: Into, + impl E1Into: Into, impl E1Drop: Drop, > of Into<(E0, E1), Query> { fn into(self: (E0, E1)) -> Query { @@ -104,11 +102,11 @@ impl TupleSize3IntoQuery< E0, E1, E2, - impl E0Into: Into, + impl E0Into: Into, impl E0Drop: Drop, - impl E1Into: Into, + impl E1Into: Into, impl E1Drop: Drop, - impl E2Into: Into, + impl E2Into: Into, impl E2Drop: Drop, > of Into<(E0, E1, E2), Query> { fn into(self: (E0, E1, E2)) -> Query { @@ -128,7 +126,7 @@ trait IntoPartitioned { impl IntoPartitionedQuery< E0, E1, - impl E0Into: Into, + impl E0Into: Into, impl E0Drop: Drop, impl E1Into: Into, impl E1Drop: Drop, diff --git a/crates/dojo-core/src/storage/utils.cairo b/crates/dojo-core/src/storage/utils.cairo index 5e9108c008..f41423cf25 100644 --- a/crates/dojo-core/src/storage/utils.cairo +++ b/crates/dojo-core/src/storage/utils.cairo @@ -3,8 +3,6 @@ use dict::Felt252DictTrait; use option::OptionTrait; use traits::{Into, TryInto}; -use dojo_core::integer::u250; - // big enough number used to construct a compound key in `find_matching` const OFFSET: felt252 = 0x10000000000000000000000000000000000; @@ -27,7 +25,7 @@ const OFFSET: felt252 = 0x10000000000000000000000000000000000; // for all entities and the function respects the ID order from the first ID array, // hence 4 and 3 in this case fn find_matching( - mut ids: Span>, mut entities: Span>> + mut ids: Span>, mut entities: Span>> ) -> Span>> { assert(ids.len() == entities.len(), 'lengths dont match'); @@ -50,7 +48,7 @@ fn find_matching( // IDs from ids_match where the value is the same as match_count // we want to keep the ordering from the first entity IDs - let mut ids1: Span = *(ids.pop_front().unwrap()); + let mut ids1: Span = *(ids.pop_front().unwrap()); // counts how many ID arrays and hence entity types we've looped over // starts at 1 because we skip the first element to keep ordering (see above) @@ -68,13 +66,13 @@ fn find_matching( match entity_ids.pop_front() { Option::Some(id) => { // keep track how many times we've encountered a particular ID - let c = ids_match[*id.inner]; - ids_match.insert(*id.inner, c + 1); + let c = ids_match[*id]; + ids_match.insert(*id, c + 1); // keep track of the index of the particular entity in an // entity type array, i.e. at which index is the entity // with `id` at, using the compound key id_to_idx - .insert(OFFSET * entity_type_counter.into() + *id.inner, index); + .insert(OFFSET * entity_type_counter.into() + *id, index); index += 1; }, Option::None(_) => { @@ -105,7 +103,7 @@ fn find_matching( loop { match ids1.pop_front() { Option::Some(id) => { - let id = *id.inner; + let id = *id; if ids_match[id] == found_in_all { // id was found in every entity_ids array diff --git a/crates/dojo-core/src/world.cairo b/crates/dojo-core/src/world.cairo index eb03d70086..bb80f9ef76 100644 --- a/crates/dojo-core/src/world.cairo +++ b/crates/dojo-core/src/world.cairo @@ -13,7 +13,6 @@ mod World { use dojo_core::storage::{db::Database, query::{Query, QueryTrait}}; use dojo_core::execution_context::Context; use dojo_core::auth::components::AuthRole; - use dojo_core::integer::{u250, ContractAddressIntoU250}; use dojo_core::auth::systems::Route; use dojo_core::interfaces::{ IComponentLibraryDispatcher, IComponentDispatcherTrait, IExecutorDispatcher, @@ -33,7 +32,7 @@ mod World { executor_dispatcher: IExecutorDispatcher, component_registry: LegacyMap::, system_registry: LegacyMap::, - _execution_role: LegacyMap::, + _execution_role: LegacyMap::, systems_for_execution: LegacyMap::<(ContractAddress, felt252), bool>, initialized: bool, nonce: usize, @@ -339,10 +338,10 @@ mod World { /// /// # Returns /// - /// * `Span` - The entity IDs + /// * `Span` - The entity IDs /// * `Span>` - The entities #[view] - fn entities(component: felt252, partition: u250) -> (Span, Span>) { + fn entities(component: felt252, partition: felt252) -> (Span, Span>) { let class_hash = component_registry::read(component); Database::all(class_hash, component.into(), partition) } @@ -372,7 +371,7 @@ mod World { /// * `role_id` - The role id to be assumed /// * `systems` - The systems to be validated #[external] - fn assume_role(role_id: u250, systems: Array) { + fn assume_role(role_id: felt252, systems: Array) { // Only Admin can set Admin role let caller = get_tx_info().unbox().account_contract_address; if role_id == ADMIN.into() { @@ -448,9 +447,9 @@ mod World { /// /// # Returns /// - /// * `u250` - The role id of the system + /// * `felt252` - The role id of the system #[view] - fn execution_role() -> u250 { + fn execution_role() -> felt252 { let caller = get_tx_info().unbox().account_contract_address; _execution_role::read(caller) } diff --git a/crates/dojo-core/tests/src/integer.cairo b/crates/dojo-core/tests/src/integer.cairo deleted file mode 100644 index b253662cc6..0000000000 --- a/crates/dojo-core/tests/src/integer.cairo +++ /dev/null @@ -1,62 +0,0 @@ -use integer::BoundedInt; -use option::OptionTrait; -use traits::TryInto; -use zeroable::Zeroable; - -use dojo_core::integer::u250; -use dojo_core::integer::Felt252TryIntoU250; - -#[test] -fn test_u250_felt252_conv() { - let a: Option = 1_felt252.try_into(); - assert(a.is_some(), '1 try_into u250'); - - // 250^2 - 1, max u250 - let m: Option = 0x3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff_felt252 - .try_into(); - assert(m.is_some(), 'max try_into u250'); - - // 250^2, max u250 + 1 - let o: Option = 0x400000000000000000000000000000000000000000000000000000000000000_felt252 - .try_into(); - assert(o.is_none(), 'max + 1 try_into u250'); -} - -#[test] -fn test_u250_zeroable() { - let zero: u250 = Zeroable::zero(); - assert(zero.inner == 0, 'u250 Zeroable::zero'); - assert(zero.is_zero(), 'u250 Zeroable::is_zero'); - assert(u250 { inner: 250 }.is_non_zero(), 'u250 Zeroable::is_non_zero'); -} - -#[test] -fn test_u250_addition() { - let one = u250 { inner: 1 }; - let two = u250 { inner: 2 }; - let three = u250 { inner: 3 }; - assert(one + two == three, 'u250 1 + 2 = 3'); - - let mut n = three; - n += one; - assert(n == u250 { inner: 4 }, 'u250 (3 += 1) = 4') -} - -#[test] -#[should_panic(expected: ('u250 overflow', ))] -fn test_u250_addition_overflow() { - let _ = BoundedInt::max() + u250 { inner: 1 }; -} - -#[test] -fn test_u250_subtraction() { - let one = u250 { inner: 1 }; - let two = u250 { inner: 2 }; - let three = u250 { inner: 3 }; - - assert(three - one == two, 'u250 3 - 1 = 2'); - - let mut n = three; - n -= one; - assert(n == two, 'u250 (3 -= 1) = 2'); -} diff --git a/crates/dojo-core/tests/src/lib.cairo b/crates/dojo-core/tests/src/lib.cairo index 80d7ddb4cf..47d74b77b5 100644 --- a/crates/dojo-core/tests/src/lib.cairo +++ b/crates/dojo-core/tests/src/lib.cairo @@ -1,5 +1,4 @@ mod executor; -mod integer; mod storage; mod world; mod world_factory; diff --git a/crates/dojo-core/tests/src/storage/index.cairo b/crates/dojo-core/tests/src/storage/index.cairo index 5c1a1b63a1..a429b4b09f 100644 --- a/crates/dojo-core/tests/src/storage/index.cairo +++ b/crates/dojo-core/tests/src/storage/index.cairo @@ -1,7 +1,6 @@ use array::ArrayTrait; use traits::Into; -use dojo_core::integer::u250; use dojo_core::storage::index::Index; #[test] diff --git a/crates/dojo-core/tests/src/storage/query.cairo b/crates/dojo-core/tests/src/storage/query.cairo index 771f66075f..8fc88906e1 100644 --- a/crates/dojo-core/tests/src/storage/query.cairo +++ b/crates/dojo-core/tests/src/storage/query.cairo @@ -7,9 +7,6 @@ use traits::Into; use zeroable::IsZeroResult; use starknet::ClassHashIntoFelt252; use poseidon::poseidon_hash_span; -use dojo_core::integer::u250; -use dojo_core::integer::Felt252IntoU250; -use dojo_core::integer::U250IntoFelt252; use dojo_core::serde::SpanSerde; use dojo_core::storage::query::IntoPartitioned; use dojo_core::storage::query::TupleSize2IntoQuery; diff --git a/crates/dojo-core/tests/src/storage/utils.cairo b/crates/dojo-core/tests/src/storage/utils.cairo index fba61b59d2..5e02711c31 100644 --- a/crates/dojo-core/tests/src/storage/utils.cairo +++ b/crates/dojo-core/tests/src/storage/utils.cairo @@ -2,7 +2,6 @@ use array::{ArrayTrait, SpanTrait}; use option::OptionTrait; use traits::Into; -use dojo_core::integer::u250; use dojo_core::storage::utils::find_matching; fn build_fake_entity(v: felt252) -> Span { @@ -23,26 +22,26 @@ fn assert_entity(entity: Span, v: felt252) { #[test] #[available_gas(1000000000)] fn test_find_matching() { - let mut ids1: Array = ArrayTrait::new(); - let mut ids2: Array = ArrayTrait::new(); - let mut ids3: Array = ArrayTrait::new(); - - ids1.append(u250 { inner: 1 }); - ids1.append(u250 { inner: 3 }); - ids1.append(u250 { inner: 6 }); - ids1.append(u250 { inner: 5 }); - - ids2.append(u250 { inner: 4 }); - ids2.append(u250 { inner: 5 }); - ids2.append(u250 { inner: 3 }); - - ids3.append(u250 { inner: 3 }); - ids3.append(u250 { inner: 2 }); - ids3.append(u250 { inner: 1 }); - ids3.append(u250 { inner: 7 }); - ids3.append(u250 { inner: 5 }); - - let mut ids: Array> = ArrayTrait::new(); + let mut ids1: Array = ArrayTrait::new(); + let mut ids2: Array = ArrayTrait::new(); + let mut ids3: Array = ArrayTrait::new(); + + ids1.append(1); + ids1.append(3); + ids1.append(6); + ids1.append(5); + + ids2.append(4); + ids2.append(5); + ids2.append(3); + + ids3.append(3); + ids3.append(2); + ids3.append(1); + ids3.append(7); + ids3.append(5); + + let mut ids: Array> = ArrayTrait::new(); ids.append(ids1.span()); ids.append(ids2.span()); ids.append(ids3.span()); @@ -102,20 +101,20 @@ fn test_find_matching() { #[available_gas(1000000000)] #[should_panic(expected: ('lengths dont match', ))] fn test_find_matching_wrong_arg_len() { - let mut ids1: Array = ArrayTrait::new(); - let mut ids2: Array = ArrayTrait::new(); - let mut ids3: Array = ArrayTrait::new(); + let mut ids1: Array = ArrayTrait::new(); + let mut ids2: Array = ArrayTrait::new(); + let mut ids3: Array = ArrayTrait::new(); - ids1.append(u250 { inner: 1 }); - ids1.append(u250 { inner: 3 }); - ids1.append(u250 { inner: 6 }); - ids1.append(u250 { inner: 5 }); + ids1.append(1); + ids1.append(3); + ids1.append(6); + ids1.append(5); - ids2.append(u250 { inner: 4 }); - ids2.append(u250 { inner: 5 }); - ids2.append(u250 { inner: 3 }); + ids2.append(4); + ids2.append(5); + ids2.append(3); - let mut ids: Array> = ArrayTrait::new(); + let mut ids: Array> = ArrayTrait::new(); ids.append(ids1.span()); ids.append(ids2.span()); diff --git a/crates/dojo-core/tests/src/world.cairo b/crates/dojo-core/tests/src/world.cairo index d9d28df0a0..5db088548f 100644 --- a/crates/dojo-core/tests/src/world.cairo +++ b/crates/dojo-core/tests/src/world.cairo @@ -9,8 +9,6 @@ use starknet::class_hash::Felt252TryIntoClassHash; use starknet::syscalls::deploy_syscall; use starknet::contract_address_const; -use dojo_core::integer::u250; -use dojo_core::integer::{U32IntoU250, Felt252IntoU250}; use dojo_core::storage::query::QueryTrait; use dojo_core::interfaces::IWorldDispatcher; use dojo_core::interfaces::IWorldDispatcherTrait; @@ -41,7 +39,6 @@ mod Bar { use super::Foo; use traits::Into; use starknet::get_caller_address; - use dojo_core::integer::u250; fn execute(a: felt252, b: u128) { let caller = get_caller_address(); @@ -54,7 +51,6 @@ mod Buzz { use super::{Foo, Fizz}; use traits::Into; use starknet::get_caller_address; - use dojo_core::integer::u250; fn execute(a: felt252, b: u128) { let caller = get_caller_address(); diff --git a/crates/dojo-defi/src/constant_product_market/systems.cairo b/crates/dojo-defi/src/constant_product_market/systems.cairo index d3036d3c77..21c56a874f 100644 --- a/crates/dojo-defi/src/constant_product_market/systems.cairo +++ b/crates/dojo-defi/src/constant_product_market/systems.cairo @@ -2,11 +2,10 @@ mod Buy { use traits::Into; use array::ArrayTrait; - use dojo_core::integer::{u250, ContractAddressIntoU250}; use dojo_defi::constant_product_market::components::{Item, Cash, Market, MarketTrait}; - fn execute(partition: u250, item_id: u250, quantity: usize) { - let player: u250 = starknet::get_caller_address().into(); + fn execute(partition: felt252, item_id: felt252, quantity: usize) { + let player: felt252 = starknet::get_caller_address().into(); let cash_sk: Query = (partition, (player)).into_partitioned(); let player_cash = commands::::entity(cash_sk); @@ -44,11 +43,10 @@ mod Buy { mod Sell { use traits::Into; use array::ArrayTrait; - use dojo_core::integer::{u250, ContractAddressIntoU250}; use dojo_defi::constant_product_market::components::{Item, Cash, Market, MarketTrait}; - fn execute(partition: u250, item_id: u250, quantity: usize) { - let player: u250 = starknet::get_caller_address().into(); + fn execute(partition: felt252, item_id: felt252, quantity: usize) { + let player: felt252 = starknet::get_caller_address().into(); let item_sk: Query = (partition, (player, item_id)).into_partitioned(); let maybe_item = commands::::try_entity(item_sk); @@ -86,13 +84,12 @@ mod Sell { mod AddLiquidity { use traits::Into; use array::ArrayTrait; - use dojo_core::integer::{u250, ContractAddressIntoU250}; use dojo_defi::constant_product_market::components::{Item, Cash, Market, Liquidity, MarketTrait}; use cubit::types::fixed::Fixed; - fn execute(partition: u250, item_id: u250, amount: u128, quantity: usize) { - let player: u250 = starknet::get_caller_address().into(); + fn execute(partition: felt252, item_id: felt252, amount: u128, quantity: usize) { + let player: felt252 = starknet::get_caller_address().into(); let item_sk: Query = (partition, (player, item_id)).into_partitioned(); let maybe_item = commands::::try_entity(item_sk); @@ -138,14 +135,13 @@ mod AddLiquidity { mod RemoveLiquidity { use traits::Into; use array::ArrayTrait; - use dojo_core::integer::{u250, ContractAddressIntoU250}; use dojo_defi::constant_product_market::components::{Item, Cash, Market, Liquidity, MarketTrait}; use cubit::types::fixed::Fixed; use serde::Serde; - fn execute(partition: u250, item_id: u250, shares: Fixed) { - let player: u250 = starknet::get_caller_address().into(); + fn execute(partition: felt252, item_id: felt252, shares: Fixed) { + let player: felt252 = starknet::get_caller_address().into(); let liquidity_sk: Query = (partition, (player, item_id)).into_partitioned(); let player_liquidity = commands::::entity(liquidity_sk); diff --git a/crates/dojo-erc/src/erc20/erc20.cairo b/crates/dojo-erc/src/erc20/erc20.cairo index 4cb8db3a7f..26cc3f5386 100644 --- a/crates/dojo-erc/src/erc20/erc20.cairo +++ b/crates/dojo-erc/src/erc20/erc20.cairo @@ -23,7 +23,7 @@ mod ERC20 { IntoPartitionedQuery }; - use dojo_core::{integer::u250, interfaces::{IWorldDispatcher, IWorldDispatcherTrait}}; + use dojo_core::interfaces::{IWorldDispatcher, IWorldDispatcherTrait}; use dojo_erc::erc20::components::{Allowance, Balance, Supply}; struct Storage { diff --git a/crates/dojo-lang/src/commands/entities.rs b/crates/dojo-lang/src/commands/entities.rs index 8120b17d20..8cde03c3df 100644 --- a/crates/dojo-lang/src/commands/entities.rs +++ b/crates/dojo-lang/src/commands/entities.rs @@ -40,7 +40,7 @@ impl CommandTrait for EntitiesCommand { command.data.rewrite_nodes.push(RewriteNode::interpolate_patched( " - let mut __$query_id$_ids: Array> = ArrayTrait::new(); + let mut __$query_id$_ids: Array> = ArrayTrait::new(); let mut __$query_id$_entities_raw: Array>> = ArrayTrait::new(); ", @@ -56,8 +56,7 @@ impl CommandTrait for EntitiesCommand { RewriteNode::interpolate_patched( " let (__$query_id$_$query_subtype$_ids, __$query_id$_$query_subtype$_raw) = \ - ctx.world.entities('$component$', \ - dojo_core::integer::u250Trait::new($partition$)); + ctx.world.entities('$component$', $partition$); __$query_id$_ids.append(__$query_id$_$query_subtype$_ids); __$query_id$_entities_raw.append(__$query_id$_$query_subtype$_raw); ", diff --git a/crates/dojo-lang/src/plugin_test_data/system b/crates/dojo-lang/src/plugin_test_data/system index cc191f0e76..7dd898c1d6 100644 --- a/crates/dojo-lang/src/plugin_test_data/system +++ b/crates/dojo-lang/src/plugin_test_data/system @@ -458,18 +458,18 @@ mod Move { #[external] fn execute(ctx: Context, player_id: felt252) { - let mut __positions_query_ids: Array> = ArrayTrait::new(); + let mut __positions_query_ids: Array> = ArrayTrait::new(); let mut __positions_query_entities_raw: Array>> = ArrayTrait::new(); let (__positions_query_position_ids, __positions_query_position_raw) = ctx .world - .entities('Position', dojo_core::integer::u250Trait::new(0)); + .entities('Position', 0); __positions_query_ids.append(__positions_query_position_ids); __positions_query_entities_raw.append(__positions_query_position_raw); let (__positions_query_player_ids, __positions_query_player_raw) = ctx .world - .entities('Player', dojo_core::integer::u250Trait::new(0)); + .entities('Player', 0); __positions_query_ids.append(__positions_query_player_ids); __positions_query_entities_raw.append(__positions_query_player_raw); @@ -527,12 +527,12 @@ mod Move { let positions_query = (__positions, __players); - let mut __players_query_ids: Array> = ArrayTrait::new(); + let mut __players_query_ids: Array> = ArrayTrait::new(); let mut __players_query_entities_raw: Array>> = ArrayTrait::new(); let (__players_query_player_ids, __players_query_player_raw) = ctx .world - .entities('Player', dojo_core::integer::u250Trait::new(0)); + .entities('Player', 0); __players_query_ids.append(__players_query_player_ids); __players_query_entities_raw.append(__players_query_player_raw); @@ -614,20 +614,19 @@ mod Move { .expect('Player failed to deserialize'); let player_position = (__player_position_position, __player_position_player); if foo.len() > 0_u32 { - let mut __positions_query_ids: Array> = - ArrayTrait::new(); + let mut __positions_query_ids: Array> = ArrayTrait::new(); let mut __positions_query_entities_raw: Array>> = ArrayTrait::new(); let (__positions_query_position_ids, __positions_query_position_raw) = ctx .world - .entities('Position', dojo_core::integer::u250Trait::new(0)); + .entities('Position', 0); __positions_query_ids.append(__positions_query_position_ids); __positions_query_entities_raw.append(__positions_query_position_raw); let (__positions_query_player_ids, __positions_query_player_raw) = ctx .world - .entities('Player', dojo_core::integer::u250Trait::new(0)); + .entities('Player', 0); __positions_query_ids.append(__positions_query_player_ids); __positions_query_entities_raw.append(__positions_query_player_raw); @@ -814,20 +813,19 @@ mod Move { .expect('Player failed to deserialize'); let player_position = (__player_position_position, __player_position_player); if bar == 123 { - let mut __positions_query_ids: Array> = - ArrayTrait::new(); + let mut __positions_query_ids: Array> = ArrayTrait::new(); let mut __positions_query_entities_raw: Array>> = ArrayTrait::new(); let (__positions_query_position_ids, __positions_query_position_raw) = ctx .world - .entities('Position', dojo_core::integer::u250Trait::new(0)); + .entities('Position', 0); __positions_query_ids.append(__positions_query_position_ids); __positions_query_entities_raw.append(__positions_query_position_raw); let (__positions_query_player_ids, __positions_query_player_raw) = ctx .world - .entities('Player', dojo_core::integer::u250Trait::new(0)); + .entities('Player', 0); __positions_query_ids.append(__positions_query_player_ids); __positions_query_entities_raw.append(__positions_query_player_raw); diff --git a/crates/torii/src/graphql/object/storage.rs b/crates/torii/src/graphql/object/storage.rs index 1a09f8eb95..19a94dc3d7 100644 --- a/crates/torii/src/graphql/object/storage.rs +++ b/crates/torii/src/graphql/object/storage.rs @@ -117,7 +117,6 @@ fn value_mapping_from_row(row: &SqliteRow, fields: &TypeMapping) -> Result { diff --git a/crates/torii/src/graphql/types.rs b/crates/torii/src/graphql/types.rs index 3ff2e765e4..55929dde87 100644 --- a/crates/torii/src/graphql/types.rs +++ b/crates/torii/src/graphql/types.rs @@ -9,7 +9,6 @@ impl ScalarType { pub const U32: &'static str = "u32"; pub const U64: &'static str = "u64"; pub const U128: &'static str = "u128"; - pub const U250: &'static str = "u250"; pub const U256: &'static str = "u256"; pub const USIZE: &'static str = "usize"; pub const BOOL: &'static str = "bool"; @@ -25,7 +24,6 @@ impl ScalarType { ScalarType::U32, ScalarType::U64, ScalarType::U128, - ScalarType::U250, ScalarType::U256, ScalarType::USIZE, ScalarType::BOOL,