@@ -6,6 +6,7 @@ use strum::IntoEnumIterator;
6
6
use strum_macros:: EnumIter ;
7
7
8
8
use crate :: execution:: contract_class:: { ContractClass , ContractClassV0 , ContractClassV1 } ;
9
+ use crate :: test_utils:: cairo_compile:: { cairo0_compile, cairo1_compile} ;
9
10
use crate :: test_utils:: { get_raw_contract_class, CairoVersion } ;
10
11
11
12
// This file contains featured contracts, used for tests. Use the function 'test_state' in
@@ -51,8 +52,10 @@ const SECURITY_TEST_CONTRACT_NAME: &str = "security_tests_contract";
51
52
const TEST_CONTRACT_NAME : & str = "test_contract" ;
52
53
53
54
// ERC20 contract is in a unique location.
55
+ const ERC20_BASE_NAME : & str = "ERC20" ;
54
56
const ERC20_CONTRACT_PATH : & str =
55
57
"./ERC20_without_some_syscalls/ERC20/erc20_contract_without_some_syscalls_compiled.json" ;
58
+ const ERC20_CONTRACT_SOURCE_PATH : & str = "./ERC20_without_some_syscalls/ERC20/ERC20.cairo" ;
56
59
57
60
/// Enum representing all feature contracts.
58
61
/// The contracts that are implemented in both Cairo versions include a version field.
@@ -69,7 +72,7 @@ pub enum FeatureContract {
69
72
}
70
73
71
74
impl FeatureContract {
72
- fn cairo_version ( & self ) -> CairoVersion {
75
+ pub fn cairo_version ( & self ) -> CairoVersion {
73
76
match self {
74
77
Self :: AccountWithLongValidate ( version)
75
78
| Self :: AccountWithoutValidations ( version)
@@ -114,18 +117,40 @@ impl FeatureContract {
114
117
}
115
118
}
116
119
117
- pub fn get_compiled_path ( & self ) -> String {
118
- let cairo_version = self . cairo_version ( ) ;
119
- let contract_name = match self {
120
+ fn contract_base_name ( & self ) -> & str {
121
+ match self {
120
122
Self :: AccountWithLongValidate ( _) => ACCOUNT_LONG_VALIDATE_NAME ,
121
123
Self :: AccountWithoutValidations ( _) => ACCOUNT_WITHOUT_VALIDATIONS_NAME ,
122
124
Self :: Empty ( _) => EMPTY_CONTRACT_NAME ,
123
125
Self :: FaultyAccount ( _) => FAULTY_ACCOUNT_NAME ,
124
126
Self :: LegacyTestContract => LEGACY_CONTRACT_NAME ,
125
127
Self :: SecurityTests => SECURITY_TEST_CONTRACT_NAME ,
126
128
Self :: TestContract ( _) => TEST_CONTRACT_NAME ,
129
+ Self :: ERC20 => ERC20_BASE_NAME ,
130
+ }
131
+ }
132
+
133
+ pub fn get_source_path ( & self ) -> String {
134
+ match self {
135
+ // Special case: ERC20 contract in a different location.
136
+ Self :: ERC20 => ERC20_CONTRACT_SOURCE_PATH . into ( ) ,
137
+ _not_erc20 => format ! (
138
+ "feature_contracts/cairo{}/{}.cairo" ,
139
+ match self . cairo_version( ) {
140
+ CairoVersion :: Cairo0 => "0" ,
141
+ CairoVersion :: Cairo1 => "1" ,
142
+ } ,
143
+ self . contract_base_name( )
144
+ ) ,
145
+ }
146
+ }
147
+
148
+ pub fn get_compiled_path ( & self ) -> String {
149
+ let cairo_version = self . cairo_version ( ) ;
150
+ let contract_name = match self {
127
151
// ERC20 is a special case - not in the feature_contracts directory.
128
152
Self :: ERC20 => return ERC20_CONTRACT_PATH . into ( ) ,
153
+ _not_erc20 => self . contract_base_name ( ) ,
129
154
} ;
130
155
format ! (
131
156
"feature_contracts/cairo{}/compiled/{}{}.json" ,
@@ -141,6 +166,31 @@ impl FeatureContract {
141
166
)
142
167
}
143
168
169
+ /// Compiles the feature contract and returns the compiled contract as a byte vector.
170
+ /// Panics if the contract is ERC20, as ERC20 contract recompilation is not supported.
171
+ pub fn compile ( & self ) -> Vec < u8 > {
172
+ if matches ! ( self , Self :: ERC20 ) {
173
+ panic ! ( "ERC20 contract recompilation not supported." ) ;
174
+ }
175
+ match self . cairo_version ( ) {
176
+ CairoVersion :: Cairo0 => {
177
+ let extra_arg: Option < String > = match self {
178
+ // Account contracts require the account_contract flag.
179
+ FeatureContract :: AccountWithLongValidate ( _)
180
+ | FeatureContract :: AccountWithoutValidations ( _)
181
+ | FeatureContract :: FaultyAccount ( _) => Some ( "--account_contract" . into ( ) ) ,
182
+ FeatureContract :: SecurityTests => Some ( "--disable_hint_validation" . into ( ) ) ,
183
+ FeatureContract :: Empty ( _)
184
+ | FeatureContract :: TestContract ( _)
185
+ | FeatureContract :: LegacyTestContract => None ,
186
+ FeatureContract :: ERC20 => unreachable ! ( ) ,
187
+ } ;
188
+ cairo0_compile ( self . get_source_path ( ) , extra_arg, false )
189
+ }
190
+ CairoVersion :: Cairo1 => cairo1_compile ( self . get_source_path ( ) ) ,
191
+ }
192
+ }
193
+
144
194
pub fn set_cairo_version ( & mut self , version : CairoVersion ) {
145
195
match self {
146
196
Self :: AccountWithLongValidate ( v)
@@ -203,4 +253,9 @@ impl FeatureContract {
203
253
}
204
254
} )
205
255
}
256
+
257
+ pub fn all_feature_contracts ( ) -> impl Iterator < Item = Self > {
258
+ // ERC20 is a special case - not in the feature_contracts directory.
259
+ Self :: all_contracts ( ) . filter ( |contract| !matches ! ( contract, Self :: ERC20 ) )
260
+ }
206
261
}
0 commit comments