@@ -635,6 +635,7 @@ async function processInitForm(
635
635
}
636
636
637
637
// If ABI is not provided, try to fetch it from Etherscan API
638
+ let implAddress : string | undefined = undefined ;
638
639
if ( protocolInstance . hasABIs ( ) && ! initAbi ) {
639
640
abiFromApi = await retryWithPrompt ( ( ) =>
640
641
withSpinner (
@@ -647,15 +648,50 @@ async function processInitForm(
647
648
initDebugger . extend ( 'processInitForm' ) ( "abiFromEtherscan len: '%s'" , abiFromApi ?. name ) ;
648
649
} else {
649
650
abiFromApi = initAbi ;
651
+ const isProxy =
652
+ abiFromApi ?. callFunctions ( ) . some ( entry => entry . get ( 'name' ) === 'implementation' ) ??
653
+ false ;
654
+ initDebugger . extend ( 'processInitForm' ) ( 'isProxy: %O' , isProxy ) ;
655
+ if ( isProxy ) {
656
+ const impl = await retryWithPrompt ( ( ) =>
657
+ withSpinner (
658
+ 'Fetching proxy implementation address...' ,
659
+ 'Failed to fetch proxy implementation address' ,
660
+ 'Warning fetching proxy implementation address' ,
661
+ ( ) => contractService . getProxyImplementation ( network . id , address ) ,
662
+ ) ,
663
+ ) ;
664
+ initDebugger . extend ( 'processInitForm' ) ( "proxyImplementation: '%s'" , impl ) ;
665
+ if ( impl ) {
666
+ const useImplementation = await prompt . confirm (
667
+ `Proxy contract detected. Index implementation contract at ${ impl } ?` ,
668
+ true ,
669
+ ) ;
670
+
671
+ if ( useImplementation ) {
672
+ implAddress = impl ;
673
+ abiFromApi = await retryWithPrompt ( ( ) =>
674
+ withSpinner (
675
+ 'Fetching implementation contract ABI...' ,
676
+ 'Failed to fetch implementation ABI' ,
677
+ 'Warning fetching implementation ABI' ,
678
+ ( ) =>
679
+ contractService . getABI ( protocolInstance . getABI ( ) , network . id , implAddress ! ) ,
680
+ ) ,
681
+ ) ;
682
+ }
683
+ }
684
+ }
650
685
}
686
+
651
687
// If startBlock is not provided, try to fetch it from Etherscan API
652
688
if ( ! initStartBlock ) {
653
689
startBlock = await retryWithPrompt ( ( ) =>
654
690
withSpinner (
655
691
'Fetching start block from contract API...' ,
656
692
'Failed to fetch start block' ,
657
693
'Warning fetching start block' ,
658
- ( ) => contractService . getStartBlock ( network . id , address ) ,
694
+ ( ) => contractService . getStartBlock ( network . id , implAddress ?? address ) ,
659
695
) ,
660
696
) ;
661
697
initDebugger . extend ( 'processInitForm' ) ( "startBlockFromEtherscan: '%s'" , startBlock ) ;
@@ -668,7 +704,7 @@ async function processInitForm(
668
704
'Fetching contract name from contract API...' ,
669
705
'Failed to fetch contract name' ,
670
706
'Warning fetching contract name' ,
671
- ( ) => contractService . getContractName ( network . id , address ) ,
707
+ ( ) => contractService . getContractName ( network . id , implAddress ?? address ) ,
672
708
) ,
673
709
) ;
674
710
initDebugger . extend ( 'processInitForm' ) ( "contractNameFromEtherscan: '%s'" , contractName ) ;
0 commit comments