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 { ContractFactory , ZeroAddress } from 'ethers' ;
55import { ethers } from 'hardhat' ;
6- import { ERC1538Query , ERC1538Query__factory , ERC1538Update__factory } from '../../typechain' ;
7-
8- function encodeModuleProxyUpdate ( ModuleInterface : Interface , moduleAddress : string ) {
9- let moduleFunctions = '' ;
10- ModuleInterface . forEachFunction ( ( functionFragment ) => {
11- const func = functionFragment . format ( ) ;
12- console . log ( `- ${ func } ` ) ;
13- moduleFunctions += func + ';' ;
14- } ) ;
15- const moduleProxyUpdateData = ERC1538Update__factory . createInterface ( ) . encodeFunctionData (
16- 'updateContract' ,
17- [ moduleAddress , moduleFunctions , '' ] ,
6+ import { FacetCutAction } from 'hardhat-deploy/dist/types' ;
7+ import { DiamondCutFacet__factory , DiamondLoupeFacet__factory } from '../../typechain' ;
8+ import { getFunctionSelectors } from '../../utils/proxy-tools' ;
9+
10+ function encodeModuleProxyUpdate ( contractFactory : ContractFactory , moduleAddress : string ) {
11+ // Get function selectors from the contract factory
12+ const functionSelectors = getFunctionSelectors ( contractFactory ) ;
13+
14+ // Create FacetCut for adding the module
15+ const facetCut = {
16+ facetAddress : moduleAddress ,
17+ action : FacetCutAction . Add ,
18+ functionSelectors : functionSelectors ,
19+ } ;
20+
21+ // Encode diamondCut call
22+ const moduleProxyUpdateData = DiamondCutFacet__factory . createInterface ( ) . encodeFunctionData (
23+ 'diamondCut' ,
24+ [ [ facetCut ] , ZeroAddress , '0x' ] ,
1825 ) ;
1926 return moduleProxyUpdateData ;
2027}
@@ -29,17 +36,29 @@ async function printBlockTime() {
2936 }
3037}
3138
32- // TODO: update this function to use DiamondLoup
3339async function printFunctions ( diamondProxyAddress : string ) {
34- const diamondQueryInstance : ERC1538Query = ERC1538Query__factory . connect (
40+ const diamondLoupeInstance = DiamondLoupeFacet__factory . connect (
3541 diamondProxyAddress ,
3642 ethers . provider ,
3743 ) ;
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 } ` ) ;
44+ const facets = await diamondLoupeInstance . facets ( ) ;
45+
46+ let totalFunctions = 0 ;
47+ facets . forEach ( ( facet ) => {
48+ totalFunctions += facet . functionSelectors . length ;
49+ } ) ;
50+
51+ console . log ( `DiamondProxy supports ${ totalFunctions } functions:` ) ;
52+
53+ let functionIndex = 0 ;
54+ for ( const facet of facets ) {
55+ for ( const selector of facet . functionSelectors ) {
56+ // Try to decode the selector to a readable function signature
57+ // Note: We can't easily get the full function signature from just the selector
58+ // This is a limitation compared to ERC1538Query.functionByIndex
59+ console . log ( `[${ functionIndex } ] ${ facet . facetAddress } ${ selector } ` ) ;
60+ functionIndex ++ ;
61+ }
4362 }
4463}
4564
0 commit comments