Skip to content

Commit c07671e

Browse files
committed
custom errors
1 parent 2501575 commit c07671e

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

contracts/crosschain/axelar/AxelarGatewayBase.sol

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: MIT
22

3-
pragma solidity ^0.8.0;
3+
pragma solidity ^0.8.27;
44

55
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
66
import {IAxelarGateway} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol";
@@ -22,6 +22,9 @@ abstract contract AxelarGatewayBase is Ownable {
2222
/// @dev Error emitted when an unsupported chain is queried.
2323
error UnsupportedChain(string caip2);
2424

25+
error ChainEquivalenceAlreadyRegistered(string caip2);
26+
error RemoteGatewayAlreadyRegistered(string caip2);
27+
2528
/// @dev Axelar's official gateway for the current chain.
2629
IAxelarGateway public immutable localGateway;
2730

@@ -36,26 +39,26 @@ abstract contract AxelarGatewayBase is Ownable {
3639
/// @dev Returns the equivalent chain given an id that can be either CAIP-2 or an Axelar network identifier.
3740
function getEquivalentChain(string memory input) public view virtual returns (string memory output) {
3841
output = _chainEquivalence[input];
39-
if (bytes(output).length == 0) revert UnsupportedChain(input);
42+
require(bytes(output).length > 0, UnsupportedChain(input));
4043
}
4144

4245
/// @dev Returns the CAIP-10 account address of the remote gateway for a given CAIP-2 chain identifier.
4346
function getRemoteGateway(string memory caip2) public view virtual returns (string memory remoteGateway) {
4447
remoteGateway = _remoteGateways[caip2];
45-
if (bytes(remoteGateway).length == 0) revert UnsupportedChain(caip2);
48+
require(bytes(remoteGateway).length > 0, UnsupportedChain(caip2));
4649
}
4750

4851
/// @dev Registers a chain equivalence between a CAIP-2 chain identifier and an Axelar network identifier.
4952
function registerChainEquivalence(string calldata caip2, string calldata axelarSupported) public virtual onlyOwner {
50-
require(bytes(_chainEquivalence[caip2]).length == 0, "Chain equivalence already registered");
53+
require(bytes(_chainEquivalence[caip2]).length == 0, ChainEquivalenceAlreadyRegistered(caip2));
5154
_chainEquivalence[caip2] = axelarSupported;
5255
_chainEquivalence[axelarSupported] = caip2;
5356
emit RegisteredChainEquivalence(caip2, axelarSupported);
5457
}
5558

5659
/// @dev Registers the CAIP-10 account address of the remote gateway for a given CAIP-2 chain identifier.
5760
function registerRemoteGateway(string calldata caip2, string calldata remoteGateway) public virtual onlyOwner {
58-
require(bytes(_remoteGateways[caip2]).length == 0, "Remote gateway already registered");
61+
require(bytes(_remoteGateways[caip2]).length == 0, RemoteGatewayAlreadyRegistered(caip2));
5962
_remoteGateways[caip2] = remoteGateway;
6063
emit RegisteredRemoteGateway(caip2, remoteGateway);
6164
}

contracts/crosschain/axelar/AxelarGatewayDestination.sol

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ abstract contract AxelarGatewayDestination is IERC7786GatewayDestinationPassive,
1818
using Strings for address;
1919
using Strings for string;
2020

21+
error InvalidOriginGateway(string sourceChain, string axelarSourceAddress);
22+
error ReceiverExecutionFailed();
23+
2124
/// @dev Sets a message as executed so it can't be executed again. Should be called by the receiver contract.
2225
function setMessageExecuted(
2326
bytes calldata messageKey,
@@ -76,7 +79,10 @@ abstract contract AxelarGatewayDestination is IERC7786GatewayDestinationPassive,
7679

7780
// check message validity
7881
// - `axelarSourceAddress` is the remote gateway on the origin chain.
79-
require(getRemoteGateway(sourceChain).equal(axelarSourceAddress), "Invalid origin gateway");
82+
require(
83+
getRemoteGateway(sourceChain).equal(axelarSourceAddress),
84+
InvalidOriginGateway(sourceChain, axelarSourceAddress)
85+
);
8086

8187
// Active mode
8288
bytes4 result = IERC7786Receiver(receiver.parseAddress()).executeMessage(
@@ -87,6 +93,6 @@ abstract contract AxelarGatewayDestination is IERC7786GatewayDestinationPassive,
8793
payload,
8894
attributes
8995
);
90-
require(result == IERC7786Receiver.executeMessage.selector, "Receiver execution failed");
96+
require(result == IERC7786Receiver.executeMessage.selector, ReceiverExecutionFailed());
9197
}
9298
}

contracts/crosschain/utils/draft-ERC7786Receiver.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// SPDX-License-Identifier: MIT
22

3-
pragma solidity ^0.8.0;
3+
pragma solidity ^0.8.27;
44

55
import {IERC7786GatewayDestinationPassive, IERC7786Receiver} from "../interfaces/draft-IERC7786.sol";
66

@@ -34,7 +34,7 @@ abstract contract ERC7786Receiver is IERC7786Receiver {
3434
// no extra check
3535
} else if (_isKnownGateway(gateway)) {
3636
// Passive mode
37-
if (msg.value != 0) revert ERC7786ReceivePassiveModeValue();
37+
require(msg.value == 0, ERC7786ReceivePassiveModeValue());
3838
IERC7786GatewayDestinationPassive(gateway).setMessageExecuted(
3939
gatewayMessageKey,
4040
source,

test/crosschain/axelar/AxelarGateway.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('AxelarGateway', function () {
6767
ethers.randomBytes(128),
6868
[],
6969
))
70-
.to.be.revertedWith('Receiver execution failed');
70+
.to.be.revertedWithCustomError(this.dstGateway, 'ReceiverExecutionFailed');
7171
});
7272

7373
it('invalid receiver - EOA', async function () {

0 commit comments

Comments
 (0)