From 592190f173421a97c48d648a54277465d18d77cf Mon Sep 17 00:00:00 2001 From: nerses-asaturyan Date: Mon, 7 Oct 2024 17:36:06 +0400 Subject: [PATCH] Refactor replace custom errors with require statements in EVM(native/ERC20) --- .../contracts/HashedTimeLockERC20.sol | 89 +++++-------------- .../contracts/HashedTimeLockEther.sol | 69 ++++---------- 2 files changed, 40 insertions(+), 118 deletions(-) diff --git a/packages/evm/solidity/contracts/HashedTimeLockERC20.sol b/packages/evm/solidity/contracts/HashedTimeLockERC20.sol index 19b03af..b144cab 100644 --- a/packages/evm/solidity/contracts/HashedTimeLockERC20.sol +++ b/packages/evm/solidity/contracts/HashedTimeLockERC20.sol @@ -57,22 +57,6 @@ contract LayerswapV8ERC20 { ); } - error FundsNotSent(); - error NotFutureTimelock(); - error NotPassedTimelock(); - error LockAlreadyExists(); - error HTLCAlreadyExists(); - error HTLCNotExists(); - error HashlockNotMatch(); - error AlreadyRedeemed(); - error AlreadyRefunded(); - error IncorrectData(); - error InsufficientBalance(); - error NoAllowance(); - error AlreadyLocked(); - error InvalidSigniture(); - error HashlockAlreadySet(); - struct HTLC { string dstAddress; string dstChain; @@ -137,7 +121,7 @@ contract LayerswapV8ERC20 { event LowLevelErrorOccurred(bytes lowLevelData); modifier _exists(bytes32 Id) { - if (!hasHTLC(Id)) revert HTLCNotExists(); + require(hasHTLC(Id),"HTLC Not Exists"); _; } @@ -154,32 +138,20 @@ contract LayerswapV8ERC20 { uint amount, address tokenContract ) external returns (bytes32 Id) { - if (amount == 0) { - revert FundsNotSent(); - } - if (timelock <= block.timestamp) { - revert NotFutureTimelock(); - } + require(amount > 0,"Funds Not Sent"); + require(timelock > block.timestamp,"Not Future Timelock"); IERC20 token = IERC20(tokenContract); - if (token.balanceOf(msg.sender) < amount) { - revert InsufficientBalance(); - } - - if (token.allowance(msg.sender, address(this)) < amount) { - revert NoAllowance(); - } - + require(token.balanceOf(msg.sender) >= amount,"Insufficient Balance"); + require(token.allowance(msg.sender, address(this)) >= amount,"No Allowance"); token.safeTransferFrom(msg.sender, address(this), amount); contractNonce += 1; Id = bytes32(blockHashAsUint ^ contractNonce); //Remove this check; the ID is guaranteed to be unique. - if (hasHTLC(Id)) { - revert HTLCAlreadyExists(); - } + require(!hasHTLC(Id),"HTLC Already Exists"); contractIds.push(Id); contracts[Id] = HTLC( dstAddress, @@ -216,24 +188,19 @@ contract LayerswapV8ERC20 { function addLock(bytes32 Id, bytes32 hashlock, uint256 timelock) external _exists(Id) returns (bytes32) { HTLC storage htlc = contracts[Id]; - if (htlc.refunded == true) { - revert AlreadyRefunded(); - } - if (timelock <= block.timestamp) { - revert NotFutureTimelock(); - } + require(!htlc.refunded,"Already Refunded"); + require(timelock > block.timestamp,"Not Future Timelock"); if (msg.sender == htlc.sender || msg.sender == address(this)) { if (htlc.hashlock == 0) { htlc.hashlock = hashlock; htlc.timelock = timelock; } else { - revert HashlockAlreadySet(); + require(false,"Hashlock Already Set"); } - emit TokenLockAdded(Id, hashlock, timelock); return Id; } else { - revert NoAllowance(); + require(false,"No Allowance"); } } @@ -241,7 +208,7 @@ contract LayerswapV8ERC20 { if (verifyMessage(message, v, r, s)) { return this.addLock(message.Id, message.hashlock, message.timelock); } else { - revert InvalidSigniture(); + require(false,"Invalid Signiture"); } } @@ -268,24 +235,13 @@ contract LayerswapV8ERC20 { uint256 amount, address tokenContract ) external returns (bytes32) { - if (timelock <= block.timestamp) { - revert NotFutureTimelock(); - } - if (amount == 0) { - revert FundsNotSent(); - } - if (hasHTLC(Id)) { - revert HTLCAlreadyExists(); - } + require(amount > 0, "Funds Not Sent"); + require(timelock > block.timestamp,"Not Future Timelock"); + require(!hasHTLC(Id),"HTLC Already Exists"); IERC20 token = IERC20(tokenContract); - if (token.balanceOf(msg.sender) < amount) { - revert InsufficientBalance(); - } - - if (token.allowance(msg.sender, address(this)) < amount) { - revert NoAllowance(); - } + require(token.balanceOf(msg.sender) >= amount,"Insufficient Balance"); + require(token.allowance(msg.sender, address(this)) >= amount,"No Allowance"); token.safeTransferFrom(msg.sender, address(this), amount); contracts[Id] = HTLC( @@ -332,9 +288,9 @@ contract LayerswapV8ERC20 { function redeem(bytes32 Id, uint256 secret) external _exists(Id) returns (bool) { HTLC storage htlc = contracts[Id]; - if (htlc.hashlock != sha256(abi.encodePacked(secret))) revert HashlockNotMatch(); - if (htlc.redeemed) revert AlreadyRedeemed(); - if (htlc.refunded) revert AlreadyRefunded(); + require(htlc.hashlock == sha256(abi.encodePacked(secret)),"Hashlock Not Match"); + require(!htlc.refunded,"Already Refunded"); + require(!htlc.redeemed,"Already Redeemed"); htlc.secret = secret; htlc.redeemed = true; @@ -351,10 +307,9 @@ contract LayerswapV8ERC20 { */ function refund(bytes32 Id) external _exists(Id) returns (bool) { HTLC storage htlc = contracts[Id]; - - if (htlc.refunded) revert AlreadyRefunded(); - if (htlc.redeemed) revert AlreadyRedeemed(); - if (htlc.timelock > block.timestamp) revert NotPassedTimelock(); + require(!htlc.refunded,"Already Refunded"); + require(!htlc.redeemed,"Already Redeemed"); + require(htlc.timelock <= block.timestamp,"Not Passed Timelock"); htlc.refunded = true; IERC20(htlc.tokenContract).safeTransfer(htlc.sender, htlc.amount); diff --git a/packages/evm/solidity/contracts/HashedTimeLockEther.sol b/packages/evm/solidity/contracts/HashedTimeLockEther.sol index 2a70b7a..d2c9b6b 100644 --- a/packages/evm/solidity/contracts/HashedTimeLockEther.sol +++ b/packages/evm/solidity/contracts/HashedTimeLockEther.sol @@ -41,20 +41,6 @@ contract LayerswapV8 { ); } - error FundsNotSent(); - error NotFutureTimelock(); - error NotPassedTimelock(); - error LockAlreadyExists(); - error HTLCAlreadyExists(); - error HTLCNotExists(); - error HashlockNotMatch(); - error AlreadyRedeemed(); - error AlreadyRefunded(); - error AlreadyLocked(); - error NoAllowance(); - error InvalidSigniture(); - error HashlockAlreadySet(); - // Structure for storing swap-related data struct HTLC { string dstAddress; @@ -110,7 +96,7 @@ contract LayerswapV8 { event LowLevelErrorOccurred(bytes lowLevelData); modifier _exists(bytes32 Id) { - if (!hasHTLC(Id)) revert HTLCNotExists(); + require(hasHTLC(Id),"HTLC Not Exists"); _; } @@ -130,19 +116,13 @@ contract LayerswapV8 { address srcReceiver, uint256 timelock ) external payable returns (bytes32 Id) { - if (msg.value == 0) { - revert FundsNotSent(); - } - if (timelock <= block.timestamp) { - revert NotFutureTimelock(); - } + require(msg.value > 0,"Funds Not Sent"); + require(timelock > block.timestamp,"Not Future Timelock"); contractNonce += 1; Id = bytes32(blockHashAsUint ^ contractNonce); //Remove this check; the ID is guaranteed to be unique. - if (hasHTLC(Id)) { - revert HTLCAlreadyExists(); - } + require(!hasHTLC(Id),"HTLC Already Exists"); contractIds.push(Id); contracts[Id] = HTLC( dstAddress, @@ -177,10 +157,9 @@ contract LayerswapV8 { function refund(bytes32 Id) external _exists(Id) returns (bool) { HTLC storage htlc = contracts[Id]; - - if (htlc.refunded) revert AlreadyRefunded(); - if (htlc.redeemed) revert AlreadyRedeemed(); - if (htlc.timelock > block.timestamp) revert NotPassedTimelock(); + require(!htlc.refunded,"Already Refunded"); + require(!htlc.redeemed,"Already Redeemed"); + require(htlc.timelock <= block.timestamp,"Not Passed Timelock"); htlc.refunded = true; (bool success, ) = htlc.sender.call{ value: htlc.amount }(''); @@ -191,25 +170,19 @@ contract LayerswapV8 { function addLock(bytes32 Id, bytes32 hashlock, uint256 timelock) external _exists(Id) returns (bytes32) { HTLC storage htlc = contracts[Id]; - if (htlc.refunded == true) { - revert AlreadyRefunded(); - } - if (timelock <= block.timestamp) { - revert NotFutureTimelock(); - } - + require(!htlc.refunded,"Already Refunded"); + require(timelock > block.timestamp,"Not Future Timelock"); if (msg.sender == htlc.sender || msg.sender == address(this)) { if (htlc.hashlock == 0) { htlc.hashlock = hashlock; htlc.timelock = timelock; } else { - revert HashlockAlreadySet(); + require(false,"Hashlock Already Set"); } - emit TokenLockAdded(Id, hashlock, timelock); return Id; } else { - revert NoAllowance(); + require(false,"No Allowance"); } } @@ -217,7 +190,7 @@ contract LayerswapV8 { if (verifyMessage(message, v, r, s)) { return this.addLock(message.Id, message.hashlock, message.timelock); } else { - revert InvalidSigniture(); + require(false,"Invalid Signiture"); } } @@ -231,15 +204,9 @@ contract LayerswapV8 { string memory dstAddress, string memory dstAsset ) external payable returns (bytes32) { - if (msg.value == 0) { - revert FundsNotSent(); - } - if (timelock <= block.timestamp) { - revert NotFutureTimelock(); - } - if (hasHTLC(Id)) { - revert HTLCAlreadyExists(); - } + require(msg.value > 0, "Funds Not Sent"); + require(timelock > block.timestamp,"Not Future Timelock"); + require(!hasHTLC(Id),"HTLC Already Exists"); contracts[Id] = HTLC( dstAddress, dstChain, @@ -273,9 +240,9 @@ contract LayerswapV8 { function redeem(bytes32 Id, uint256 secret) external _exists(Id) returns (bool) { HTLC storage htlc = contracts[Id]; - if (htlc.hashlock != sha256(abi.encodePacked(secret))) revert HashlockNotMatch(); - if (htlc.refunded) revert AlreadyRefunded(); - if (htlc.redeemed) revert AlreadyRedeemed(); + require(htlc.hashlock == sha256(abi.encodePacked(secret)),"Hashlock Not Match"); + require(!htlc.refunded,"Already Refunded"); + require(!htlc.redeemed,"Already Redeemed"); htlc.secret = secret; htlc.redeemed = true;