Skip to content

Commit

Permalink
Adding tests for advance errors
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov committed Oct 18, 2023
1 parent b7f9d1e commit b1db954
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 14 deletions.
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: 1 addition & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const Contract = {
PrngSystemContract: 'PrngSystemContract',
Concatenation: 'Concatenation',
Errors: 'Errors',
ErrorsExternal: 'ErrorsExternal',
Transaction: 'Transaction',
MessageFrameAddresses: 'MessageFrameAddresses',
New: 'New',
Expand Down
54 changes: 45 additions & 9 deletions test/solidity/errors/errors.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
const { expect, assert } = require('chai')
const { expect } = require('chai')
const { ethers } = require('hardhat')
const Constants = require('../../constants')

describe('Solidity Errors', function () {
let signers
describe('@solidityequiv2 Solidity Errors', function () {
let contract

before(async function () {
signers = await ethers.getSigners()
const factoryErrorsExternal = await ethers.getContractFactory(Constants.Contract.ErrorsExternal)
contractExternal = await factoryErrorsExternal.deploy()

const factory = await ethers.getContractFactory(Constants.Contract.Errors)
contract = await factory.deploy()
contract = await factory.deploy(contractExternal.address)
})

it('confirm assert works', async function () {
it('should confirm assert works', async function () {
try {
const res = await contract.assertCheck(1 == 1)
expect(res).to.equal(true)
Expand All @@ -24,7 +24,7 @@ describe('Solidity Errors', function () {
}
})

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)
Expand All @@ -35,15 +35,15 @@ describe('Solidity Errors', function () {
}
})

it('confirm revert works', async function () {
it('should confirm revert works', async function () {
try {
await contract.revertCheck()
} catch (err) {
expect(err).to.exist
}
})

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)
Expand All @@ -53,4 +53,40 @@ describe('Solidity Errors', function () {
}
})

it('should confirm revert with custom error works', async function () {
try {
await contract.revertWithCustomError()
} catch (err) {
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))
}
})

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

})

0 comments on commit b1db954

Please sign in to comment.