Skip to content

Commit

Permalink
support libraries in sozo
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed Jan 21, 2025
1 parent c25a143 commit a872e02
Show file tree
Hide file tree
Showing 19 changed files with 730 additions and 32 deletions.
51 changes: 51 additions & 0 deletions bin/sozo/src/commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ enum ResourceInspect {
Contract(ContractInspect),
Model(ModelInspect),
Event(EventInspect),
Library(LibraryInspect),
}

#[derive(Debug, Tabled, Serialize)]
Expand Down Expand Up @@ -115,6 +116,18 @@ struct ContractInspect {
current_class_hash: String,
}

#[derive(Debug, Tabled, Serialize)]
struct LibraryInspect {
#[tabled(rename = "Libraries")]
tag: String,
#[tabled(rename = "Status")]
status: ResourceStatus,
#[tabled(rename = "Dojo Selector")]
selector: String,
#[tabled(rename = "Class Hash")]
current_class_hash: String,
}

#[derive(Debug, Tabled, Serialize)]
struct ModelInspect {
#[tabled(rename = "Models")]
Expand Down Expand Up @@ -267,6 +280,7 @@ fn inspect_world(world_diff: &WorldDiff) {
let mut contracts_disp = vec![];
let mut models_disp = vec![];
let mut events_disp = vec![];
let mut libraries_disp = vec![];

for resource in world_diff.resources.values() {
match resource.resource_type() {
Expand All @@ -286,6 +300,10 @@ fn inspect_world(world_diff: &WorldDiff) {
ResourceInspect::Event(e) => events_disp.push(e),
_ => unreachable!(),
},
ResourceType::Library => match resource_diff_display(world_diff, resource) {
ResourceInspect::Library(l) => libraries_disp.push(l),
_ => unreachable!(),
},
_ => {}
}
}
Expand All @@ -294,9 +312,11 @@ fn inspect_world(world_diff: &WorldDiff) {
contracts_disp.sort_by_key(|m| m.tag.to_string());
models_disp.sort_by_key(|m| m.tag.to_string());
events_disp.sort_by_key(|m| m.tag.to_string());
libraries_disp.sort_by_key(|m| m.tag.to_string());

print_table(&namespaces_disp, Some(Color::FG_BRIGHT_BLACK), None);
print_table(&contracts_disp, Some(Color::FG_BRIGHT_BLACK), None);
print_table(&libraries_disp, Some(Color::FG_BRIGHT_BLACK), None);
print_table(&models_disp, Some(Color::FG_BRIGHT_BLACK), None);
print_table(&events_disp, Some(Color::FG_BRIGHT_BLACK), None);
}
Expand Down Expand Up @@ -372,6 +392,37 @@ fn resource_diff_display(world_diff: &WorldDiff, resource: &ResourceDiff) -> Res
selector: format!("{:#066x}", resource.dojo_selector()),
})
}
ResourceType::Library => {
let (_current_class_hash, status) = match resource {
ResourceDiff::Created(_) => {
(resource.current_class_hash(), ResourceStatus::Created)
}
ResourceDiff::Updated(_, _remote) => {
(resource.current_class_hash(), ResourceStatus::Updated)
}
ResourceDiff::Synced(_, remote) => (
remote.current_class_hash(),
if has_dirty_perms {
ResourceStatus::DirtyLocalPerms
} else {
ResourceStatus::Synced
},
),
};

let status = if world_diff.profile_config.is_skipped(&resource.tag()) {
ResourceStatus::MigrationSkipped
} else {
status
};

ResourceInspect::Library(LibraryInspect {
tag: resource.tag(),
status,
current_class_hash: format!("{:#066x}", resource.current_class_hash()),
selector: format!("{:#066x}", resource.dojo_selector()),
})
}
ResourceType::Model => {
let status = match resource {
ResourceDiff::Created(_) => ResourceStatus::Created,
Expand Down
3 changes: 3 additions & 0 deletions crates/dojo/core/src/contract/interface.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#[starknet::interface]
pub trait IContract<T> {}

#[starknet::interface]
pub trait ILibrary<T> {}
10 changes: 7 additions & 3 deletions crates/dojo/core/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod contract {
pub mod interface;
pub use interface::{IContract, IContractDispatcher, IContractDispatcherTrait};
pub use interface::{
IContract, IContractDispatcher, IContractDispatcherTrait, ILibrary, ILibraryDispatcher,
ILibraryDispatcherTrait,
};

pub mod components {
pub mod upgradeable;
Expand All @@ -24,8 +27,9 @@ pub mod event {
pub mod meta {
pub mod interface;
pub use interface::{
IDeployedResource, IDeployedResourceDispatcher, IDeployedResourceDispatcherTrait, IDeployedResourceLibraryDispatcher,
IStoredResource, IStoredResourceDispatcher, IStoredResourceDispatcherTrait,
IDeployedResource, IDeployedResourceDispatcher, IDeployedResourceDispatcherTrait,
IDeployedResourceLibraryDispatcher, IStoredResource, IStoredResourceDispatcher,
IStoredResourceDispatcherTrait,
};

pub mod introspect;
Expand Down
4 changes: 1 addition & 3 deletions crates/dojo/core/src/world/iworld.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ pub trait IWorld<T> {
///
/// * `namespace` - The namespace of the contract to be registered.
/// * `class_hash` - The class hash of the contract.
fn register_library(
ref self: T, namespace: ByteArray, class_hash: ClassHash,
) -> ClassHash;
fn register_library(ref self: T, namespace: ByteArray, class_hash: ClassHash) -> ClassHash;

/// Initializes a contract associated registered in the world.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo/core/src/world/resource.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ use starknet::{ContractAddress, ClassHash};
///
/// - Unregistered: The unregistered state, required to ensure the security of the world
/// to not have operations done on non-existent resources.
///
///
/// WARNING: enum order must be preserved to ensure backward compatibility
///
///
#[derive(Drop, starknet::Store, Serde, Default, Debug)]
pub enum Resource {
Model: (ContractAddress, felt252),
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo/core/src/world/world_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ pub mod world {
// can upgrade it.
match self.resources.read(contract_selector) {
Resource::Library((
class_hash, _,
_, _,
)) => {
self.assert_caller_permissions(contract_selector, Permission::Owner);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#[starknet::contract]
pub mod $name$ {
use dojo::contract::IContract;
use dojo::contract::ILibrary;
use dojo::meta::IDeployedResource;

#[abi(embed_v0)]
pub impl $name$__LibraryImpl of ILibrary<ContractState> {}


// TODO: rename impl ??
#[abi(embed_v0)]
pub impl $name$__DeployedContractImpl of IDeployedResource<ContractState> {
fn dojo_name(self: @ContractState) -> ByteArray {
Expand Down
3 changes: 1 addition & 2 deletions crates/dojo/lang/src/cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::QueryAttrs;
use cairo_lang_syntax::node::{ast, Terminal, TypedSyntaxNode};

use crate::attribute_macros::DojoLibrary;

use super::attribute_macros::{
DojoContract, DojoEvent, DojoModel, DOJO_CONTRACT_ATTR, DOJO_EVENT_ATTR, DOJO_LIBRARY_ATTR,
DOJO_MODEL_ATTR,
};
use super::derive_macros::{dojo_derive_all, DOJO_INTROSPECT_DERIVE, DOJO_PACKED_DERIVE};
use super::inline_macros::SelectorFromTagMacro;
use crate::attribute_macros::DojoLibrary;

// #[cfg(test)]
// #[path = "plugin_test.rs"]
Expand Down
Loading

0 comments on commit a872e02

Please sign in to comment.