Skip to content

fix(provider): read the compiler version in the Sierra program in getContractVersion() - #1766

Merged
PhilippeR26 merged 1 commit into
starknet-io:developfrom
PhilippeR26:fix/getContractVersion
Nov 25, 2026
Merged

PhilippeR26 merged 1 commit into
starknet-io:developfrom
PhilippeR26:fix/getContractVersion

Conversation

@PhilippeR26

Copy link
Copy Markdown
Collaborator

Motivation and Resolution

provider.getContractVersion() (and Contract.getVersion(), which calls it) guessed the compiler version of a Cairo 1 class from the shape of its abi: '2' when the abi has an interface entry, '1' otherwise. A contract compiled by Cairo 2 has no interface entry when it has no external function, or when it declares its functions one by one with #[external(v0)]. For such a contract, the answer was '1': wrong. Example: __mocks__/cairo/onlyConstructor (compiled by Cairo 2.8.2).

The real version is in the class: the Cairo compiler writes it at the start of the Sierra program, and nodes return this program unchanged (the class hash is computed on it).

  • Since Cairo 1.0.0-rc0, the first 6 felts are the Sierra version, then the compiler version: 0x1, 0x6, 0x0, 0x2, 0x8, 0x2 is Sierra 1.6.0, compiled by Cairo 2.8.2.
  • The oldest format (Sierra 0.1.0, Cairo 1.0.0-alpha) starts with the text '0.1.0', and holds no compiler version: it means compiler 1.

getContractVersion now reads the compiler major version there. A header it does not know (for example a future compiler 3.x) or cannot read gives compiler: undefined. It never throws, and never guesses.

RPC version (if applicable)

N/A

Usage related changes

  • getContractVersion() / Contract.getVersion() return compiler: '2' instead of '1' for a Cairo 2 contract with no interface entry in its abi.
  • compiler is undefined for a compiler unknown to this version of starknet.js, or for a Sierra program that cannot be read. undefined was already a possible value of CompilerVersion.
  • No change of signature or type. The cairo field does not change, so Account.getCairoVersion() behaves the same.
  • cairo.getAbiContractVersion() is unchanged; its JSDoc now explains that its compiler version is a guess, and when it is wrong.

Development related changes

  • New unit test __tests__/utils/contractVersion.test.ts (no node needed): Sierra headers of the fixtures, Sierra 0.1.0, unknown and unreadable headers, option compiler: false, Cairo 0 class.
  • __tests__/defaultProvider.test.ts: getContractVersion on onlyConstructor (declared with declareIfNot, on all targets), and on a Cairo 0 contract already deployed on Testnet and Mainnet, the legacy UDC (describeIfNotDevnet: devnet cannot run Cairo 0, and no network accepts a Cairo 0 declaration any more).

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing

@PhilippeR26
PhilippeR26 merged commit 37d5953 into starknet-io:develop Nov 25, 2026
9 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 11.9.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant