Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BoundedInt terminal to grammar definition for parsing ABI #1449

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions starknet_py/abi/v2/parser_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
from typing import cast

import pytest

from starknet_py.abi.v2 import Abi, AbiParser
from starknet_py.cairo.data_types import UintType
from starknet_py.tests.e2e.fixtures.misc import ContractVersion, load_contract


Expand Down Expand Up @@ -31,3 +33,40 @@ def test_abi_parse(contract_name):
parsed_abi = parser.parse()

assert isinstance(parsed_abi, Abi)


def test_bounded_int_parse():
abi_list = [
{
"type": "struct",
"name": "core::circuit::u384",
"members": [
{
"name": "limb0",
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
},
{
"name": "limb1",
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
},
{
"name": "limb2",
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
},
{
"name": "limb3",
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
},
],
}
]

parser = AbiParser(abi_list)
parsed_abi = parser.parse()

assert isinstance(parsed_abi, Abi)

uint = cast(
UintType, parsed_abi.defined_structures["core::circuit::u384"].types["limb0"]
)
assert uint.bits == 96
14 changes: 14 additions & 0 deletions starknet_py/abi/v2/parser_transformer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import log2
from typing import Any, List, Optional

import lark
Expand Down Expand Up @@ -25,6 +26,7 @@
| type_felt
| type_bytes
| type_uint
| type_bounded_int
| type_contract_address
| type_class_hash
| type_storage_address
Expand All @@ -40,6 +42,7 @@
type_bytes: "core::bytes_31::bytes31"
type_bool: "core::bool"
type_uint: "core::integer::u" INT
type_bounded_int: "core::internal::BoundedInt::<" INT "," WS? INT ">"
type_contract_address: "core::starknet::contract_address::ContractAddress"
type_class_hash: "core::starknet::class_hash::ClassHash"
type_storage_address: "core::starknet::storage_access::StorageAddress"
Expand Down Expand Up @@ -109,6 +112,17 @@ def type_uint(self, value: List[Token]) -> UintType:
"""
return UintType(int(value[0]))

def type_bounded_int(self, value: List[Token]) -> UintType:
"""
BoundedInt Uint type contains information about its ranges. They are present in the value[0] and value[2].
"""
if value[0] != "0":
raise ValueError("BoundedInt should start from 0.")
kkawula marked this conversation as resolved.
Show resolved Hide resolved

bits = log2(int(value[2]) + 1)

return UintType(int(bits))

def type_unit(self, _value: List[Any]) -> UnitType:
"""
`()` type.
Expand Down
Loading