Skip to content

Commit 309bf97

Browse files
authored
Add BoundedInt terminal to grammar definition for parsing ABI (#1449)
* Add BoundedInt to grammar * Fix calculating log This reverts commit e438de5.
1 parent 2869338 commit 309bf97

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

starknet_py/abi/v2/parser_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import json
2+
from typing import cast
23

34
import pytest
45

56
from starknet_py.abi.v2 import Abi, AbiParser
7+
from starknet_py.cairo.data_types import UintType
68
from starknet_py.tests.e2e.fixtures.misc import ContractVersion, load_contract
79

810

@@ -31,3 +33,40 @@ def test_abi_parse(contract_name):
3133
parsed_abi = parser.parse()
3234

3335
assert isinstance(parsed_abi, Abi)
36+
37+
38+
def test_bounded_int_parse():
39+
abi_list = [
40+
{
41+
"type": "struct",
42+
"name": "core::circuit::u384",
43+
"members": [
44+
{
45+
"name": "limb0",
46+
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
47+
},
48+
{
49+
"name": "limb1",
50+
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
51+
},
52+
{
53+
"name": "limb2",
54+
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
55+
},
56+
{
57+
"name": "limb3",
58+
"type": "core::internal::BoundedInt::<0, 79228162514264337593543950335>",
59+
},
60+
],
61+
}
62+
]
63+
64+
parser = AbiParser(abi_list)
65+
parsed_abi = parser.parse()
66+
67+
assert isinstance(parsed_abi, Abi)
68+
69+
uint = cast(
70+
UintType, parsed_abi.defined_structures["core::circuit::u384"].types["limb0"]
71+
)
72+
assert uint.bits == 96

starknet_py/abi/v2/parser_transformer.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from math import log2
12
from typing import Any, List, Optional
23

34
import lark
@@ -25,6 +26,7 @@
2526
| type_felt
2627
| type_bytes
2728
| type_uint
29+
| type_bounded_int
2830
| type_contract_address
2931
| type_class_hash
3032
| type_storage_address
@@ -40,6 +42,7 @@
4042
type_bytes: "core::bytes_31::bytes31"
4143
type_bool: "core::bool"
4244
type_uint: "core::integer::u" INT
45+
type_bounded_int: "core::internal::BoundedInt::<" INT "," WS? INT ">"
4346
type_contract_address: "core::starknet::contract_address::ContractAddress"
4447
type_class_hash: "core::starknet::class_hash::ClassHash"
4548
type_storage_address: "core::starknet::storage_access::StorageAddress"
@@ -109,6 +112,17 @@ def type_uint(self, value: List[Token]) -> UintType:
109112
"""
110113
return UintType(int(value[0]))
111114

115+
def type_bounded_int(self, value: List[Token]) -> UintType:
116+
"""
117+
BoundedInt Uint type contains information about its ranges. They are present in the value[0] and value[2].
118+
"""
119+
if value[0] != "0":
120+
raise ValueError("BoundedInt should start from 0.")
121+
122+
bits = log2(int(value[2]) + 1)
123+
124+
return UintType(int(bits))
125+
112126
def type_unit(self, _value: List[Any]) -> UnitType:
113127
"""
114128
`()` type.

0 commit comments

Comments
 (0)