From 72646199e31bddc96a35f1adcc3c9de8e4efd99a Mon Sep 17 00:00:00 2001 From: glihm Date: Sat, 13 Jan 2024 17:05:45 -0600 Subject: [PATCH] feat: add `ensure_abi` method into model generated contract (#1433) feat: add ensure_abi method into model generated contract --- .../compiler_cairo_v240/Scarb.lock | 2 +- .../dojo-lang/src/manifest_test_data/manifest | 100 +++++++++++++++++- crates/dojo-lang/src/model.rs | 4 + crates/dojo-lang/src/plugin_test_data/model | 36 +++++++ crates/dojo-world/src/contracts/model_test.rs | 2 +- examples/spawn-and-move/Scarb.lock | 2 +- 6 files changed, 141 insertions(+), 5 deletions(-) diff --git a/crates/dojo-lang/src/manifest_test_data/compiler_cairo_v240/Scarb.lock b/crates/dojo-lang/src/manifest_test_data/compiler_cairo_v240/Scarb.lock index 6f0435b5b6..bb593d0412 100644 --- a/crates/dojo-lang/src/manifest_test_data/compiler_cairo_v240/Scarb.lock +++ b/crates/dojo-lang/src/manifest_test_data/compiler_cairo_v240/Scarb.lock @@ -10,7 +10,7 @@ dependencies = [ [[package]] name = "dojo" -version = "0.4.4" +version = "0.5.0" dependencies = [ "dojo_plugin", ] diff --git a/crates/dojo-lang/src/manifest_test_data/manifest b/crates/dojo-lang/src/manifest_test_data/manifest index 1f9e0fb0e9..76df306c7a 100644 --- a/crates/dojo-lang/src/manifest_test_data/manifest +++ b/crates/dojo-lang/src/manifest_test_data/manifest @@ -1205,7 +1205,7 @@ test_manifest_file { "name": "dojo_examples::models::moves", "address": null, - "class_hash": "0x64495ca6dc1dc328972697b30468cea364bcb7452bbb6e4aaad3e4b3f190147", + "class_hash": "0x1e13e74f3cb66e022c2b58a8ab7670a065d12d050446e20f736bd5bdc37c17e", "abi": [ { "type": "function", @@ -1360,6 +1360,62 @@ test_manifest_file ], "state_mutability": "view" }, + { + "type": "enum", + "name": "dojo_examples::models::Direction", + "variants": [ + { + "name": "None", + "type": "()" + }, + { + "name": "Left", + "type": "()" + }, + { + "name": "Right", + "type": "()" + }, + { + "name": "Up", + "type": "()" + }, + { + "name": "Down", + "type": "()" + } + ] + }, + { + "type": "struct", + "name": "dojo_examples::models::Moves", + "members": [ + { + "name": "player", + "type": "core::starknet::contract_address::ContractAddress" + }, + { + "name": "remaining", + "type": "core::integer::u8" + }, + { + "name": "last_direction", + "type": "dojo_examples::models::Direction" + } + ] + }, + { + "type": "function", + "name": "ensure_abi", + "inputs": [ + { + "name": "model", + "type": "dojo_examples::models::Moves" + } + ], + "outputs": [], + "state_mutability": "view" + }, { "type": "event", "name": "dojo_examples::models::moves::Event", @@ -1374,7 +1430,7 @@ test_manifest_file { "name": "dojo_examples::models::position", "address": null, - "class_hash": "0x4cd20d231b04405a77b184c115dc60637e186504fad7f0929bd76cbd09c10b", + "class_hash": "0x2a7df852d6ef0af662dad741b97b423ba0fb34a1483599781ac9e7f6822bc25", "abi": [ { "type": "function", @@ -1529,6 +1585,46 @@ test_manifest_file ], "state_mutability": "view" }, + { + "type": "struct", + "name": "dojo_examples::models::Vec2", + "members": [ + { + "name": "x", + "type": "core::integer::u32" + }, + { + "name": "y", + "type": "core::integer::u32" + } + ] + }, + { + "type": "struct", + "name": "dojo_examples::models::Position", + "members": [ + { + "name": "player", + "type": "core::starknet::contract_address::ContractAddress" + }, + { + "name": "vec", + "type": "dojo_examples::models::Vec2" + } + ] + }, + { + "type": "function", + "name": "ensure_abi", + "inputs": [ + { + "name": "model", + "type": "dojo_examples::models::Position" + } + ], + "outputs": [], + "state_mutability": "view" + }, { "type": "event", "name": "dojo_examples::models::position::Event", diff --git a/crates/dojo-lang/src/model.rs b/crates/dojo-lang/src/model.rs index 3a42488add..558a560dd1 100644 --- a/crates/dojo-lang/src/model.rs +++ b/crates/dojo-lang/src/model.rs @@ -157,6 +157,10 @@ pub fn handle_model_struct( fn schema(self: @ContractState) -> dojo::database::introspect::Ty { dojo::database::introspect::Introspect::<$type_name$>::ty() } + + #[external(v0)] + fn ensure_abi(self: @ContractState, model: $type_name$) { + } } ", &UnorderedHashMap::from([ diff --git a/crates/dojo-lang/src/plugin_test_data/model b/crates/dojo-lang/src/plugin_test_data/model index bcaac79822..54cc06914c 100644 --- a/crates/dojo-lang/src/plugin_test_data/model +++ b/crates/dojo-lang/src/plugin_test_data/model @@ -586,6 +586,11 @@ error: Unsupported attribute. #[external(v0)] ^*************^ +error: Unsupported attribute. + --> test_src/lib.cairo[Position]:110:17 + #[external(v0)] + ^*************^ + error: Unsupported attribute. --> test_src/lib.cairo[Roles]:73:17 #[storage] @@ -616,6 +621,11 @@ error: Unsupported attribute. #[external(v0)] ^*************^ +error: Unsupported attribute. + --> test_src/lib.cairo[Roles]:106:17 + #[external(v0)] + ^*************^ + error: Unsupported attribute. --> test_src/lib.cairo[OnlyKeyModel]:72:17 #[storage] @@ -646,6 +656,11 @@ error: Unsupported attribute. #[external(v0)] ^*************^ +error: Unsupported attribute. + --> test_src/lib.cairo[OnlyKeyModel]:105:17 + #[external(v0)] + ^*************^ + error: Unsupported attribute. --> test_src/lib.cairo[Player]:81:17 #[storage] @@ -676,6 +691,11 @@ error: Unsupported attribute. #[external(v0)] ^*************^ +error: Unsupported attribute. + --> test_src/lib.cairo[Player]:114:17 + #[external(v0)] + ^*************^ + //! > expanded_cairo_code use core::serde::Serde; @@ -907,6 +927,10 @@ impl PositionIntrospect<> of dojo::database::introspect::Introspect> fn schema(self: @ContractState) -> dojo::database::introspect::Ty { dojo::database::introspect::Introspect::::ty() } + + #[external(v0)] + fn ensure_abi(self: @ContractState, model: Position) { + } } impl RolesSerde of core::serde::Serde:: { fn serialize(self: @Roles, ref output: core::array::Array) { @@ -1022,6 +1046,10 @@ impl RolesIntrospect<> of dojo::database::introspect::Introspect> { fn schema(self: @ContractState) -> dojo::database::introspect::Ty { dojo::database::introspect::Introspect::::ty() } + + #[external(v0)] + fn ensure_abi(self: @ContractState, model: Roles) { + } } impl OnlyKeyModelSerde of core::serde::Serde:: { fn serialize(self: @OnlyKeyModel, ref output: core::array::Array) { @@ -1136,6 +1164,10 @@ impl OnlyKeyModelIntrospect<> of dojo::database::introspect::Introspect dojo::database::introspect::Ty { dojo::database::introspect::Introspect::::ty() } + + #[external(v0)] + fn ensure_abi(self: @ContractState, model: OnlyKeyModel) { + } } impl PlayerCopy of core::traits::Copy::; impl PlayerDrop of core::traits::Drop::; @@ -1265,4 +1297,8 @@ impl PlayerIntrospect<> of dojo::database::introspect::Introspect> { fn schema(self: @ContractState) -> dojo::database::introspect::Ty { dojo::database::introspect::Introspect::::ty() } + + #[external(v0)] + fn ensure_abi(self: @ContractState, model: Player) { + } } diff --git a/crates/dojo-world/src/contracts/model_test.rs b/crates/dojo-world/src/contracts/model_test.rs index ce03fd3f3a..c11d2ae460 100644 --- a/crates/dojo-world/src/contracts/model_test.rs +++ b/crates/dojo-world/src/contracts/model_test.rs @@ -63,7 +63,7 @@ async fn test_model() { assert_eq!( position.class_hash(), FieldElement::from_hex_be( - "0x004cd20d231b04405a77b184c115dc60637e186504fad7f0929bd76cbd09c10b" + "0x02a7df852d6ef0af662dad741b97b423ba0fb34a1483599781ac9e7f6822bc25" ) .unwrap() ); diff --git a/examples/spawn-and-move/Scarb.lock b/examples/spawn-and-move/Scarb.lock index b61ff06a70..ae8ad6f337 100644 --- a/examples/spawn-and-move/Scarb.lock +++ b/examples/spawn-and-move/Scarb.lock @@ -10,7 +10,7 @@ dependencies = [ [[package]] name = "dojo_examples" -version = "0.4.4" +version = "0.5.0" dependencies = [ "dojo", ]