-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bd271e
commit 5236166
Showing
11 changed files
with
355 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
--- | ||
"@layerzerolabs/devtools-evm-hardhat-test": patch | ||
"@layerzerolabs/devtools-evm-hardhat": patch | ||
--- | ||
|
||
Add a generic contract error parser for hardhat projects |
This file contains 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
File renamed without changes.
This file contains 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,2 @@ | ||
export * from './errors' | ||
export * from './parser' |
This file contains 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,40 @@ | ||
import { getDefaultRuntimeEnvironment } from '@/runtime' | ||
import { Contract } from '@ethersproject/contracts' | ||
import { | ||
createErrorParser as createErrorParserBase, | ||
makeZeroAddress, | ||
type OmniContractFactory, | ||
} from '@layerzerolabs/devtools-evm' | ||
|
||
/** | ||
* Helper function that combines all the available ABIs into a one giant | ||
* interface (only containing the error fragments) used for error decoding. | ||
* | ||
* TODO This function is not memoized at the moment, if the performance turns out to be a bottleneck we can memoize | ||
* | ||
* @returns {OmniContractFactory} | ||
*/ | ||
const createCombinedContractFactory = | ||
(): OmniContractFactory => | ||
async ({ eid }) => { | ||
// We're getting the artifacts so it does not really matter which environment we get them from | ||
const env = getDefaultRuntimeEnvironment() | ||
|
||
// We'll grab all the artifacts from the environment | ||
const artifactNames = await env.artifacts.getAllFullyQualifiedNames() | ||
const artifacts = artifactNames.map((name) => env.artifacts.readArtifactSync(name)) | ||
|
||
// Now we combine the ABIs and keep only the errors | ||
const abi = artifacts.flatMap((artifact) => artifact.abi).filter(({ type }) => type === 'error') | ||
|
||
// Even though duplicated fragments don't throw errors, they still pollute the interface with warning console.logs | ||
// To prevent this, we'll run a simple deduplication algorithm - use JSON encoded values as hashes | ||
const deduplicatedAbi = Object.values(Object.fromEntries(abi.map((abi) => [JSON.stringify(abi), abi]))) | ||
|
||
return { eid, contract: new Contract(makeZeroAddress(), deduplicatedAbi) } | ||
} | ||
|
||
/** | ||
* Creates a generic error parser based on all the artifacts found in your hardhat project | ||
*/ | ||
export const createErrorParser = () => createErrorParserBase(createCombinedContractFactory()) |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,75 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.22; | ||
|
||
contract Thrower { | ||
error CustomErrorWithNoArguments(); | ||
error CustomErrorWithAnArgument(string message); | ||
|
||
// This error is duplicated in the NestedThrower | ||
error CommonErrorWithNoArguments(); | ||
|
||
// This error is duplicated in the NestedThrower but with a different argument | ||
error CommonErrorWithAnArgument(string message); | ||
|
||
NestedThrower private nested; | ||
|
||
constructor() { | ||
nested = new NestedThrower(); | ||
} | ||
|
||
function throwWithCustomErrorAndNoArguments() external pure { | ||
revert CustomErrorWithNoArguments(); | ||
} | ||
|
||
function throwWithCustomErrorAndArgument(string calldata message) external pure { | ||
revert CustomErrorWithAnArgument(message); | ||
} | ||
|
||
function throwWithCommonErrorAndNoArguments() external pure { | ||
revert CommonErrorWithNoArguments(); | ||
} | ||
|
||
function throwWithCommonErrorAndArgument(string calldata message) external pure { | ||
revert CommonErrorWithAnArgument(message); | ||
} | ||
|
||
function throwNestedWithCustomErrorAndNoArguments() external view { | ||
nested.throwWithCustomErrorAndNoArguments(); | ||
} | ||
|
||
function throwNestedWithCustomErrorAndArgument(string calldata message) external view { | ||
nested.throwWithCustomErrorAndArgument(message); | ||
} | ||
|
||
function throwNestedWithCommonErrorAndNoArguments() external view { | ||
nested.throwWithCommonErrorAndNoArguments(); | ||
} | ||
|
||
function throwNestedWithCommonErrorAndArgument(uint256 code) external view { | ||
nested.throwWithCommonErrorAndArgument(code); | ||
} | ||
} | ||
|
||
contract NestedThrower { | ||
error NestedCustomErrorWithNoArguments(); | ||
error NestedCustomErrorWithAnArgument(string message); | ||
|
||
error CommonErrorWithNoArguments(); | ||
error CommonErrorWithAnArgument(uint256 code); | ||
|
||
function throwWithCustomErrorAndNoArguments() external pure { | ||
revert NestedCustomErrorWithNoArguments(); | ||
} | ||
|
||
function throwWithCustomErrorAndArgument(string calldata message) external pure { | ||
revert NestedCustomErrorWithAnArgument(message); | ||
} | ||
|
||
function throwWithCommonErrorAndNoArguments() external pure { | ||
revert CommonErrorWithNoArguments(); | ||
} | ||
|
||
function throwWithCommonErrorAndArgument(uint256 code) external pure { | ||
revert CommonErrorWithAnArgument(code); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.