Created
January 19, 2022 19:34
-
-
Save alephao/942363f5af4b618d892d66e97ee2db8f to your computer and use it in GitHub Desktop.
Handling a Solidity Library function that reverts
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
// SPDX-License-Identifier: Unlicense | |
pragma solidity 0.8.10; | |
import "ds-test/test.sol"; | |
interface Hevm { | |
// Expects an error on next call | |
function expectRevert(bytes calldata) external; | |
} | |
error MyError(); | |
library MyLib { | |
function foo() internal { | |
revert MyError(); | |
} | |
} | |
contract MyTest is DSTest { | |
Hevm internal constant hevm = Hevm(HEVM_ADDRESS); | |
function testExample() public { | |
hevm.expectRevert(abi.encodeWithSignature("MyError()")); | |
MyLib.foo(); | |
} | |
} |
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
// SPDX-License-Identifier: Unlicense | |
pragma solidity 0.8.10; | |
import "ds-test/test.sol"; | |
interface Hevm { | |
// Expects an error on next call | |
function expectRevert(bytes calldata) external; | |
} | |
error MyError(); | |
library MyLib { | |
function foo() internal { | |
revert MyError(); | |
} | |
} | |
contract MyLibWrapper { | |
function foo() public { | |
MyLib.foo(); | |
} | |
} | |
contract MyTestFixed is DSTest { | |
Hevm internal constant hevm = Hevm(HEVM_ADDRESS); | |
MyLibWrapper libWrapper; | |
function setUp() public { | |
libWrapper = new MyLibWrapper(); | |
} | |
function testExample() public { | |
hevm.expectRevert(abi.encodeWithSignature("MyError()")); | |
libWrapper.foo(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment