|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.18; |
| 3 | + |
| 4 | +import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; |
| 5 | +import { IOFT } from "./interfaces/IOFT.sol"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @title MessengerTypes |
| 9 | + * @notice Library containing messenger type constants |
| 10 | + * @custom:security-contact [email protected] |
| 11 | + */ |
| 12 | +library MessengerTypes { |
| 13 | + /** @notice Identifier for OFT (Omni-chain Fungible Token by LayerZero) messenger type */ |
| 14 | + bytes32 public constant OFT_MESSENGER = bytes32("OFT_MESSENGER"); |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * @dev A helper contract for chain adapters on the hub chain that support OFT messaging. Handles |
| 19 | + * @dev token => messenger mapping storage. Adapters can't store this themselves as they're called |
| 20 | + * @dev via `delegateCall` and their storage is not part of available context. |
| 21 | + * @custom:security-contact [email protected] |
| 22 | + */ |
| 23 | +contract AdapterStore is Ownable { |
| 24 | + /** @notice Maps messenger type and destination domain to token-messenger pairs */ |
| 25 | + mapping(bytes32 messengerType => mapping(uint256 dstDomainId => mapping(address srcChainToken => address messengerAddress))) |
| 26 | + public crossChainMessengers; |
| 27 | + |
| 28 | + /** |
| 29 | + * @notice Emitted when a messenger is set for a specific token and destination |
| 30 | + * @param messengerType Type of messenger being set |
| 31 | + * @param dstDomainId Destination domain ID |
| 32 | + * @param srcChainToken Source chain token address |
| 33 | + * @param srcChainMessenger Source chain messenger address |
| 34 | + */ |
| 35 | + event MessengerSet( |
| 36 | + bytes32 indexed messengerType, |
| 37 | + uint256 indexed dstDomainId, |
| 38 | + address indexed srcChainToken, |
| 39 | + address srcChainMessenger |
| 40 | + ); |
| 41 | + |
| 42 | + /** @notice Thrown when array lengths don't match in batch operations */ |
| 43 | + error ArrayLengthMismatch(); |
| 44 | + |
| 45 | + /** @notice Thrown when IOFT messenger's token doesn't match expected token */ |
| 46 | + error IOFTTokenMismatch(); |
| 47 | + |
| 48 | + /** @notice Thrown when messenger type is not supported */ |
| 49 | + error NonExistentMessengerType(); |
| 50 | + |
| 51 | + /** |
| 52 | + * @notice Sets a messenger for a specific token and destination domain |
| 53 | + * @param messengerType Type of messenger to set |
| 54 | + * @param dstDomainId Destination domain ID |
| 55 | + * @param srcChainToken Source chain token address |
| 56 | + * @param srcChainMessenger Source chain messenger address |
| 57 | + */ |
| 58 | + function setMessenger( |
| 59 | + bytes32 messengerType, |
| 60 | + uint256 dstDomainId, |
| 61 | + address srcChainToken, |
| 62 | + address srcChainMessenger |
| 63 | + ) external onlyOwner { |
| 64 | + _setMessenger(messengerType, dstDomainId, srcChainToken, srcChainMessenger); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @notice Sets multiple messengers in a single transaction |
| 69 | + * @param messengerTypes Array of messenger types |
| 70 | + * @param dstDomainIds Array of destination domain IDs |
| 71 | + * @param srcChainTokens Array of source chain token addresses |
| 72 | + * @param srcChainMessengers Array of source chain messenger addresses |
| 73 | + */ |
| 74 | + function batchSetMessengers( |
| 75 | + bytes32[] calldata messengerTypes, |
| 76 | + uint256[] calldata dstDomainIds, |
| 77 | + address[] calldata srcChainTokens, |
| 78 | + address[] calldata srcChainMessengers |
| 79 | + ) external onlyOwner { |
| 80 | + if ( |
| 81 | + messengerTypes.length != dstDomainIds.length || |
| 82 | + messengerTypes.length != srcChainTokens.length || |
| 83 | + messengerTypes.length != srcChainMessengers.length |
| 84 | + ) { |
| 85 | + revert ArrayLengthMismatch(); |
| 86 | + } |
| 87 | + |
| 88 | + for (uint256 i = 0; i < dstDomainIds.length; i++) { |
| 89 | + _setMessenger(messengerTypes[i], dstDomainIds[i], srcChainTokens[i], srcChainMessengers[i]); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @notice Internal function to set a messenger with validation |
| 95 | + * @param _messengerType Type of messenger to set |
| 96 | + * @param _dstDomainId Destination domain ID |
| 97 | + * @param _srcChainToken Source chain token address |
| 98 | + * @param _srcChainMessenger Source chain messenger address |
| 99 | + */ |
| 100 | + function _setMessenger( |
| 101 | + bytes32 _messengerType, |
| 102 | + uint256 _dstDomainId, |
| 103 | + address _srcChainToken, |
| 104 | + address _srcChainMessenger |
| 105 | + ) internal { |
| 106 | + // @dev Always allow zero-messenger to be set: this can be used to 'remove' a stored token <> messenger relationship |
| 107 | + if (_srcChainMessenger != address(0)) { |
| 108 | + if (_messengerType == MessengerTypes.OFT_MESSENGER) { |
| 109 | + // @dev Protect against human error: check that IOFT messenger's token matches the expected one |
| 110 | + if (IOFT(_srcChainMessenger).token() != _srcChainToken) { |
| 111 | + revert IOFTTokenMismatch(); |
| 112 | + } |
| 113 | + } else { |
| 114 | + revert NonExistentMessengerType(); |
| 115 | + } |
| 116 | + } |
| 117 | + crossChainMessengers[_messengerType][_dstDomainId][_srcChainToken] = _srcChainMessenger; |
| 118 | + emit MessengerSet(_messengerType, _dstDomainId, _srcChainToken, _srcChainMessenger); |
| 119 | + } |
| 120 | +} |
0 commit comments