|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.27; |
| 4 | + |
| 5 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 6 | +import {IAxelarGateway} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @dev Base implementation of a cross-chain gateway adapter for the Axelar Network. |
| 10 | + * |
| 11 | + * This contract allows developers to register equivalence between chains (i.e. CAIP-2 chain identifiers |
| 12 | + * to Axelar chain identifiers) and remote gateways (i.e. gateways on other chains) to |
| 13 | + * facilitate cross-chain communication. |
| 14 | + */ |
| 15 | +abstract contract AxelarGatewayBase is Ownable { |
| 16 | + /// @dev A remote gateway has been registered for a chain. |
| 17 | + event RegisteredRemoteGateway(string caip2, string gatewayAddress); |
| 18 | + |
| 19 | + /// @dev A chain equivalence has been registered. |
| 20 | + event RegisteredChainEquivalence(string caip2, string destinationChain); |
| 21 | + |
| 22 | + /// @dev Error emitted when an unsupported chain is queried. |
| 23 | + error UnsupportedChain(string caip2); |
| 24 | + |
| 25 | + error ChainEquivalenceAlreadyRegistered(string caip2); |
| 26 | + error RemoteGatewayAlreadyRegistered(string caip2); |
| 27 | + |
| 28 | + /// @dev Axelar's official gateway for the current chain. |
| 29 | + IAxelarGateway public immutable localGateway; |
| 30 | + |
| 31 | + mapping(string caip2 => string remoteGateway) private _remoteGateways; |
| 32 | + mapping(string caip2OrAxelar => string axelarOrCaip2) private _chainEquivalence; |
| 33 | + |
| 34 | + /// @dev Sets the local gateway address (i.e. Axelar's official gateway for the current chain). |
| 35 | + constructor(IAxelarGateway _gateway) { |
| 36 | + localGateway = _gateway; |
| 37 | + } |
| 38 | + |
| 39 | + /// @dev Returns the equivalent chain given an id that can be either CAIP-2 or an Axelar network identifier. |
| 40 | + function getEquivalentChain(string memory input) public view virtual returns (string memory output) { |
| 41 | + output = _chainEquivalence[input]; |
| 42 | + require(bytes(output).length > 0, UnsupportedChain(input)); |
| 43 | + } |
| 44 | + |
| 45 | + /// @dev Returns the address string of the remote gateway for a given CAIP-2 chain identifier. |
| 46 | + function getRemoteGateway(string memory caip2) public view virtual returns (string memory remoteGateway) { |
| 47 | + remoteGateway = _remoteGateways[caip2]; |
| 48 | + require(bytes(remoteGateway).length > 0, UnsupportedChain(caip2)); |
| 49 | + } |
| 50 | + |
| 51 | + /// @dev Registers a chain equivalence between a CAIP-2 chain identifier and an Axelar network identifier. |
| 52 | + function registerChainEquivalence(string calldata caip2, string calldata axelarSupported) public virtual onlyOwner { |
| 53 | + require(bytes(_chainEquivalence[caip2]).length == 0, ChainEquivalenceAlreadyRegistered(caip2)); |
| 54 | + _chainEquivalence[caip2] = axelarSupported; |
| 55 | + _chainEquivalence[axelarSupported] = caip2; |
| 56 | + emit RegisteredChainEquivalence(caip2, axelarSupported); |
| 57 | + } |
| 58 | + |
| 59 | + /// @dev Registers the address string of the remote gateway for a given CAIP-2 chain identifier. |
| 60 | + function registerRemoteGateway(string calldata caip2, string calldata remoteGateway) public virtual onlyOwner { |
| 61 | + require(bytes(_remoteGateways[caip2]).length == 0, RemoteGatewayAlreadyRegistered(caip2)); |
| 62 | + _remoteGateways[caip2] = remoteGateway; |
| 63 | + emit RegisteredRemoteGateway(caip2, remoteGateway); |
| 64 | + } |
| 65 | +} |
0 commit comments