Skip to content

Commit

Permalink
feat: generate contracts for Dojo events
Browse files Browse the repository at this point in the history
  • Loading branch information
remybar committed Sep 16, 2024
1 parent 069bc1a commit e1bb916
Show file tree
Hide file tree
Showing 112 changed files with 40,576 additions and 5,127 deletions.
2 changes: 1 addition & 1 deletion crates/dojo-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn filter_model_tokens(tokens: &TokenizedAbi) -> TokenizedAbi {
// All types from introspect module can also be removed as the clients does not rely on them.
// Events are also always empty at model contract level.
fn skip_token(token: &Token) -> bool {
if token.type_path().starts_with("dojo::model::introspect") {
if token.type_path().starts_with("dojo::meta::introspect") {
return true;
}

Expand Down
65 changes: 65 additions & 0 deletions crates/dojo-core/src/event/event.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use starknet::SyscallResult;

use dojo::meta::Layout;
use dojo::meta::introspect::Ty;
use dojo::world::IWorldDispatcher;

pub trait Event<T> {
fn name() -> ByteArray;
fn namespace() -> ByteArray;
fn tag() -> ByteArray;

fn version() -> u8;

fn selector() -> felt252;
fn instance_selector(self: @T) -> felt252;

fn name_hash() -> felt252;
fn namespace_hash() -> felt252;

fn layout() -> Layout;
fn schema(self: @T) -> Ty;

fn packed_size() -> Option<usize>;
fn unpacked_size() -> Option<usize>;
}

#[starknet::interface]
pub trait IEvent<T> {
fn name(self: @T) -> ByteArray;
fn namespace(self: @T) -> ByteArray;
fn tag(self: @T) -> ByteArray;

fn version(self: @T) -> u8;

fn selector(self: @T) -> felt252;
fn name_hash(self: @T) -> felt252;
fn namespace_hash(self: @T) -> felt252;

fn packed_size(self: @T) -> Option<usize>;
fn unpacked_size(self: @T) -> Option<usize>;

fn layout(self: @T) -> Layout;
fn schema(self: @T) -> Ty;
}

/// Deploys an event with the given [`ClassHash`] and retrieves it's name.
/// Currently, the event is expected to already be declared by `sozo`.
///
/// # Arguments
///
/// * `salt` - A salt used to uniquely deploy the event.
/// * `class_hash` - Class Hash of the event.
pub fn deploy_and_get_metadata(
salt: felt252, class_hash: starknet::ClassHash
) -> SyscallResult<(starknet::ContractAddress, ByteArray, felt252, ByteArray, felt252)> {
let (contract_address, _) = starknet::syscalls::deploy_syscall(
class_hash, salt, [].span(), false,
)?;
let event = IEventDispatcher { contract_address };
let name = event.name();
let selector = event.selector();
let namespace = event.namespace();
let namespace_hash = event.namespace_hash();
Result::Ok((contract_address, name, selector, namespace, namespace_hash))
}
16 changes: 14 additions & 2 deletions crates/dojo-core/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ pub mod contract {
pub mod upgradeable;
}

pub mod model {
pub mod meta {
pub mod introspect;
pub mod layout;
pub use layout::{Layout, FieldLayout};
}

pub mod event {
pub mod event;
pub use event::{
Event, IEvent, IEventDispatcher, IEventDispatcherTrait, deploy_and_get_metadata
};
}

pub mod model {
pub mod model;
pub use model::{
Model, ModelIndex, ModelEntity, IModel, IModelDispatcher, IModelDispatcherTrait,
Expand Down Expand Up @@ -72,8 +81,11 @@ pub mod world {

#[cfg(test)]
mod tests {
mod model {
mod meta {
mod introspect;
}

mod model {
mod model;
}
mod storage {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dojo::model::{Layout, FieldLayout};
use dojo::meta::{Layout, FieldLayout};

#[derive(Copy, Drop, Serde)]
pub enum Ty {
Expand Down Expand Up @@ -240,9 +240,9 @@ pub impl Introspect_option<T, +Introspect<T>> of Introspect<Option<T>> {
fn layout() -> Layout {
Layout::Enum(
[
dojo::model::FieldLayout { // Some
dojo::meta::FieldLayout { // Some
selector: 0, layout: Introspect::<T>::layout() },
dojo::model::FieldLayout { // None
dojo::meta::FieldLayout { // None
selector: 1, layout: Layout::Fixed([].span()) },
].span()
)
Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions crates/dojo-core/src/model/metadata.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use core::byte_array::ByteArray;
use core::poseidon::poseidon_hash_span;
use core::serde::Serde;

use dojo::model::introspect::{Introspect, Ty, Struct, Member};
use dojo::model::{Model, ModelIndex, Layout, FieldLayout};
use dojo::meta::{Layout, FieldLayout};
use dojo::meta::introspect::{Introspect, Ty, Struct, Member};
use dojo::model::{Model, ModelIndex};
use dojo::utils;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

Expand Down Expand Up @@ -198,8 +199,8 @@ pub mod resource_metadata {
use super::ResourceMetadata;
use super::ResourceMetadataModel;

use dojo::model::introspect::{Introspect, Ty};
use dojo::model::Layout;
use dojo::meta::introspect::{Introspect, Ty};
use dojo::meta::Layout;

#[storage]
struct Storage {}
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo-core/src/model/model.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use starknet::SyscallResult;

use dojo::model::Layout;
use dojo::model::introspect::Ty;
use dojo::meta::Layout;
use dojo::meta::introspect::Ty;
use dojo::world::IWorldDispatcher;

#[derive(Copy, Drop, Serde, Debug, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/storage/layout.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use dojo::model::{Layout, FieldLayout};
use dojo::meta::{Layout, FieldLayout};
use dojo::utils::{combine_key, find_field_layout};

use super::database;
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/tests/base.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ mod invalid_model_world {
#[test]
#[available_gas(6000000)]
#[should_panic(
expected: ("Resource `dojo-invalid_model` is already registered", 'ENTRYPOINT_FAILED',)
expected: ("Model `dojo-invalid_model` is already registered", 'ENTRYPOINT_FAILED',)
)]
fn test_deploy_from_world_invalid_model() {
let world = deploy_world();
Expand Down
5 changes: 3 additions & 2 deletions crates/dojo-core/src/tests/benchmarks.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use starknet::storage_access::{
};
use starknet::syscalls::{storage_read_syscall, storage_write_syscall};

use dojo::model::{Model, Layout, ModelIndex};
use dojo::model::introspect::Introspect;
use dojo::meta::Layout;
use dojo::model::{Model, ModelIndex};
use dojo::meta::introspect::Introspect;
use dojo::storage::{database, storage};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use dojo::model::introspect::Introspect;
use dojo::model::{Layout, FieldLayout};
use dojo::meta::introspect::Introspect;
use dojo::meta::{Layout, FieldLayout};

#[derive(Drop, Introspect)]
struct Base {
Expand Down
5 changes: 3 additions & 2 deletions crates/dojo-core/src/tests/world/entities.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use core::array::{ArrayTrait, SpanTrait};

use starknet::{contract_address_const, ContractAddress};

use dojo::model::{ModelIndex, Layout, FieldLayout, Model};
use dojo::model::introspect::Introspect;
use dojo::meta::{Layout, FieldLayout};
use dojo::model::{ModelIndex, Model};
use dojo::meta::introspect::Introspect;
use dojo::storage::database::MAX_ARRAY_LENGTH;
use dojo::utils::entity_id_from_keys;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait, world};
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/tests/world/resources.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn test_upgrade_model_from_model_writer() {
}

#[test]
#[should_panic(expected: ("Resource `dojo-Foo` is already registered", 'ENTRYPOINT_FAILED',))]
#[should_panic(expected: ("Model `dojo-Foo` is already registered", 'ENTRYPOINT_FAILED',))]
fn test_upgrade_model_from_random_account() {
let bob = starknet::contract_address_const::<0xb0b>();
let alice = starknet::contract_address_const::<0xa11ce>();
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-core/src/utils/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::option::Option;
use core::poseidon::poseidon_hash_span;
use core::serde::Serde;

use dojo::model::{Layout, FieldLayout};
use dojo::meta::{Layout, FieldLayout};

/// Compute the poseidon hash of a serialized ByteArray
pub fn bytearray_hash(data: @ByteArray) -> felt252 {
Expand Down
12 changes: 10 additions & 2 deletions crates/dojo-core/src/world/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ pub fn no_namespace_write_access(caller: ContractAddress, namespace: @ByteArray)
format!("Caller `{:?}` has no write access on namespace `{}`", caller, namespace)
}

pub fn event_already_registered(namespace: @ByteArray, name: @ByteArray) -> ByteArray {
format!("Event `{}-{}` is already registered", namespace, name)
}

pub fn event_not_registered(namespace: @ByteArray, name: @ByteArray) -> ByteArray {
format!("Event `{}-{}` is not registered", namespace, name)
}

pub fn model_already_registered(namespace: @ByteArray, name: @ByteArray) -> ByteArray {
format!("Resource `{}-{}` is already registered", namespace, name)
format!("Model `{}-{}` is already registered", namespace, name)
}

pub fn model_not_registered(namespace: @ByteArray, name: @ByteArray) -> ByteArray {
format!("Resource `{}-{}` is not registered", namespace, name)
format!("Model `{}-{}` is not registered", namespace, name)
}

pub fn resource_not_registered(resource: felt252) -> ByteArray {
Expand Down
Loading

0 comments on commit e1bb916

Please sign in to comment.