Skip to content

Instantly share code, notes, and snippets.

@alephao
Created January 19, 2022 19:34
Show Gist options
  • Save alephao/942363f5af4b618d892d66e97ee2db8f to your computer and use it in GitHub Desktop.
Save alephao/942363f5af4b618d892d66e97ee2db8f to your computer and use it in GitHub Desktop.
Handling a Solidity Library function that reverts
// 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();
}
}
// 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