-
Notifications
You must be signed in to change notification settings - Fork 14
Add Diamond contract unit tests #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zguesmi
merged 8 commits into
feature/diamond
from
feature/diamond-migration-part3-diamond-unit-tests
Jul 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a261ed
Update diamond contract license
zguesmi 7c8972a
Remove unused contract
zguesmi 9f2c309
Add Diamond unit tests
zguesmi 4894ead
Update changelog
zguesmi f81399f
Merge branch 'feature/diamond' into feature/diamond-migration-part3-d…
zguesmi 0152a1d
Fix coverage task
zguesmi 4446222
Add revert test
zguesmi 6d07510
Fix copyright headers
zguesmi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-FileCopyrightText: 2025 IEXEC BLOCKCHAIN TECH <contact@iex.ec> | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; | ||
import { getStorageAt } from '@nomicfoundation/hardhat-network-helpers'; | ||
import { expect } from 'chai'; | ||
import { ZeroAddress } from 'ethers'; | ||
import { ethers } from 'hardhat'; | ||
import { FacetCutAction } from 'hardhat-deploy/dist/types'; | ||
import { Diamond__factory, DiamondLoupeFacet__factory, IDiamond } from '../../typechain'; | ||
import { DiamondArgsStruct } from '../../typechain/contracts/Diamond'; | ||
import { getFunctionSelectors } from '../../utils/proxy-tools'; | ||
import { getLibDiamondConfigOrEmpty } from '../../utils/tools'; | ||
|
||
const DIAMOND_STORAGE_POSITION = ethers.id('diamond.standard.diamond.storage'); | ||
|
||
describe('Diamond', async () => { | ||
let deployer: SignerWithAddress; | ||
let owner: SignerWithAddress; | ||
|
||
beforeEach('Deploy', async () => { | ||
[deployer, owner] = await ethers.getSigners(); | ||
}); | ||
|
||
describe('Deployment', () => { | ||
it('Should set owner at deployment', async () => { | ||
const diamond = await _deployDiamond([]); // No facets | ||
const diamondAddress = await diamond.getAddress(); | ||
// Check the owner. | ||
const ownerSlotPosition = ethers.toBeHex(BigInt(DIAMOND_STORAGE_POSITION) + 3n); | ||
const actualOwnerAddress = await getStorageAt(diamondAddress, ownerSlotPosition); | ||
const expectedOwnerAddress = ethers.zeroPadValue(owner.address, 32); // Padded to 32 bytes. | ||
expect(actualOwnerAddress).to.equal(expectedOwnerAddress); | ||
// Check `DiamondCut` event with empty facet cuts. | ||
await expect(diamond.deploymentTransaction()) | ||
.to.emit(diamond, 'DiamondCut') | ||
.withArgs([], ZeroAddress, '0x'); | ||
}); | ||
|
||
it('Should apply diamond cuts at deployment', async () => { | ||
// Deploy any facet. | ||
const facet = await new DiamondLoupeFacet__factory() | ||
.connect(deployer) | ||
.deploy() | ||
.then((tx) => tx.waitForDeployment()); | ||
const facetCuts = [ | ||
{ | ||
facetAddress: await facet.getAddress(), | ||
action: FacetCutAction.Add, | ||
functionSelectors: getFunctionSelectors(new DiamondLoupeFacet__factory()), | ||
}, | ||
]; | ||
const diamond = await _deployDiamond(facetCuts); | ||
await expect(diamond.deploymentTransaction()) | ||
.to.emit(diamond, 'DiamondCut') | ||
.withArgs( | ||
[Object.values(facetCuts[0])], // Convert object to array for deep comparison. | ||
ZeroAddress, | ||
'0x', | ||
); | ||
}); | ||
}); | ||
|
||
describe('Delegatecall', () => { | ||
it.skip('[TODO] Should delegate `fallback` call', async () => {}); | ||
|
||
it.skip('[TODO] Should delegate `receive` call', async () => {}); | ||
|
||
it.skip('[TODO] Should delegate any function call', async () => {}); | ||
|
||
it('Should revert when function not found', async () => { | ||
const diamond = await _deployDiamond([]); | ||
const randomData = ethers.id('0xrandom'); | ||
const tx = owner.sendTransaction({ | ||
to: await diamond.getAddress(), | ||
value: 0, | ||
data: randomData, | ||
}); | ||
await expect(tx) | ||
.to.be.revertedWithCustomError(diamond, 'FunctionNotFound') | ||
.withArgs(randomData.slice(0, 10)); // First 4 bytes. | ||
}); | ||
}); | ||
|
||
async function _deployDiamond(facetCuts: IDiamond.FacetCutStruct[]) { | ||
const libDiamondConfig = await getLibDiamondConfigOrEmpty(deployer); | ||
return await new Diamond__factory(libDiamondConfig) | ||
.connect(deployer) | ||
.deploy(facetCuts, { | ||
owner: owner.address, | ||
init: ZeroAddress, | ||
initCalldata: '0x', | ||
} as DiamondArgsStruct) | ||
.then((tx) => tx.waitForDeployment()); | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
// SPDX-FileCopyrightText: 2020-2025 IEXEC BLOCKCHAIN TECH <contact@iex.ec> | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers'; | ||
import { Signature } from 'ethers'; | ||
import { ethers } from 'hardhat'; | ||
import hre, { ethers } from 'hardhat'; | ||
import { LibDiamond__factory } from '../typechain'; | ||
|
||
export function compactSignature(signature: string): string { | ||
return Signature.from(signature).compactSerialized; | ||
|
@@ -19,3 +21,27 @@ export function minBigInt(a: bigint, b: bigint) { | |
export function maxBigInt(a: bigint, b: bigint) { | ||
return a > b ? a : b; | ||
} | ||
|
||
/** | ||
* Deploys the `LibDiamond` library if running coverage task and returns | ||
* the linking configuration. Returns an empty config if not running coverage | ||
* task. | ||
* This fixes an issue with the coverage task that requires the library to be | ||
* deployed and linked to contracts where it is used. | ||
* @param deployer Signer to deploy the library. | ||
* @returns The library configuration or an empty object. | ||
*/ | ||
export async function getLibDiamondConfigOrEmpty(deployer: SignerWithAddress): Promise<any> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to type the promise output ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It generates a typing error so I use the generic type |
||
// No need to deploy the library if not running coverage task. | ||
if (!(hre as any).__SOLIDITY_COVERAGE_RUNNING) { | ||
return {}; | ||
} | ||
const libDiamondAddress = await new LibDiamond__factory() | ||
.connect(deployer) | ||
.deploy() | ||
.then((contract) => contract.waitForDeployment()) | ||
.then((contract) => contract.getAddress()); | ||
return { | ||
['@mudgen/diamond-1/contracts/libraries/LibDiamond.sol:LibDiamond']: libDiamondAddress, | ||
}; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.