Skip to content
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
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions contracts/solidity/errors/Errors.sol
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');
}
}
}
22 changes: 22 additions & 0 deletions contracts/solidity/errors/ErrorsExternal.sol
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);
}
}
1 change: 0 additions & 1 deletion test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ const Contract = {
ExchangeRateMock: 'ExchangeRateMock',
PrngSystemContract: 'PrngSystemContract',
Concatenation: 'Concatenation',
Errors: 'Errors',
Copy link
Collaborator

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

Transaction: 'Transaction',
MessageFrameAddresses: 'MessageFrameAddresses',
New: 'New',
Expand Down
70 changes: 60 additions & 10 deletions test/solidity/errors/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Copy link
Collaborator

Choose a reason for hiding this comment

The 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')
})

})