@@ -22,6 +22,7 @@ use crate::cairo_compile::{
2222 LibfuncArg ,
2323} ;
2424use crate :: cairo_versions:: { CairoVersion , RunnableCairo1 } ;
25+ use crate :: compile_cache;
2526
2627#[ cfg( test) ]
2728#[ path = "contracts_test.rs" ]
@@ -409,6 +410,16 @@ impl FeatureContract {
409410 get_raw_contract_class ( & self . get_compiled_path ( ) )
410411 }
411412
413+ /// Ensures that compiled artifacts are available for this contract.
414+ /// For Cairo 1 non-ERC20 contracts, this triggers on-demand compilation with caching.
415+ /// For Cairo 0 and ERC20 contracts (which use committed artifacts), this is a no-op.
416+ fn ensure_compiled ( & self ) {
417+ if matches ! ( self , Self :: ERC20 ( _) ) || self . cairo_version ( ) . is_cairo0 ( ) {
418+ return ;
419+ }
420+ compile_cache:: ensure_cairo1_compiled ( self ) ;
421+ }
422+
412423 fn get_cairo_version_bit ( & self ) -> u32 {
413424 match self . cairo_version ( ) {
414425 CairoVersion :: Cairo0 => 0 ,
@@ -514,17 +525,16 @@ impl FeatureContract {
514525
515526 pub fn get_sierra_path ( & self ) -> String {
516527 assert_ne ! ( self . cairo_version( ) , CairoVersion :: Cairo0 ) ;
528+ self . ensure_compiled ( ) ;
517529 if matches ! ( self , & Self :: ERC20 ( CairoVersion :: Cairo1 ( _) ) ) {
518530 return ERC20_SIERRA_CONTRACT_PATH . to_string ( ) ;
519531 }
520532
521- format ! (
522- "{CAIRO1_FEATURE_CONTRACTS_DIR}/{SIERRA_CONTRACTS_SUBDIR}/{}.sierra.json" ,
523- self . get_non_erc20_base_name( )
524- )
533+ compile_cache:: cached_sierra_path ( self ) . to_string_lossy ( ) . to_string ( )
525534 }
526535
527536 pub fn get_compiled_path ( & self ) -> String {
537+ self . ensure_compiled ( ) ;
528538 // ERC20 is a special case - not in the feature_contracts directory.
529539 if let Self :: ERC20 ( cairo_version) = self {
530540 match cairo_version {
@@ -535,23 +545,21 @@ impl FeatureContract {
535545 }
536546 . into ( )
537547 } else {
538- let cairo_version = self . cairo_version ( ) ;
539- format ! (
540- "resources/feature_contracts/cairo{}/{}{}.json" ,
541- match cairo_version {
542- CairoVersion :: Cairo0 => "0/compiled" ,
543- CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) => "1/compiled" ,
544- #[ cfg( feature = "cairo_native" ) ]
545- CairoVersion :: Cairo1 ( RunnableCairo1 :: Native ) => "1/sierra" ,
546- } ,
547- self . get_non_erc20_base_name( ) ,
548- match cairo_version {
549- CairoVersion :: Cairo0 => "_compiled" ,
550- CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) => ".casm" ,
551- #[ cfg( feature = "cairo_native" ) ]
552- CairoVersion :: Cairo1 ( RunnableCairo1 :: Native ) => ".sierra" ,
548+ match self . cairo_version ( ) {
549+ CairoVersion :: Cairo0 => {
550+ format ! (
551+ "resources/feature_contracts/cairo0/compiled/{}_compiled.json" ,
552+ self . get_non_erc20_base_name( )
553+ )
553554 }
554- )
555+ CairoVersion :: Cairo1 ( RunnableCairo1 :: Casm ) => {
556+ compile_cache:: cached_compiled_path ( self ) . to_string_lossy ( ) . to_string ( )
557+ }
558+ #[ cfg( feature = "cairo_native" ) ]
559+ CairoVersion :: Cairo1 ( RunnableCairo1 :: Native ) => {
560+ compile_cache:: cached_sierra_path ( self ) . to_string_lossy ( ) . to_string ( )
561+ }
562+ }
555563 }
556564 }
557565
@@ -697,7 +705,11 @@ impl FeatureContract {
697705}
698706
699707pub fn get_raw_contract_class ( contract_path : & str ) -> String {
700- let path: PathBuf = [ compile_time_cargo_manifest_dir ! ( ) , contract_path] . iter ( ) . collect ( ) ;
708+ let path: PathBuf = if std:: path:: Path :: new ( contract_path) . is_absolute ( ) {
709+ PathBuf :: from ( contract_path)
710+ } else {
711+ [ compile_time_cargo_manifest_dir ! ( ) , contract_path] . iter ( ) . collect ( )
712+ } ;
701713 fs:: read_to_string ( & path)
702714 . unwrap_or_else ( |e| panic ! ( "Failed to read contract from {path:?}: {e}" ) )
703715}
0 commit comments