11// SPDX-FileCopyrightText: 2024-2025 IEXEC BLOCKCHAIN TECH <[email protected] > 22// SPDX-License-Identifier: Apache-2.0
33
4- import { Interface } from 'ethers' ;
4+ import { Interface , ZeroAddress } from 'ethers' ;
55import { ethers } from 'hardhat' ;
6- import { ERC1538Query , ERC1538Query__factory , ERC1538Update__factory } from '../../typechain' ;
6+ import { FacetCutAction } from 'hardhat-deploy/dist/types' ;
7+ import { DiamondCutFacet__factory , DiamondLoupeFacet__factory } from '../../typechain' ;
78
89function encodeModuleProxyUpdate ( ModuleInterface : Interface , moduleAddress : string ) {
9- let moduleFunctions = '' ;
10+ // Get function selectors from the interface
11+ const functionSelectors : string [ ] = [ ] ;
1012 ModuleInterface . forEachFunction ( ( functionFragment ) => {
1113 const func = functionFragment . format ( ) ;
1214 console . log ( `- ${ func } ` ) ;
13- moduleFunctions += func + ';' ;
15+ const selector = ModuleInterface . getFunction ( functionFragment . name ) ?. selector ;
16+ if ( selector ) {
17+ functionSelectors . push ( selector ) ;
18+ }
1419 } ) ;
15- const moduleProxyUpdateData = ERC1538Update__factory . createInterface ( ) . encodeFunctionData (
16- 'updateContract' ,
17- [ moduleAddress , moduleFunctions , '' ] ,
20+
21+ // Create FacetCut for adding the module
22+ const facetCut = {
23+ facetAddress : moduleAddress ,
24+ action : FacetCutAction . Add ,
25+ functionSelectors : functionSelectors ,
26+ } ;
27+
28+ // Encode diamondCut call
29+ const moduleProxyUpdateData = DiamondCutFacet__factory . createInterface ( ) . encodeFunctionData (
30+ 'diamondCut' ,
31+ [ [ facetCut ] , ZeroAddress , '0x' ] ,
1832 ) ;
1933 return moduleProxyUpdateData ;
2034}
@@ -29,17 +43,29 @@ async function printBlockTime() {
2943 }
3044}
3145
32- // TODO: update this function to use DiamondLoup
3346async function printFunctions ( diamondProxyAddress : string ) {
34- const diamondQueryInstance : ERC1538Query = ERC1538Query__factory . connect (
47+ const diamondLoupeInstance = DiamondLoupeFacet__factory . connect (
3548 diamondProxyAddress ,
3649 ethers . provider ,
3750 ) ;
38- const functionCount = Number ( await diamondQueryInstance . totalFunctions ( ) ) ;
39- console . log ( `DiamondProxy supports ${ functionCount } functions:` ) ;
40- for ( let i = 0 ; i < functionCount ; i ++ ) {
41- const [ method , , contract ] = await diamondQueryInstance . functionByIndex ( i ) ;
42- console . log ( `[${ i } ] ${ contract } ${ method } ` ) ;
51+ const facets = await diamondLoupeInstance . facets ( ) ;
52+
53+ let totalFunctions = 0 ;
54+ facets . forEach ( ( facet ) => {
55+ totalFunctions += facet . functionSelectors . length ;
56+ } ) ;
57+
58+ console . log ( `DiamondProxy supports ${ totalFunctions } functions:` ) ;
59+
60+ let functionIndex = 0 ;
61+ for ( const facet of facets ) {
62+ for ( const selector of facet . functionSelectors ) {
63+ // Try to decode the selector to a readable function signature
64+ // Note: We can't easily get the full function signature from just the selector
65+ // This is a limitation compared to ERC1538Query.functionByIndex
66+ console . log ( `[${ functionIndex } ] ${ facet . facetAddress } ${ selector } ` ) ;
67+ functionIndex ++ ;
68+ }
4369 }
4470}
4571
0 commit comments