Skip to content

Commit

Permalink
fix: ensure public methods are named to external in IDiamondProxy (#46)
Browse files Browse the repository at this point in the history
* fix: ensure public methods are named to external in IDiamondProxy - see #41

* build: try to fix build failing due to typescript issue

see isaacs/node-lru-cache#348
  • Loading branch information
hiddentao authored Sep 11, 2024
1 parent 9eccf8c commit 7ce5704
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/shared/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ export const getUserFacetsAndFunctions = (ctx: Context): FacetDefinition[] => {
.filter(
node => node.visibility === 'external' || (ctx.config.diamond.publicMethods && node.visibility === 'public')
)
.map(node => {
// rename public methods to external if publicMethods is true
if (node.visibility === 'public') {
trace(`Renamed public method to external: ${contract.name}.${node.name}`)
node.visibility = 'external'
}
return node
})

const functions = functionDefinitions.map(node => {
const fnParamParsingContext: FunctionParsingContext = {
Expand Down
40 changes: 40 additions & 0 deletions test/common-build-steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,46 @@ function getData() external view returns (Data memory);`,
})
})

describe('if public methods are configured to be included', () => {
beforeEach(async () => {
removeFile(join(cwd, `${contractSrcBasePath}/facets/ExampleFacet.sol`))

writeFile(join(cwd, `${contractSrcBasePath}/facets/ExampleFacet.sol`), `
pragma solidity >=0.8.21;
import "../libs/LibAppStorage.sol";
contract ExampleFacet {
function getInt() external view returns (uint) {
return 1;
}
function getIntPublic() public view returns (uint) {
return 2;
}
}
`)

await updateConfigFile(join(cwd, 'gemforge.config.cjs'), (cfg: GemforgeConfig) => {
cfg.diamond.publicMethods = true
return cfg
})
})

it("generates proxy interface and renames public methods to external", async () => {
const ret = cli('build', { cwd })
expect(ret.success).to.be.true

const filePath = path.join(cwd, `${contractSrcBasePath}/generated/IDiamondProxy.sol`)
assertFileMatchesTemplate(filePath, 'IDiamondProxy.sol', {
__SOLC_SPDX__: 'MIT',
__SOLC_VERSION__: '0.8.21',
__LIB_DIAMOND_PATH__: 'lib/diamond-2-hardhat',
__CUSTOM_IMPORTS__: `import "../shared/Structs.sol";\n`,
__METHODS__: `function setData(Data calldata d) external;
function getInt() external view returns (uint);
function getIntPublic() external view returns (uint);`,
})
})
})

if (framework === 'foundry') {
it("generates test helper", async () => {
expect(cli('build', { cwd }).success).to.be.true
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"skipLibCheck": false,
"skipLibCheck": true,
"declaration": true,
"resolveJsonModule": true,
"isolatedModules": true,
Expand Down

0 comments on commit 7ce5704

Please sign in to comment.