Skip to content

Add block number for external contract events - #3224

Merged
glihm merged 16 commits into
dev-1.6.0from
add_block_number_for_external_contracts
Jun 3, 2025
Merged

Add block number for external contract events#3224
glihm merged 16 commits into
dev-1.6.0from
add_block_number_for_external_contracts

Conversation

@remybar

@remybar remybar commented May 21, 2025

Copy link
Copy Markdown
Contributor

Description

To ease external contract indexing at Torii level, ExternalContractRegistered and ExternalContractUpgraded events must contain the block number to use for indexing.

At sozo level, there are 2 kinds of external contracts: the ones directly deployed and so managed by sozo (called sozo-managed), and the ones deployed outside of sozo (called self-managed).

For sozo-managed external contracts:

  • by default, the block number of the deploy transaction is used to register the contract into the world, but it can be overriden in dojo_dev.toml. A ExternalContractRegistered event is emitted with this block number.
  • If the contract changes (its class_hash changes), it will be upgraded but, as its address stays the same, the contract registration at world level does not change.
  • If the configured block_number in dojo_dev.toml changes, the contract registration at world level will be updated with the new block number (i.e a ExternalContractUpgraded event will be emitted).

For self-managed external contracts:

  • if the block number is not provided in the dojo_dev.toml, 0 is used by default.
  • the first time the contract is detected in the dojo_dev.toml file, it is registered into the world with the configured block number (0 by default),
  • if the configured block number changes, the contract registration at world level is updated with the new block number (i.e a ExternalContractUpgraded event will be emitted).

Tests

  • Yes
  • No, because they aren't needed
  • No, because I need help

Added to documentation?

  • README.md
  • Dojo Book
  • [] No documentation needed

Checklist

  • I've formatted my code (scripts/rust_fmt.sh, scripts/cairo_fmt.sh)
  • I've linted my code (scripts/clippy.sh, scripts/docs.sh)
  • I've commented my code
  • I've requested a review after addressing the comments

@remybar
remybar force-pushed the add_block_number_for_external_contracts branch from 8ce8087 to 86e2529 Compare May 28, 2025 06:20
contract_selector,
class_hash,
contract_address,
block_number: get_block_number(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of the block number is that when Torii detects this event, it knows at which block this contract must be indexed from.

In the current implementation, we are actually using the current block, which may not correspond to the block the user wants to setup the indexing from.

This must be an argument of the register external contract function. In case that the external contract is not a token, which may not be indexed by Torii, the use can simply use 0.
Otherwise, the user can provide the block at which the token has been deployed (which is not the get_block_number()).

Does it make sense to you too?

cc @MartianGreed this is how I understood it at least.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That totally makes sense !

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum ok, but external contracts are deployed by sozo so how the user knows/provides the block number to use for indexing to be able to pass it to the register_external_contract entry point ? Because registering is done just after having deployed the contract.
It looks like more an indexer configuration (so at Torii level) than an information that should be provided when registering the external contract 🤔
But if you still think that this information has to be present in the ExternalContractRegistered event, we can provide this block number through the dojo_dev.toml file for example, and then pass it to the register entry point.
Tell me WDYT about that 😉

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can know the block when it's a token that already exists in the network for instance. Let's say $LORD for instance, one may just want to register this token in the world to ease the dns usage to refer to this token instead of hardcoding addresses.

It is definitely an indexer configuration. However the goal here is to give the whole context to the automatic indexer Torii, to avoid having to edit configurations for each new token.
In a marketplace example, having to restart Torii by adding a new token address would make it unmanageable.

With this strategy of exposing the block number during the registration, then a marketplace could have Torii automatically adding some tokens to the indexing list without restart or human intervention in the config.

But if you still think that this information has to be present in the ExternalContractRegistered event, we can provide this block number through the dojo_dev.toml file for example, and then pass it to the register entry point.

Yes this sounds the right approach. I guess that when Sozo is deploying the external contract, then the block could be automatically taken.
However, we should also be able to pass the block for contracts not deployed by Sozo.

Not sure the migration has this info at the moment, we may need to add this context for Sozo when deployments are done.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this detailed feedback 👍

I've updated the PR (description + code) accordingly but I still have to test all the corner cases.

@remybar
remybar force-pushed the add_block_number_for_external_contracts branch from 7dd25b1 to 4225988 Compare June 1, 2025 13:43

@glihm glihm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @remybar for the work here and the addition of this block number parameter. 👍

Was wondering if the block number should be an option instead of u64. Since in some cases, the registration of the contract may not be coupled to indexing.

But we may stay with that for now, and we will see with usage in the marketplace. 🫡

@glihm
glihm merged commit a65b063 into dev-1.6.0 Jun 3, 2025
@glihm
glihm deleted the add_block_number_for_external_contracts branch June 3, 2025 03:29
remybar added a commit that referenced this pull request Jun 13, 2025
* add block_number to ExternalContractRegistered/ExternalContractUpgraded events

* remove useless println

* restore external_contracts tests + update with block_number

* update test artifacts

* fix clippy

* rework: handle block_number

* fix world.dns for external contract + update tests

* update sozo events with missing events

* add a self-managed external contract called from spawn-and-move

* update manifest_dev.json

* use dns_address

* fix fmt + abigen

* update test artifacts

* fix fmt

* update starkli path

* update policies
glihm added a commit that referenced this pull request Jul 14, 2025
…3266)

* feat: add external contract registering in the world (#3195)

* Use the world as external contract reference and manage upgrades with sozo

* update spawn-and-move with new external contract features

* update test artifacts + world_address in dojo_dev.toml

* export entrypoints + fix tests + remove TODO RBA

* restore test_metadata_updated_event test

* fix tests

* rabbit improvements

* fix test

* feat: add proc macros (#3212)

* WIP

* add macros + core-tests crates + update them from 1.6.0

* update scripts/cairo_fmt.sh + fix fmt

* update CI + Scarb.toml files

* fix fmt

* change from 2.11.2 to 2.11.4

* fix scarb issue + fmt

* one testing library for each test runner (cairo, snfoundry) + update examples

* fix fmt

* update comment

* chore: apply patches to sync exact same commits than scarb

* fix(ci): update rust-toolchain version

* fix(ci): bump clippy to use nightly compatible with 1.86

* fix: clippy and fmt

* update spawn-and-move to use dojo-snf-test

* update world address

---------

Co-authored-by: glihm <dev@glihm.net>

* Scarb crate removing (#3223)

* introduce scarb_interop crate

* handle missing scarb

* introduce metadata instead of workspace to manage dojo related paths and config

* add stats back and ensure profile is propagated until build not only metadata

* cleanup and add error message if SCARB is not set correctly

* add features + packages to build command

* update scarb build/test command

* refactor packages/features

* fix utils.rs + enable auth command

* fix rust fmt

* re-enable sozo commands

* re-enable the last sozo commands + new version command

* fix issue with conflicts_with_all

* check if manifest_path does not exist

* set run() functions as async instead of using tokio

* update init command management

* refactor metadata loading + dead code cleaning

* add build_simple_dev() function to be used to easily build spawn-and-move for tests/benches

* update tests + fmt + clippy

* remove dojo/lang

* remove useless sozo files

* tiny change

* fix CI

* use scarb nightly in CI

* fix manifest_path/manifest_dir issue

* fix fmt

* update snfoundry version + update tests accordingly + add cairo-profiler to .tool-versions

---------

Co-authored-by: glihm <dev@glihm.net>

* feat: add DojoStore trait to handle storage serialization (#3219)

* add DojoStore management

* fix rust fmt after abigen

* fix ModelReader

* fix fmt

* set DojoStore functions as inline(always)

* update test artifacts

* fix fmt

* remove dojo-core test from CI as all tests are now in core-tests

* feat: add block number for external contract events (#3224)

* add block_number to ExternalContractRegistered/ExternalContractUpgraded events

* remove useless println

* restore external_contracts tests + update with block_number

* update test artifacts

* fix clippy

* rework: handle block_number

* fix world.dns for external contract + update tests

* update sozo events with missing events

* add a self-managed external contract called from spawn-and-move

* update manifest_dev.json

* use dns_address

* fix fmt + abigen

* update test artifacts

* fix fmt

* update starkli path

* update policies

* rebuild test db

* fix: restore resource order to avoid storage conflict (#3238)

* Resource enum variant order should not change as it is used in the world storage

* fix fmt

* fix world address and test db

* fix fmt

---------

Co-authored-by: glihm <dev@glihm.net>

* fix(lang): remove warning with unit type (#3255)

fix warning with unit type for enumerations serialization generated code.

* dev: merge main in dev 1.6.0 (#3263)

* Fix broken getting started link in README (#3235)

* chore(versions): add torii 1.5.6

* Update DEVELOPMENT.md (#3236)

* Update DEVELOPMENT.md with new instructions

* Update scripts/rebuild_test_artifacts.sh with Rabbit suggestion

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Respond to review comments

* Respond to review comments II

* Respond to review comments III

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* feat: update rpc spec to 0.8 (#3179)

* release(prepare): v1.6.0-alpha.0 (#3241)

Prepare release: v1.6.0-alpha.0

Co-authored-by: glihm <glihm@users.noreply.github.com>

* chore(versions): bump katana to 1.6.0-alpha.0

* fix(dojoup): remove new line in the source cmd (#3244)

fix: fixed new line in the source cmd

* chore: edited the build badge and its link (#3221)

Co-authored-by: Ammar Arif <evergreenkary@gmail.com>

* chore(devcontainer): update image: v1.6.0-alpha.0 (#3242)

Update devcontainer image: v1.6.0-alpha.0

Co-authored-by: glihm <glihm@users.noreply.github.com>

* chore: add katana 1.5.4 (#3245)

* chore: add the missing backticks in the comments (#3243)

Signed-off-by: one230six <723682061@qq.com>

* fix(bindgen): use world namespace for imported models in TypeScript S… (#3249)

* fix(bindgen): filter out Value models (#3248)

* fix(bindgen): fix ts bytearray type mapping (#3247)

* chore: update katana versions (#3253)

add katana versions

* sozo(unrealengine): handle UE5.6 and Dojo 1.5 (#3252)

* fix(sozo): assert caller permission with match (#3254)

* handle call_contract_syscall() result for better panic trace

* avoid using 0 as caller address

* remove temporarly the RPC version check.

Currently, Katana uses the new RPC types, without
bumping the spec version. To ensure we can still use
sozo with sepolia/mainnet and Katana, Sozo will not check
the RPC version for now

* update test dbs

---------

Co-authored-by: glihm <dev@glihm.net>

* feat(sozo): create standalone bindgen command (#3246)

* feat: create standalone bindgen command

* cleanup unused inputs

* remove dbg

* add meaningful error if project is not built

---------

Co-authored-by: glihm <dev@glihm.net>

* feat(sozo): add MCP sever (#3256)

* feat(sozo): add mvp for mcp server

* refacto: restructure MCP server

* evaluate changes using official rust sdk

* feat(mcp): add stdio support

* refacto with rmcp crate

* wip

* bump rmcp

* add testing support

* wip

* refacto

* cleanup

* add instructions

* add instructions

* wip test

* refacto tests

* add debugging

* ignore test with katana at the moment

* fix typos and sozo path

* refacto uri parsing

* remove dbg

* disable test that should be run with katana

* fix test, windows fails because of new reqwest version

* refactor(types): schema json sql value (#3257)

* refactor(types): schema json sql value

* remove excess comma

* fix clippy

---------

Co-authored-by: glihm <dev@glihm.net>

* chore: bump cairo packages to 1.6.0-alpha.0

* fix(mcp): ensure test is using latest version

* split scarb metadata ext in a different crate for dependencies

* wip: controller issue

* wip

* wip: resolve conflicts and update deps

* cleanup

* lint

* wip

* wip tests

* fix all tests

* rebuild test db

* fix: run linter

* fix slot dep with github commit

* fix clippy

* fix inspect by adding missing json format

* fix mcp formatting

* fix stdio_test

* use sozo from path

---------

Signed-off-by: one230six <723682061@qq.com>
Co-authored-by: Ritik <ritikverma0050@gmail.com>
Co-authored-by: Daniel Kronovet <kronovet@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Ammar Arif <evergreenkary@gmail.com>
Co-authored-by: Tarrence van As <tarrencev@users.noreply.github.com>
Co-authored-by: glihm <glihm@users.noreply.github.com>
Co-authored-by: Benjamin <158306087+bengineer42@users.noreply.github.com>
Co-authored-by: braveocheretovych <braveocheretovych@gmail.com>
Co-authored-by: one230six <163239332+one230six@users.noreply.github.com>
Co-authored-by: Brother MartianGreed <valentin@pupucecorp.com>
Co-authored-by: Corentin Cailleaud <corentin.cailleaud@caillef.com>
Co-authored-by: Rémy Baranx <remy.baranx@gmail.com>
Co-authored-by: Larko <59736843+Larkooo@users.noreply.github.com>

* feat(core): use metaprogramming for tuple and fixed size array Introspect and DojoStore (#3260)

* reuse Starkware metaprogramming stuff to manage tuples

* fix fmt + clippy + artifacts

* use metaprogramming to handle tuple introspect

* fix fmt + clippy + artifacts

* add fixed size array support

* update dojo-world

* update abigen + artifacts after rebase

* add a sum_sizes function

* update artifacts

* tooling: add cairo-bench tool + bench tests (#3240)

* add cairo-bench tool

* add bench tests

* first optimisation batch

* fix fmt

* add license info

* update artifacts after rebase

* fix cairo-bench tests

* update artifacts

* update artifacts

* update scarb lock

---------

Co-authored-by: glihm <dev@glihm.net>

* fix(cairo-bench): use write and threshold + refactor fixed size array (#3267)

* remove update-ref-test-list argument

* add threshold parameter to cairo-bench (set to 3% by default)

* max_fee_raw and fee_estimate_multiplier don't exist anymore

* update sozo model commands for fixed size arrays

* fix fmt + clippy

* rename --update-ref to --write

* fix some tests

* fix fmt

* fix(ci): update katana to 1.6.2 for compatible db

---------

Signed-off-by: one230six <723682061@qq.com>
Co-authored-by: Rémy Baranx <remy.baranx@gmail.com>
Co-authored-by: Ritik <ritikverma0050@gmail.com>
Co-authored-by: Daniel Kronovet <kronovet@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Ammar Arif <evergreenkary@gmail.com>
Co-authored-by: Tarrence van As <tarrencev@users.noreply.github.com>
Co-authored-by: glihm <glihm@users.noreply.github.com>
Co-authored-by: Benjamin <158306087+bengineer42@users.noreply.github.com>
Co-authored-by: braveocheretovych <braveocheretovych@gmail.com>
Co-authored-by: one230six <163239332+one230six@users.noreply.github.com>
Co-authored-by: Brother MartianGreed <valentin@pupucecorp.com>
Co-authored-by: Corentin Cailleaud <corentin.cailleaud@caillef.com>
Co-authored-by: Larko <59736843+Larkooo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants