Skip to content

Commit

Permalink
Migrate contract names to snake case (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Jun 30, 2023
1 parent 8626f9f commit 5715d09
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 65 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ chrono = { version = "0.4.24", features = [ "serde" ] }
clap = { version = "4.2", features = [ "derive" ] }
colored = "2"
console = "0.15.7"
convert_case = "0.6.0"
env_logger = "0.10.0"
flate2 = "1.0.24"
indoc = "1.0.7"
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ mod World {
}

#[system]
mod LibraryCall {
mod library_call {
fn execute(
class_hash: starknet::ClassHash, entrypoint: felt252, calladata: Span<felt252>
) -> Span<felt252> {
Expand Down
20 changes: 10 additions & 10 deletions crates/dojo-core/tests/src/world.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use dojo::interfaces::IWorldDispatcher;
use dojo::interfaces::IWorldDispatcherTrait;
use dojo::executor::Executor;
use dojo::world::World;
use dojo::world::LibraryCall;
use dojo::world::library_call;

// Components and Systems

Expand Down Expand Up @@ -74,7 +74,7 @@ fn test_component() {
let name = 'Foo'.into();
let world = deploy_world();

world.register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(foo::TEST_CLASS_HASH.try_into().unwrap());
let mut data = ArrayTrait::new();
data.append(1337);
let id = world.uuid();
Expand All @@ -89,7 +89,7 @@ fn test_component_with_partition() {
let name = 'Foo'.into();
let world = deploy_world();

world.register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(foo::TEST_CLASS_HASH.try_into().unwrap());
let mut data = ArrayTrait::new();
data.append(1337);
let id = world.uuid();
Expand All @@ -107,7 +107,7 @@ fn test_system() {
let world = spawn_empty_world();

world.register_system(Bar::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(foo::TEST_CLASS_HASH.try_into().unwrap());
let mut data = ArrayTrait::new();
data.append(1337);
data.append(1337);
Expand Down Expand Up @@ -135,7 +135,7 @@ fn test_set_entity_admin() {
let world = spawn_empty_world();

world.register_system(Bar::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(foo::TEST_CLASS_HASH.try_into().unwrap());

let alice = starknet::contract_address_const::<0x1337>();
starknet::testing::set_contract_address(alice);
Expand All @@ -158,7 +158,7 @@ fn test_set_entity_unauthorized() {
let world = spawn_empty_world();

world.register_system(Bar::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(foo::TEST_CLASS_HASH.try_into().unwrap());

let caller = starknet::contract_address_const::<0x1337>();
starknet::testing::set_account_contract_address(caller);
Expand All @@ -178,7 +178,7 @@ fn test_set_entity_directly() {
let world = spawn_empty_world();

world.register_system(Bar::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
world.register_component(foo::TEST_CLASS_HASH.try_into().unwrap());

// Change Foo component directly
let id = world.uuid();
Expand Down Expand Up @@ -218,12 +218,12 @@ fn test_library_call_system() {
// Spawn empty world
let world = spawn_empty_world();

world.register_system(LibraryCall::TEST_CLASS_HASH.try_into().unwrap());
world.register_system(library_call::TEST_CLASS_HASH.try_into().unwrap());
let mut calldata = ArrayTrait::new();
calldata.append(FooComponent::TEST_CLASS_HASH);
calldata.append(foo::TEST_CLASS_HASH);
calldata.append(0x011efd13169e3bceace525b23b7f968b3cc611248271e35f04c5c917311fc7f7);
calldata.append(0);
world.execute('LibraryCall'.into(), calldata.span());
world.execute('library_call'.into(), calldata.span());
}

#[test]
Expand Down
12 changes: 6 additions & 6 deletions crates/dojo-core/tests/src/world_factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct Foo {
}

#[system]
mod Bar {
mod bar {
use super::Foo;

fn execute(foo: Foo) -> Foo {
Expand Down Expand Up @@ -80,10 +80,10 @@ fn test_spawn_world() {

// Prepare components and systems
let mut systems: Array<ClassHash> = array::ArrayTrait::new();
systems.append(Bar::TEST_CLASS_HASH.try_into().unwrap());
systems.append(bar::TEST_CLASS_HASH.try_into().unwrap());

let mut components: Array<ClassHash> = array::ArrayTrait::new();
components.append(FooComponent::TEST_CLASS_HASH.try_into().unwrap());
components.append(foo::TEST_CLASS_HASH.try_into().unwrap());

// Spawn World from WorldFactory
let world_address = factory.spawn(components, systems);
Expand All @@ -92,9 +92,9 @@ fn test_spawn_world() {
// Check Foo component and Bar system are registered
let foo_hash = world.component('Foo'.into());
assert(
foo_hash == FooComponent::TEST_CLASS_HASH.try_into().unwrap(), 'component not registered'
foo_hash == foo::TEST_CLASS_HASH.try_into().unwrap(), 'component not registered'
);

let bar_hash = world.system('Bar'.into());
assert(bar_hash == Bar::TEST_CLASS_HASH.try_into().unwrap(), 'system not registered');
let bar_hash = world.system('bar'.into());
assert(bar_hash == bar::TEST_CLASS_HASH.try_into().unwrap(), 'system not registered');
}
1 change: 1 addition & 0 deletions crates/dojo-lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cairo-lang-sierra-generator.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-starknet.workspace = true
cairo-lang-utils.workspace = true
convert_case.workspace = true
dojo-world = { path = "../dojo-world" }
itertools.workspace = true
scarb.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Compiler for DojoCompiler {
for (decl, class) in zip(contracts, classes) {
let target_name = &unit.target().name;
let contract_name = decl.submodule_id.name(db.upcast_mut());
let file_name = format!("{target_name}_{contract_name}.json");
let file_name = format!("{target_name}-{contract_name}.json");

let mut file = target_dir.open_rw(file_name.clone(), "output file", ws.config())?;
serde_json::to_writer_pretty(file.deref_mut(), &class)
Expand Down
4 changes: 3 additions & 1 deletion crates/dojo-lang/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::QueryAttrs;
use cairo_lang_syntax::node::{ast, Terminal, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use convert_case::{Case, Casing};
use dojo_world::manifest::Member;

use crate::plugin::{Component, DojoAuxData};
Expand Down Expand Up @@ -105,7 +106,7 @@ pub fn handle_component_struct(
}
#[starknet::contract]
mod $type_name$Component {
mod $contract_name$ {
use super::$type_name$;
#[storage]
Expand All @@ -122,6 +123,7 @@ pub fn handle_component_struct(
}
",
UnorderedHashMap::from([
("contract_name".to_string(), RewriteNode::Text(name.to_case(Case::Snake))),
(
"type_name".to_string(),
RewriteNode::new_trimmed(struct_ast.name(db).as_syntax_node()),
Expand Down
7 changes: 4 additions & 3 deletions crates/dojo-lang/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cairo_lang_defs::ids::{ModuleId, ModuleItemId};
use cairo_lang_filesystem::ids::CrateId;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::plugin::DynPluginAuxData;
use convert_case::{Case, Casing};
use dojo_world::manifest::{Contract, Input, Output, System};
use itertools::Itertools;
use serde::Serialize;
Expand Down Expand Up @@ -75,8 +76,8 @@ impl Manifest {
// It needs the `Component` suffix because we are
// searching from the compiled contracts.
let class_hash = compiled_classes
.get(format!("{name}Component").as_str())
.with_context(|| format!("Contract {name} not found in target."))
.get(name.to_case(Case::Snake).as_str())
.with_context(|| format!("Component {name} not found in target."))
.unwrap();

self.0.components.push(dojo_world::manifest::Component {
Expand Down Expand Up @@ -130,7 +131,7 @@ impl Manifest {

let class_hash = compiled_classes
.get(name.as_str())
.with_context(|| format!("Contract {name} not found in target."))
.with_context(|| format!("System {name} not found in target."))
.unwrap();

self.0.systems.push(System {
Expand Down
6 changes: 3 additions & 3 deletions crates/dojo-lang/src/plugin_test_data/component
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ trait IPosition<T> {
}

#[starknet::contract]
mod PositionComponent {
mod position {
use super::Position;

#[storage]
Expand Down Expand Up @@ -116,7 +116,7 @@ trait IRoles<T> {
}

#[starknet::contract]
mod RolesComponent {
mod roles {
use super::Roles;

#[storage]
Expand Down Expand Up @@ -158,7 +158,7 @@ trait IPlayer<T> {
}

#[starknet::contract]
mod PlayerComponent {
mod player {
use super::Player;

#[storage]
Expand Down
26 changes: 13 additions & 13 deletions crates/dojo-lang/src/plugin_test_data/system
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Player {
}

#[system]
mod Spawn {
mod spawn {
use traits::Into;
use dojo::world::Context;
use super::Position;
Expand Down Expand Up @@ -67,7 +67,7 @@ mod Spawn {
}

#[system]
mod Move {
mod move {
use traits::Into;
use array::ArrayTrait;
use dojo::world::Context;
Expand Down Expand Up @@ -136,7 +136,7 @@ mod Move {

let mut calldata = ArrayTrait::new();
calldata.append('name');
execute!(ctx.world, SpawnSystem, 'PositionPlayerWriter'.into(), calldata.span());
execute!(ctx.world, spawn, 'PositionPlayerWriter'.into(), calldata.span());

// move_inner(@positions_query, 0_u32, positions_query.len());

Expand All @@ -162,7 +162,7 @@ mod Move {
}

#[system]
mod Proxy {
mod proxy {
fn execute(value: felt252) -> felt252 {
value
}
Expand All @@ -185,7 +185,7 @@ trait IPosition<T> {
}

#[starknet::contract]
mod PositionComponent {
mod position {
use super::Position;

#[storage]
Expand Down Expand Up @@ -228,7 +228,7 @@ trait IPlayer<T> {
}

#[starknet::contract]
mod PlayerComponent {
mod player {
use super::Player;

#[storage]
Expand Down Expand Up @@ -260,7 +260,7 @@ mod PlayerComponent {
}

#[starknet::contract]
mod Spawn {
mod spawn {
use option::OptionTrait;
use array::SpanTrait;

Expand All @@ -281,7 +281,7 @@ mod Spawn {

#[external(v0)]
fn name(self: @ContractState) -> felt252 {
'Spawn'
'spawn'
}

#[external(v0)]
Expand Down Expand Up @@ -400,7 +400,7 @@ mod Spawn {
}

#[starknet::contract]
mod Move {
mod move {
use option::OptionTrait;
use array::SpanTrait;

Expand All @@ -421,7 +421,7 @@ mod Move {

#[external(v0)]
fn name(self: @ContractState) -> felt252 {
'Move'
'move'
}

#[external(v0)]
Expand Down Expand Up @@ -901,7 +901,7 @@ mod Move {

let mut calldata = ArrayTrait::new();
calldata.append('name');
ctx.world.execute('SpawnSystem', calldata.span());
ctx.world.execute('spawn', calldata.span());

let mut i = 0;
loop {
Expand Down Expand Up @@ -939,7 +939,7 @@ mod Move {
}

#[starknet::contract]
mod Proxy {
mod proxy {
use option::OptionTrait;
use array::SpanTrait;

Expand All @@ -960,7 +960,7 @@ mod Proxy {

#[external(v0)]
fn name(self: @ContractState) -> felt252 {
'Proxy'
'proxy'
}

#[external(v0)]
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cairo-lang-filesystem.workspace = true
cairo-lang-project.workspace = true
cairo-lang-starknet.workspace = true
camino.workspace = true
convert_case.workspace = true
reqwest = { version = "0.11.18", default-features = false, features = [
"rustls-tls",
] }
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-world/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'a, P: Provider + Sync> ComponentReader<'a, P> {
let res = self
.world
.call(
"LibraryCall",
"library_call",
vec![FieldElement::THREE, self.class_hash, entrypoint, FieldElement::ZERO],
block_id,
)
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-world/src/component_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn test_component() {
assert_eq!(
component.class_hash(),
FieldElement::from_hex_be(
"0x03a51494efe5416272b29bee6a95d21c2742d9a8a132fd98216f6b2c00f8304d"
"0x0669ea11a6f52d1330f76322e0c2393fb72a73e0cbc05359854f2a539743d000"
)
.unwrap()
);
Expand Down
Loading

0 comments on commit 5715d09

Please sign in to comment.