Skip to content

Commit b01b9e5

Browse files
Merge pull request #107 from The-Poolz/issue-104
use errors instead of requires
2 parents d9848e4 + 9139d1f commit b01b9e5

File tree

12 files changed

+59
-60
lines changed

12 files changed

+59
-60
lines changed

contracts/Array.sol

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ pragma solidity ^0.8.0;
44

55
/// @title contains array utility functions
66
library Array {
7+
error InvalidArrayLength(uint256 _arrLength, uint256 _n);
8+
error ZeroArrayLength();
9+
710
/// @dev returns a new slice of the array
811
function KeepNElementsInArray(uint256[] memory _arr, uint256 _n)
912
internal
1013
pure
1114
returns (uint256[] memory newArray)
1215
{
1316
if (_arr.length == _n) return _arr;
14-
require(_arr.length > _n, "can't cut more then got");
17+
if (_arr.length <= _n) revert InvalidArrayLength(_arr.length, _n);
1518
newArray = new uint256[](_n);
1619
for (uint256 i = 0; i < _n; ++i) {
1720
newArray[i] = _arr[i];
@@ -25,7 +28,7 @@ library Array {
2528
returns (address[] memory newArray)
2629
{
2730
if (_arr.length == _n) return _arr;
28-
require(_arr.length > _n, "can't cut more then got");
31+
if (_arr.length <= _n) revert InvalidArrayLength(_arr.length, _n);
2932
newArray = new address[](_n);
3033
for (uint256 i = 0; i < _n; ++i) {
3134
newArray[i] = _arr[i];
@@ -39,7 +42,7 @@ library Array {
3942
pure
4043
returns (bool)
4144
{
42-
require(_arr.length > 0, "array should be greater than zero");
45+
if (_arr.length == 0) revert ZeroArrayLength();
4346
uint256 temp = _arr[0];
4447
for (uint256 i = 1; i < _arr.length; ++i) {
4548
if (temp > _arr[i]) {

contracts/ERC20Helper.sol

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import "@ironblocks/firewall-consumer/contracts/FirewallConsumer.sol";
88
contract ERC20Helper is FirewallConsumer {
99
event TransferOut(uint256 Amount, address To, address Token);
1010
event TransferIn(uint256 Amount, address From, address Token);
11+
12+
error NoAllowance();
13+
error SentIncorrectAmount();
14+
error ReceivedIncorrectAmount();
15+
error ZeroAmount();
16+
1117
modifier TestAllowance(
1218
address _token,
1319
address _owner,
1420
uint256 _amount
1521
) {
16-
require(
17-
ERC20(_token).allowance(_owner, address(this)) >= _amount,
18-
"ERC20Helper: no allowance"
19-
);
22+
if (ERC20(_token).allowance(_owner, address(this)) < _amount) {
23+
revert NoAllowance();
24+
}
2025
_;
2126
}
2227

@@ -28,33 +33,27 @@ contract ERC20Helper is FirewallConsumer {
2833
uint256 OldBalance = ERC20(_Token).balanceOf(address(this));
2934
emit TransferOut(_Amount, _Reciver, _Token);
3035
ERC20(_Token).transfer(_Reciver, _Amount);
31-
require(
32-
(ERC20(_Token).balanceOf(address(this)) + _Amount) == OldBalance,
33-
"ERC20Helper: sent incorrect amount"
34-
);
36+
if (ERC20(_Token).balanceOf(address(this)) == (OldBalance + _Amount)) revert SentIncorrectAmount();
3537
}
3638

3739
function TransferInToken(
3840
address _Token,
3941
address _Subject,
4042
uint256 _Amount
4143
) internal TestAllowance(_Token, _Subject, _Amount) {
42-
require(_Amount > 0);
44+
if (_Amount == 0) revert ZeroAmount();
4345
uint256 OldBalance = ERC20(_Token).balanceOf(address(this));
4446
ERC20(_Token).transferFrom(_Subject, address(this), _Amount);
4547
emit TransferIn(_Amount, _Subject, _Token);
46-
require(
47-
(OldBalance + _Amount) == ERC20(_Token).balanceOf(address(this)),
48-
"ERC20Helper: Received Incorrect Amount"
49-
);
48+
if (ERC20(_Token).balanceOf(address(this)) != (OldBalance + _Amount)) revert ReceivedIncorrectAmount();
5049
}
5150

5251
function ApproveAllowanceERC20(
5352
address _Token,
5453
address _Subject,
5554
uint256 _Amount
5655
) internal firewallProtectedSig(0x91251680) {
57-
require(_Amount > 0);
56+
if (_Amount == 0) revert ZeroAmount();
5857
ERC20(_Token).approve(_Subject, _Amount);
5958
}
6059
}

contracts/ERC721Helper.sol

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@ contract ERC721Helper is FirewallConsumer {
99
event TransferOut(address Token, uint256 TokenId, address To);
1010
event TransferIn(address Token, uint256 TokenId, address From);
1111

12+
error NoAllowance();
13+
1214
modifier TestNFTAllowance(
1315
address _token,
1416
uint256 _tokenId,
1517
address _owner
1618
) {
17-
require(
18-
IERC721(_token).isApprovedForAll(_owner, address(this)) ||
19-
IERC721(_token).getApproved(_tokenId) == address(this),
20-
"No Allowance"
21-
);
19+
if (
20+
!IERC721(_token).isApprovedForAll(_owner, address(this)) &&
21+
IERC721(_token).getApproved(_tokenId) != address(this)
22+
) {
23+
revert NoAllowance();
24+
}
2225
_;
2326
}
2427

25-
function TransferNFTOut(
26-
address _Token,
27-
uint256 _TokenId,
28-
address _To
29-
) internal firewallProtectedSig(0x53905fab) {
28+
function TransferNFTOut(address _Token, uint256 _TokenId, address _To) internal firewallProtectedSig(0x53905fab) {
3029
IERC721(_Token).transferFrom(address(this), _To, _TokenId);
3130
emit TransferOut(_Token, _TokenId, _To);
3231
assert(IERC721(_Token).ownerOf(_TokenId) == _To);
@@ -42,11 +41,7 @@ contract ERC721Helper is FirewallConsumer {
4241
assert(IERC721(_Token).ownerOf(_TokenId) == address(this));
4342
}
4443

45-
function SetApproveForAllNFT(
46-
address _Token,
47-
address _To,
48-
bool _Approve
49-
) internal firewallProtectedSig(0xd5ebe78c) {
44+
function SetApproveForAllNFT(address _Token, address _To, bool _Approve) internal firewallProtectedSig(0xd5ebe78c) {
5045
IERC721(_Token).setApprovalForAll(_To, _Approve);
5146
}
5247
}

contracts/ETHHelper.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import "@openzeppelin/contracts/access/Ownable.sol";
55
import "@ironblocks/firewall-consumer/contracts/FirewallConsumer.sol";
66

77
contract ETHHelper is Ownable, FirewallConsumer {
8-
constructor() Ownable(_msgSender()) {
9-
IsPayble = false;
10-
}
8+
error InvalidAmount();
9+
error SentIncorrectAmount();
10+
11+
constructor() Ownable(_msgSender()) {}
1112

1213
modifier ReceivETH(
1314
uint256 msgValue,
1415
address msgSender,
1516
uint256 _MinETHInvest
1617
) {
17-
require(msgValue >= _MinETHInvest, "Send ETH to invest");
18+
if (msgValue < _MinETHInvest) revert InvalidAmount();
1819
emit TransferInETH(msgValue, msgSender);
1920
_;
2021
}
@@ -37,6 +38,6 @@ contract ETHHelper is Ownable, FirewallConsumer {
3738
emit TransferOutETH(_amount, _Reciver);
3839
uint256 beforeBalance = address(_Reciver).balance;
3940
_Reciver.transfer(_amount);
40-
require((beforeBalance + _amount) == address(_Reciver).balance, "The transfer did not complite");
41+
if ((beforeBalance + _amount) != address(_Reciver).balance) revert SentIncorrectAmount();
4142
}
4243
}

contracts/GovManager.sol

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ import "@openzeppelin/contracts/access/Ownable.sol";
66
import "@ironblocks/firewall-consumer/contracts/FirewallConsumer.sol";
77

88
contract GovManager is Ownable, FirewallConsumer {
9-
event GovernorUpdated (
10-
address indexed oldGovernor,
11-
address indexed newGovernor
12-
);
9+
event GovernorUpdated(address indexed oldGovernor, address indexed newGovernor);
10+
11+
error AuthorizationError();
1312

1413
address public GovernorContract;
1514

1615
modifier onlyOwnerOrGov() {
17-
require(
18-
msg.sender == owner() || msg.sender == GovernorContract,
19-
"Authorization Error"
20-
);
16+
if (msg.sender != owner() && msg.sender != GovernorContract) {
17+
revert AuthorizationError();
18+
}
2119
_;
2220
}
2321

contracts/mocks/ETHHelperMock.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ pragma solidity ^0.8.0;
44
import "../ETHHelper.sol";
55

66
contract ETHHelperMock is ETHHelper {
7-
function receiveETH(
8-
uint256 minETHInvest
9-
) external payable ReceivETH(msg.value, msg.sender, minETHInvest) {}
7+
constructor() ETHHelper() {}
8+
9+
function receiveETH(uint256 minETHInvest) external payable ReceivETH(msg.value, msg.sender, minETHInvest) {}
1010

1111
function transferETH(address payable reciver, uint256 amount) external {
1212
TransferETH(reciver, amount);

test/1_Admin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ describe('Admin Tests', function () {
6161
it('should fail to set POZ timer when called without invalid address', async () => {
6262
await pozInstance.setGovernorContract(ZERO_ADDRESS);
6363
const timer = 1000;
64-
await expect(pozInstance.connect(addr).SetPozTimer(timer)).to.be.revertedWith('Authorization Error');
64+
await expect(pozInstance.connect(addr).SetPozTimer(timer)).to.be.revertedWithCustomError(pozInstance, "AuthorizationError");
6565
});
6666
});

test/4_Pausable.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ describe('PausableHelper', function () {
1212

1313
it('should revert invalid user access', async () => {
1414
const [, addr1] = await ethers.getSigners();
15-
await expect(pausableHelper.connect(addr1).pause()).to.be.revertedWith('Authorization Error');
16-
await expect(pausableHelper.connect(addr1).unpause()).to.be.revertedWith('Authorization Error');
15+
await expect(pausableHelper.connect(addr1).pause()).to.be.revertedWithCustomError(pausableHelper, "AuthorizationError")
16+
await expect(pausableHelper.connect(addr1).unpause()).to.be.revertedWithCustomError(pausableHelper, "AuthorizationError");
1717
});
1818

1919
it('check pause event', async () => {

test/5_Array.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Array library', function () {
2323
const arr = [1, 2, 46, 6, 9];
2424
const newArr = await arrayLibrary.callStatic.keepNElementsInArray(arr, 2);
2525
expect(newArr.toString()).to.equal(arr.slice(0, 2).toString());
26-
await expect(arrayLibrary.keepNElementsInArray(arr, 20)).to.be.revertedWith("can't cut more then got");
26+
await expect(arrayLibrary.keepNElementsInArray(arr, 20)).to.be.revertedWithCustomError(arrayLibrary, "InvalidArrayLength")
2727
// return the same array if the length is the same
2828
const sameArr = await arrayLibrary.callStatic.keepNElementsInArray(arr, arr.length);
2929
expect(sameArr.toString()).to.equal(arr.toString());
@@ -34,14 +34,14 @@ describe('Array library', function () {
3434
const arr = [addr0.address, addr1.address, addr2.address, addr4.address];
3535
const newArr = await arrayLibrary.callStatic.KeepNElementsInArray(arr, 2);
3636
expect(newArr.toString()).to.equal(arr.slice(0, 2).toString());
37-
await expect(arrayLibrary.KeepNElementsInArray(arr, 20)).to.be.revertedWith("can't cut more then got");
37+
await expect(arrayLibrary.KeepNElementsInArray(arr, 20)).to.be.revertedWithCustomError(arrayLibrary, "InvalidArrayLength");
3838
// return the same array if the length is the same
3939
const sameArr = await arrayLibrary.callStatic.KeepNElementsInArray(arr, arr.length);
4040
expect(sameArr.toString()).to.equal(arr.toString());
4141
});
4242

4343
it('is Array Ordered', async () => {
44-
await expect(arrayLibrary.isArrayOrdered([])).to.be.revertedWith('array should be greater than zero');
44+
await expect(arrayLibrary.isArrayOrdered([])).to.be.revertedWithCustomError(arrayLibrary, "ZeroArrayLength");
4545
const orderedArr = [1, 2, 3, 4, 5];
4646
const nonOrderedArr = [5, 8, 3, 19];
4747
let status = await arrayLibrary.callStatic.isArrayOrdered(orderedArr);

test/6_ERC721Helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ describe('ERC721 Helper tests', function () {
5757
});
5858

5959
it('should return no allowance message', async () => {
60-
await expect(erc721Helper.transferNFTIn(erc721Token.address, itemId, user.address)).to.be.revertedWith(
61-
'No Allowance',
60+
await expect(erc721Helper.transferNFTIn(erc721Token.address, itemId, user.address)).to.be.revertedWithCustomError(
61+
erc721Helper,
62+
'NoAllowance',
6263
);
6364
});
6465
});

test/7_ETHHelper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ describe('ETH Helper tests', function () {
2828
});
2929

3030
it('should revert invalid amount eth', async () => {
31-
await expect(ethHelper.receiveETH(minETHInvest, { value: minETHInvest / 2 })).to.be.revertedWith(
32-
'Send ETH to invest',
31+
await expect(ethHelper.receiveETH(minETHInvest, { value: minETHInvest / 2 })).to.be.revertedWithCustomError(
32+
ethHelper,
33+
'InvalidAmount',
3334
);
3435
});
3536

test/8_ERC20Helper.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ describe('ERC20 Helper tests', function () {
4444
});
4545

4646
it('should revert invalid approved amount', async () => {
47-
await expect(erc20Helper.transferInToken(token.address, owner.address, MAX_UINT256)).to.be.revertedWith(
48-
'ERC20Helper: no allowance',
47+
await expect(erc20Helper.transferInToken(token.address, owner.address, MAX_UINT256)).to.be.revertedWithCustomError(
48+
erc20Helper,
49+
'NoAllowance',
4950
);
5051
});
5152

0 commit comments

Comments
 (0)