Skip to main content

Generate type hints for Cairo lang

Project description

Cairo type hints

Add type hints to your Cairo-lang files and feed the generated json files to Stark-Dot-Net code generators.

Install

pip install cairo-type-hints

Example usage:

python cairo-type-hints/console.py \
    -i ./src/my-awesome-contract.cairo \
    -o ./artifacts/hints/test-hints.json

Adding hints to structs

A type hint is a comment with a colon, a space and then a type. Valid types are int, string, and address. For example, #: int.

Type hints are added inline per member of the struct. For example:

struct UserMeta:
    member index : felt #: int
    member hash : felt
    member position : Position
end

Members without a type hint will default to string for felt and the struct type for structs.

The above Cairo-lang example outputs:

[
   {
      "name":"UserMeta",
      "members":[
         {
            "type":"int",
            "name":"index"
         },
         {
            "type":"string",
            "name":"hash"
         },
         {
            "type":"Position",
            "name":"position"
         }
      ],
      "type":"struct"
   }
]

Adding hints to functions

Function hint comments mimic Cairo-lang function type declarations and are placed as the first statement in a function body. For example:

#: (index : int, position : Position) -> (user : User)
@view
func get_user_by_index {
    syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
    range_check_ptr
    } (index : felt, position : Position) -> (user : User):
    #: (index : int, position : Position) -> (user : User)
    let (hash) = users_index_to_hash.read(index)
    let (user) = get_user_by_hash(hash)
    return (user = user)
end

The above Cairo-lang example outputs:

[
    {
      "name":"get_user_by_index",
      "inputs":[
         {
            "type":"int",
            "name":"index"
         },
         {
            "type":"Position",
            "name":"position"
         }
      ],
      "outputs":[
         {
            "type":"User",
            "name":"user"
         }
      ],
      "type":"function"
   }
]

Development

Install and switch to python 3.9.x

$ pyenv install 3.9.9
$ pyenv local 3.9.9

Create a virtual environment and activate it

$ python -m venv env
$ source venv/bin/activate

Do some awesome work!

Build and publish the package

$ python setup.py bdist_wheel sdist
$ twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cairo-type-hints-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cairo_type_hints-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file cairo-type-hints-0.1.0.tar.gz.

File metadata

  • Download URL: cairo-type-hints-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.9

File hashes

Hashes for cairo-type-hints-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a94e0a86407473cda4d8523c60d039306f92c5095ec10c69bac58a0980862de
MD5 b913f4b81817d0a9fc0190dc40f713d5
BLAKE2b-256 cba3d4cbbd53db6ca7c4eb60c023f9333b86e3bddd7cd3329ca6ff0721710b08

See more details on using hashes here.

File details

Details for the file cairo_type_hints-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cairo_type_hints-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8462527071f91df79371f97e720b10e3d1dff2a4b7a0e4b7503240018917a450
MD5 2e85e57789b9c0caa08e31ca06fd2793
BLAKE2b-256 f32c0a3704cb72ad7b17252ab867ae35d9bf79678a4e26722ba5856bf38dd65b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page