-
Notifications
You must be signed in to change notification settings - Fork 54
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
Adding tests for advance errors #499
Merged
+122
−16
Merged
Changes from all commits
Commits
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 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,25 +1,60 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
import { ErrorsExternal } from "./ErrorsExternal.sol"; | ||
|
||
contract Errors { | ||
constructor() { | ||
error InsufficientBalance(uint256 available, uint256 required); | ||
ErrorsExternal errorsExternal; | ||
event Result(uint code, string message); | ||
|
||
constructor(address errorsExternalAddr) { | ||
errorsExternal = ErrorsExternal(errorsExternalAddr); | ||
} | ||
|
||
function assertCheck(bool condition) public pure returns (bool) { | ||
function assertCheck(bool condition) external pure returns (bool) { | ||
assert(condition); | ||
return true; | ||
} | ||
|
||
function requireCheck(bool shouldRevert) public pure returns (bool) { | ||
function requireCheck(bool shouldRevert) external pure returns (bool) { | ||
require(shouldRevert); | ||
return true; | ||
} | ||
|
||
function revertCheck() public pure returns (bool) { | ||
function revertCheck() external pure returns (bool) { | ||
revert(); | ||
} | ||
|
||
function revertWithMessageCheck(string calldata message) public pure returns (bool) { | ||
function revertWithMessageCheck(string calldata message) external pure returns (bool) { | ||
revert(message); | ||
} | ||
|
||
function revertWithCustomError() external pure returns (bool) { | ||
revert InsufficientBalance(1, 100); | ||
} | ||
|
||
function tryCatchWithSimpleRevert() external returns (int value, bool success) { | ||
try errorsExternal.revertSimple() returns (bool v) { | ||
return (1, v); | ||
} catch (bytes memory) { | ||
emit Result(0, 'revertSimple'); | ||
} | ||
} | ||
|
||
function tryCatchWithErrorMessageRevert(string memory message) external returns (int value, bool success) { | ||
try errorsExternal.revertWithErrorMessage(message) returns (bool v) { | ||
return (1, v); | ||
} catch Error(string memory message) { | ||
emit Result(0, message); | ||
} | ||
} | ||
|
||
function tryCatchWithPanic() external returns (uint value, bool success) { | ||
try errorsExternal.panic() returns (uint v) { | ||
return (v, false); | ||
} catch Panic(uint errorCode) { | ||
emit Result(errorCode, 'panic'); | ||
} | ||
} | ||
} |
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,22 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.20; | ||
|
||
contract ErrorsExternal { | ||
error InsufficientBalance(uint256 available, uint256 required); | ||
|
||
function revertWithCustomError() external pure returns (bool) { | ||
revert InsufficientBalance(1, 100); | ||
} | ||
|
||
function revertSimple() external pure returns (bool) { | ||
revert(); | ||
} | ||
|
||
function revertWithErrorMessage(string memory message) external pure returns (bool) { | ||
revert(message); | ||
} | ||
|
||
function panic() external pure returns (uint) { | ||
return uint(4)/uint(0); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,54 +23,104 @@ const { ethers } = require('hardhat') | |
const Constants = require('../../constants') | ||
|
||
describe('@solidityequiv2 Solidity Errors', function () { | ||
let signers | ||
let contract | ||
let contract, hasError | ||
|
||
before(async function () { | ||
signers = await ethers.getSigners() | ||
const factoryErrorsExternal = await ethers.getContractFactory('ErrorsExternal') | ||
contractExternal = await factoryErrorsExternal.deploy() | ||
|
||
const factory = await ethers.getContractFactory(Constants.Contract.Errors) | ||
contract = await factory.deploy() | ||
const factory = await ethers.getContractFactory('Errors') | ||
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. Didn't need to change this as it's already in file |
||
contract = await factory.deploy(contractExternal.address) | ||
}) | ||
|
||
it('confirm assert works', async function () { | ||
beforeEach(async function () { | ||
hasError = false | ||
}) | ||
|
||
it('should confirm assert works', async function () { | ||
try { | ||
const res = await contract.assertCheck(1 == 1) | ||
expect(res).to.equal(true) | ||
|
||
await contract.assertCheck(1 > 1) | ||
} catch (err) { | ||
hasError = true | ||
expect(err).to.exist | ||
} | ||
expect(hasError).to.equal(true) | ||
}) | ||
|
||
it('confirm require works', async function () { | ||
it('should confirm require works', async function () { | ||
try { | ||
const resReverted = await contract.requireCheck(true) | ||
expect(resReverted).to.equal(true) | ||
|
||
const res = await contract.requireCheck(false) | ||
await contract.requireCheck(false) | ||
} catch (err) { | ||
hasError = true | ||
expect(err).to.exist | ||
} | ||
expect(hasError).to.equal(true) | ||
}) | ||
|
||
it('confirm revert works', async function () { | ||
it('should confirm revert works', async function () { | ||
try { | ||
await contract.revertCheck() | ||
} catch (err) { | ||
hasError = true | ||
expect(err).to.exist | ||
} | ||
expect(hasError).to.equal(true) | ||
}) | ||
|
||
it('confirm revert with message works', async function () { | ||
it('should confirm revert with message works', async function () { | ||
const message = "We unfortunalty need to revert this transaction" | ||
try { | ||
await contract.revertWithMessageCheck(message) | ||
} catch (err) { | ||
hasError = true | ||
expect(err.reason).to.exist | ||
expect(err.reason).to.equal(message) | ||
} | ||
expect(hasError).to.equal(true) | ||
}) | ||
|
||
it('should confirm revert with custom error works', async function () { | ||
try { | ||
await contract.revertWithCustomError() | ||
} catch (err) { | ||
hasError = true | ||
expect(err.code).to.equal('CALL_EXCEPTION') | ||
expect(err.errorName).to.equal('InsufficientBalance') | ||
expect(err.errorArgs.available).to.equal(ethers.BigNumber.from(1)) | ||
expect(err.errorArgs.required).to.equal(ethers.BigNumber.from(100)) | ||
} | ||
stefan-stefanooov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(hasError).to.equal(true) | ||
}) | ||
|
||
it('should confirm try/catch with simple revert', async function () { | ||
const tx = await contract.tryCatchWithSimpleRevert() | ||
const receipt = await tx.wait() | ||
expect(receipt).to.exist | ||
expect(receipt.events[0].args.code).to.equal(0) | ||
expect(receipt.events[0].args.message).to.equal('revertSimple') | ||
}) | ||
|
||
it('should confirm try/catch revert with error message', async function () { | ||
const message = "We unfortunalty need to revert this transaction" | ||
const tx = await contract.tryCatchWithErrorMessageRevert(message) | ||
const receipt = await tx.wait() | ||
expect(receipt).to.exist | ||
expect(receipt.events[0].args.code).to.equal(0) | ||
expect(receipt.events[0].args.message).to.equal(message) | ||
}) | ||
|
||
it('should confirm try/catch revert with panic', async function () { | ||
const tx = await contract.tryCatchWithPanic() | ||
const receipt = await tx.wait() | ||
expect(receipt).to.exist | ||
expect(receipt.events[0].args.code).to.equal(18) | ||
expect(receipt.events[0].args.message).to.equal('panic') | ||
}) | ||
|
||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was already there so no need to remove it right?
It should conflict with other PRs