Skip to content

Commit

Permalink
Add BoundedInt terminal to grammar definition for parsing ABI (#1449)
Browse files Browse the repository at this point in the history
* Add BoundedInt to grammar

* Fix calculating log

This reverts commit e438de5.
  • Loading branch information
kkawula authored Aug 20, 2024
1 parent 2869338 commit 309bf97
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
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.")

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

return UintType(int(bits))

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

0 comments on commit 309bf97

Please sign in to comment.