|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 5 | +import {AxelarExecutable} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol"; |
| 6 | +import {IAxelarGasService} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol"; |
| 7 | +import { |
| 8 | + StringToAddress, AddressToString |
| 9 | +} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/libs/AddressString.sol"; |
| 10 | +import {IEndpointManagerStandalone} from |
| 11 | + "@wormhole-foundation/native_token_transfer/interfaces/IEndpointManagerStandalone.sol"; |
| 12 | +import {EndpointStandalone} from "@wormhole-foundation/native_token_transfer/EndpointStandalone.sol"; |
| 13 | +import {EndpointStructs} from "@wormhole-foundation/native_token_transfer/libraries/EndpointStructs.sol"; |
| 14 | +import {SetEmitterMessage} from "./Structs.sol"; |
| 15 | + |
| 16 | +contract AxelarEndpoint is EndpointStandalone, AxelarExecutable, Ownable { |
| 17 | + IAxelarGasService public immutable gasService; |
| 18 | + |
| 19 | + // These mappings are used to convert between chainId and chainName as Axelar accept chainName as string format |
| 20 | + mapping(uint16 => string) public idToAxelarChainIds; |
| 21 | + mapping(string => uint16) public axelarChainIdToId; |
| 22 | + |
| 23 | + error UnsupportedMessageType(); |
| 24 | + error InvalidSibling(uint16 chainId, bytes32 siblingAddress); |
| 25 | + error NotImplemented(); |
| 26 | + |
| 27 | + modifier onlySibling(string calldata sourceChain, string calldata sourceAddress) { |
| 28 | + uint16 chainId = axelarChainIdToId[sourceChain]; |
| 29 | + address _sourceAddress = StringToAddress.toAddress(sourceAddress); |
| 30 | + if (getSibling(chainId) != bytes32(uint256(uint160(_sourceAddress)))) { |
| 31 | + revert InvalidSibling(chainId, getSibling(chainId)); |
| 32 | + } |
| 33 | + _; |
| 34 | + } |
| 35 | + |
| 36 | + constructor(address _gateway, address _gasService, address _manager, address _owner) |
| 37 | + AxelarExecutable(_gateway) |
| 38 | + EndpointStandalone(_manager) |
| 39 | + Ownable(_owner) |
| 40 | + { |
| 41 | + gasService = IAxelarGasService(_gasService); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Set the bridge manager contract address |
| 46 | + * @param chainId The chainId of the chain. This is used to identify the chain in the EndpointManager. |
| 47 | + * @param chainName The chainName of the chain. This is used to identify the chain in the AxelarGateway. |
| 48 | + */ |
| 49 | + function setAxelarChainId(uint16 chainId, string calldata chainName) external onlyOwner { |
| 50 | + idToAxelarChainIds[chainId] = chainName; |
| 51 | + axelarChainIdToId[chainName] = chainId; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Revert if the message type is not supported |
| 56 | + */ |
| 57 | + function _handleMessage(bytes memory payload) internal returns (bool) { |
| 58 | + // Decode the payload as a EndpointManagerMessage |
| 59 | + EndpointStructs.EndpointManagerMessage memory message = EndpointStructs.parseEndpointManagerMessage(payload); |
| 60 | + |
| 61 | + // msgType 1: Send Token |
| 62 | + // msgType 2: Set Emitter (destination contract address) |
| 63 | + if (message.msgType == 1) { |
| 64 | + return false; |
| 65 | + } else { |
| 66 | + revert UnsupportedMessageType(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Send message to Axelar Gateway |
| 72 | + * @param recipientChain The chainId of the chain. This is used to identify the chain in the EndpointManager. |
| 73 | + * @param payload The payload of the message which is a NativeTokenTransfer |
| 74 | + */ |
| 75 | + function _sendMessage(uint16 recipientChain, bytes memory payload) internal virtual override { |
| 76 | + bool isInternalCall = _handleMessage(payload); |
| 77 | + |
| 78 | + if (isInternalCall) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + bytes32 destEmitter = getSibling(recipientChain); |
| 83 | + string memory destinationContract = AddressToString.toString(address(uint160(uint256(destEmitter)))); |
| 84 | + string memory destinationChain = idToAxelarChainIds[recipientChain]; |
| 85 | + |
| 86 | + gasService.payNativeGasForContractCall{value: msg.value}( |
| 87 | + address(this), destinationChain, destinationContract, payload, msg.sender |
| 88 | + ); |
| 89 | + |
| 90 | + gateway.callContract(destinationChain, destinationContract, payload); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Receive message from Axelar Gateway |
| 95 | + */ |
| 96 | + function _execute(string calldata sourceChain, string calldata sourceAddress, bytes calldata payload) |
| 97 | + internal |
| 98 | + override |
| 99 | + onlySibling(sourceChain, sourceAddress) |
| 100 | + { |
| 101 | + EndpointStructs.EndpointManagerMessage memory message = EndpointStructs.parseEndpointManagerMessage(payload); |
| 102 | + IEndpointManagerStandalone(_manager).attestationReceived(message); |
| 103 | + } |
| 104 | + |
| 105 | + function _verifyMessage(bytes memory encodedMessage) internal override returns (bytes memory) { |
| 106 | + revert NotImplemented(); |
| 107 | + } |
| 108 | + |
| 109 | + function _quoteDeliveryPrice(uint16 targetChain) |
| 110 | + internal |
| 111 | + view |
| 112 | + virtual |
| 113 | + override |
| 114 | + returns (uint256 nativePriceQuote) |
| 115 | + { |
| 116 | + // Axelar doesn't support on-chain gas fee. |
| 117 | + return 0; |
| 118 | + } |
| 119 | +} |
0 commit comments