Skip to content

Commit 9264491

Browse files
Support fixed size array type (#1607)
* Support fixed size array type * Rename variables * Update migration guide * Fix variable name * Format Cairo files * Fix tests
1 parent 772b160 commit 9264491

22 files changed

+222
-72
lines changed

docs/migration_guide.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Migration guide
22
===============
33

4+
**********************
5+
0.26.3 Migration guide
6+
**********************
7+
8+
0.26.3 Bugfixes
9+
---------------
10+
11+
1. ABI parser supports now fixed size arrays.
12+
413
**********************
514
0.26.2 Migration guide
615
**********************

starknet_py/abi/v1/parser_transformer.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, List, Optional
1+
from typing import Any, List, Optional, Tuple
22

33
import lark
44
from lark import Token, Transformer
@@ -8,6 +8,7 @@
88
BoolType,
99
CairoType,
1010
FeltType,
11+
FixedSizeArrayType,
1112
OptionType,
1213
TupleType,
1314
TypeIdentifier,
@@ -27,6 +28,7 @@
2728
| type_storage_address
2829
| type_option
2930
| type_array
31+
| type_fixed_size_array
3032
| type_span
3133
| tuple
3234
| type_identifier
@@ -41,6 +43,7 @@
4143
type_storage_address: "core::starknet::storage_access::StorageAddress"
4244
type_option: "core::option::Option::<" (type | type_identifier) ">"
4345
type_array: "core::array::Array::<" (type | type_identifier) ">"
46+
type_fixed_size_array: "[" (type | type_identifier) ";" INT "]"
4447
type_span: "core::array::Span::<" (type | type_identifier) ">"
4548
4649
tuple: "(" type? ("," type?)* ")"
@@ -115,6 +118,16 @@ def type_array(self, value: List[CairoType]) -> ArrayType:
115118
"""
116119
return ArrayType(value[0])
117120

121+
def type_fixed_size_array(
122+
self, value: Tuple[CairoType, Token]
123+
) -> FixedSizeArrayType:
124+
"""
125+
Fixed-size array contains values of type under `value[0]`.
126+
"""
127+
cairo_type, size_token = value
128+
size = int(size_token)
129+
return FixedSizeArrayType(cairo_type, size)
130+
118131
def type_span(self, value: List[CairoType]) -> ArrayType:
119132
"""
120133
Span contains values of type under `value[0]`.

starknet_py/abi/v2/parser_transformer.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from math import log2
2-
from typing import Any, List, Optional, Union
2+
from typing import Any, List, Optional, Tuple, Union
33

44
import lark
55
from lark import Token, Transformer
@@ -9,6 +9,7 @@
99
BoolType,
1010
CairoType,
1111
FeltType,
12+
FixedSizeArrayType,
1213
NonZeroType,
1314
OptionType,
1415
TupleType,
@@ -34,6 +35,7 @@
3435
| type_option
3536
| type_non_zero
3637
| type_array
38+
| type_fixed_size_array
3739
| type_span
3840
| tuple
3941
| type_identifier
@@ -50,6 +52,7 @@
5052
type_storage_address: "core::starknet::storage_access::StorageAddress"
5153
type_option: "core::option::Option::<" (type | type_identifier) ">"
5254
type_array: "core::array::Array::<" (type | type_identifier) ">"
55+
type_fixed_size_array: "[" (type | type_identifier) ";" INT "]"
5356
type_span: "core::array::Span::<" (type | type_identifier) ">"
5457
type_non_zero: "core::zeroable::NonZero::<" (type | type_identifier) ">"
5558
@@ -145,6 +148,17 @@ def type_array(self, value: List[CairoType]) -> ArrayType:
145148
"""
146149
return ArrayType(value[0])
147150

151+
def type_fixed_size_array(
152+
self, value: Tuple[CairoType, Token]
153+
) -> FixedSizeArrayType:
154+
"""
155+
Fixed-size array contains values of type under `value[0]`.
156+
"""
157+
cairo_type, size_token = value
158+
size = int(size_token)
159+
160+
return FixedSizeArrayType(cairo_type, size)
161+
148162
def type_span(self, value: List[CairoType]) -> ArrayType:
149163
"""
150164
Span contains values of type under `value[0]`.

starknet_py/cairo/data_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class ArrayType(CairoType):
5353
inner_type: CairoType #: Type of element inside array.
5454

5555

56+
@dataclass
57+
class FixedSizeArrayType(CairoType):
58+
"""
59+
Type representation of Cairo fixed-size arrays.
60+
"""
61+
62+
inner_type: CairoType #: Type of element inside array.
63+
length: int #: Length of the array.
64+
65+
5666
@dataclass
5767
class StructType(CairoType):
5868
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
scarb 2.6.3
1+
scarb 2.10.0

starknet_py/tests/e2e/mock/contracts_v2/Scarb.lock

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,107 @@ dependencies = [
1010

1111
[[package]]
1212
name = "openzeppelin"
13-
version = "0.10.0"
14-
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.10.0#d77082732daab2690ba50742ea41080eb23299d3"
13+
version = "1.0.0"
14+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
15+
dependencies = [
16+
"openzeppelin_access",
17+
"openzeppelin_account",
18+
"openzeppelin_finance",
19+
"openzeppelin_governance",
20+
"openzeppelin_introspection",
21+
"openzeppelin_merkle_tree",
22+
"openzeppelin_presets",
23+
"openzeppelin_security",
24+
"openzeppelin_token",
25+
"openzeppelin_upgrades",
26+
"openzeppelin_utils",
27+
]
28+
29+
[[package]]
30+
name = "openzeppelin_access"
31+
version = "1.0.0"
32+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
33+
dependencies = [
34+
"openzeppelin_introspection",
35+
"openzeppelin_utils",
36+
]
37+
38+
[[package]]
39+
name = "openzeppelin_account"
40+
version = "1.0.0"
41+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
42+
dependencies = [
43+
"openzeppelin_introspection",
44+
"openzeppelin_utils",
45+
]
46+
47+
[[package]]
48+
name = "openzeppelin_finance"
49+
version = "1.0.0"
50+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
51+
dependencies = [
52+
"openzeppelin_access",
53+
"openzeppelin_token",
54+
]
55+
56+
[[package]]
57+
name = "openzeppelin_governance"
58+
version = "1.0.0"
59+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
60+
dependencies = [
61+
"openzeppelin_access",
62+
"openzeppelin_account",
63+
"openzeppelin_introspection",
64+
"openzeppelin_token",
65+
"openzeppelin_utils",
66+
]
67+
68+
[[package]]
69+
name = "openzeppelin_introspection"
70+
version = "1.0.0"
71+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
72+
73+
[[package]]
74+
name = "openzeppelin_merkle_tree"
75+
version = "1.0.0"
76+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
77+
78+
[[package]]
79+
name = "openzeppelin_presets"
80+
version = "1.0.0"
81+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
82+
dependencies = [
83+
"openzeppelin_access",
84+
"openzeppelin_account",
85+
"openzeppelin_finance",
86+
"openzeppelin_introspection",
87+
"openzeppelin_token",
88+
"openzeppelin_upgrades",
89+
"openzeppelin_utils",
90+
]
91+
92+
[[package]]
93+
name = "openzeppelin_security"
94+
version = "1.0.0"
95+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
96+
97+
[[package]]
98+
name = "openzeppelin_token"
99+
version = "1.0.0"
100+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
101+
dependencies = [
102+
"openzeppelin_access",
103+
"openzeppelin_account",
104+
"openzeppelin_introspection",
105+
"openzeppelin_utils",
106+
]
107+
108+
[[package]]
109+
name = "openzeppelin_upgrades"
110+
version = "1.0.0"
111+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"
112+
113+
[[package]]
114+
name = "openzeppelin_utils"
115+
version = "1.0.0"
116+
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v1.0.0#755ce1adbf8d89ed23fbb8daa328fcbeab7c6424"

starknet_py/tests/e2e/mock/contracts_v2/Scarb.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name = "contracts_v2"
33
version = "0.1.0"
44

55
[dependencies]
6-
starknet = "2.6.3"
7-
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v0.10.0" }
6+
starknet = "2.10.0"
7+
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", tag="v1.0.0" }
88

99
[[target.starknet-contract]]
1010
casm = true

starknet_py/tests/e2e/mock/contracts_v2/src/abi_types.cairo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ struct ExampleStruct {
1919
field_d: (),
2020
field_e: NonZero<felt252>,
2121
field_f: NonZero<u8>,
22+
field_g: [u64; 5],
2223
}
2324

2425
#[starknet::interface]
2526
trait IAbiTest<TContractState> {
2627
fn example_view_function(self: @TContractState) -> ExampleEnum;
2728
fn example_external_function(
28-
ref self: TContractState, recipient: ContractAddress, amount: u256
29+
ref self: TContractState, recipient: ContractAddress, amount: u256,
2930
) -> ExampleStruct;
3031
}
3132

@@ -62,7 +63,7 @@ mod AbiTypes {
6263
ExampleEnum::variant_a(100)
6364
}
6465
fn example_external_function(
65-
ref self: ContractState, recipient: ContractAddress, amount: u256
66+
ref self: ContractState, recipient: ContractAddress, amount: u256,
6667
) -> ExampleStruct {
6768
ExampleStruct {
6869
field_a: 200,
@@ -71,6 +72,7 @@ mod AbiTypes {
7172
field_d: (),
7273
field_e: felt_to_nonzero(100),
7374
field_f: u8_to_nonzero(100),
75+
field_g: [1, 2, 3, 4, 5],
7476
}
7577
}
7678
}

starknet_py/tests/e2e/mock/contracts_v2/src/account.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod Account {
1717
#[substorage(v0)]
1818
account: AccountComponent::Storage,
1919
#[substorage(v0)]
20-
src5: SRC5Component::Storage
20+
src5: SRC5Component::Storage,
2121
}
2222

2323
#[event]
@@ -26,7 +26,7 @@ mod Account {
2626
#[flat]
2727
AccountEvent: AccountComponent::Event,
2828
#[flat]
29-
SRC5Event: SRC5Component::Event
29+
SRC5Event: SRC5Component::Event,
3030
}
3131

3232
#[constructor]

starknet_py/tests/e2e/mock/contracts_v2/src/account_copy_1.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod AccountCopy1 {
1717
#[substorage(v0)]
1818
account: AccountComponent::Storage,
1919
#[substorage(v0)]
20-
src5: SRC5Component::Storage
20+
src5: SRC5Component::Storage,
2121
}
2222

2323
#[event]
@@ -26,7 +26,7 @@ mod AccountCopy1 {
2626
#[flat]
2727
AccountEvent: AccountComponent::Event,
2828
#[flat]
29-
SRC5Event: SRC5Component::Event
29+
SRC5Event: SRC5Component::Event,
3030
}
3131

3232
#[constructor]

0 commit comments

Comments
 (0)