|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import { IMessageTransmitter, ITokenMessenger } from "../external/interfaces/CCTPInterfaces.sol"; |
| 5 | +import { SpokePoolInterface } from "../interfaces/SpokePoolInterface.sol"; |
| 6 | +import { AdapterInterface } from "./interfaces/AdapterInterface.sol"; |
| 7 | +import { CircleCCTPAdapter, CircleDomainIds } from "../libraries/CircleCCTPAdapter.sol"; |
| 8 | +import { Bytes32ToAddress } from "../libraries/AddressConverters.sol"; |
| 9 | + |
| 10 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @notice Contract containing logic to send messages from L1 to Solana via CCTP. |
| 14 | + * @dev Public functions calling external contracts do not guard against reentrancy because they are expected to be |
| 15 | + * called via delegatecall, which will execute this contract's logic within the context of the originating contract. |
| 16 | + * For example, the HubPool will delegatecall these functions, therefore it's only necessary that the HubPool's methods |
| 17 | + * that call this contract's logic guard against reentrancy. |
| 18 | + * @custom:security-contact [email protected] |
| 19 | + */ |
| 20 | + |
| 21 | +// solhint-disable-next-line contract-name-camelcase |
| 22 | +contract Solana_Adapter is AdapterInterface, CircleCCTPAdapter { |
| 23 | + /** |
| 24 | + * @notice We use Bytes32ToAddress library to map a Solana address to an Ethereum address representation. |
| 25 | + * @dev The Ethereum address is derived from the Solana address by truncating it to its lowest 20 bytes. This same |
| 26 | + * conversion must be done by the HubPool owner when adding Solana spoke pool and setting the corresponding pool |
| 27 | + * rebalance and deposit routes. |
| 28 | + */ |
| 29 | + using Bytes32ToAddress for bytes32; |
| 30 | + |
| 31 | + /** |
| 32 | + * @notice The official Circle CCTP MessageTransmitter contract endpoint. |
| 33 | + * @dev Posted officially here: https://developers.circle.com/stablecoins/docs/evm-smart-contracts |
| 34 | + */ |
| 35 | + // solhint-disable-next-line immutable-vars-naming |
| 36 | + IMessageTransmitter public immutable cctpMessageTransmitter; |
| 37 | + |
| 38 | + // Solana spoke pool address, decoded from Base58 to bytes32. |
| 39 | + bytes32 public immutable SOLANA_SPOKE_POOL_BYTES32; |
| 40 | + |
| 41 | + // Solana spoke pool address, mapped to its EVM address representation. |
| 42 | + address public immutable SOLANA_SPOKE_POOL_ADDRESS; |
| 43 | + |
| 44 | + // USDC mint address on Solana, decoded from Base58 to bytes32. |
| 45 | + bytes32 public immutable SOLANA_USDC_BYTES32; |
| 46 | + |
| 47 | + // USDC mint address on Solana, mapped to its EVM address representation. |
| 48 | + address public immutable SOLANA_USDC_ADDRESS; |
| 49 | + |
| 50 | + // USDC token address on Solana for the spoke pool (vault ATA), decoded from Base58 to bytes32. |
| 51 | + bytes32 public immutable SOLANA_SPOKE_POOL_USDC_VAULT; |
| 52 | + |
| 53 | + // Custom errors for constructor argument validation. |
| 54 | + error InvalidCctpTokenMessenger(address tokenMessenger); |
| 55 | + error InvalidCctpMessageTransmitter(address messageTransmitter); |
| 56 | + |
| 57 | + // Custom errors for relayMessage validation. |
| 58 | + error InvalidRelayMessageTarget(address target); |
| 59 | + error InvalidOriginToken(address originToken); |
| 60 | + error InvalidDestinationChainId(uint256 destinationChainId); |
| 61 | + |
| 62 | + // Custom errors for relayTokens validation. |
| 63 | + error InvalidL1Token(address l1Token); |
| 64 | + error InvalidL2Token(address l2Token); |
| 65 | + error InvalidAmount(uint256 amount); |
| 66 | + error InvalidTokenRecipient(address to); |
| 67 | + |
| 68 | + /** |
| 69 | + * @notice Constructs new Adapter. |
| 70 | + * @param _l1Usdc USDC address on L1. |
| 71 | + * @param _cctpTokenMessenger TokenMessenger contract to bridge tokens via CCTP. |
| 72 | + * @param _cctpMessageTransmitter MessageTransmitter contract to bridge messages via CCTP. |
| 73 | + * @param solanaSpokePool Solana spoke pool address, decoded from Base58 to bytes32. |
| 74 | + * @param solanaUsdc USDC mint address on Solana, decoded from Base58 to bytes32. |
| 75 | + * @param solanaSpokePoolUsdcVault USDC token address on Solana for the spoke pool, decoded from Base58 to bytes32. |
| 76 | + */ |
| 77 | + constructor( |
| 78 | + IERC20 _l1Usdc, |
| 79 | + ITokenMessenger _cctpTokenMessenger, |
| 80 | + IMessageTransmitter _cctpMessageTransmitter, |
| 81 | + bytes32 solanaSpokePool, |
| 82 | + bytes32 solanaUsdc, |
| 83 | + bytes32 solanaSpokePoolUsdcVault |
| 84 | + ) CircleCCTPAdapter(_l1Usdc, _cctpTokenMessenger, CircleDomainIds.Solana) { |
| 85 | + // Solana adapter requires CCTP TokenMessenger and MessageTransmitter contracts to be set. |
| 86 | + if (address(_cctpTokenMessenger) == address(0)) { |
| 87 | + revert InvalidCctpTokenMessenger(address(_cctpTokenMessenger)); |
| 88 | + } |
| 89 | + if (address(_cctpMessageTransmitter) == address(0)) { |
| 90 | + revert InvalidCctpMessageTransmitter(address(_cctpMessageTransmitter)); |
| 91 | + } |
| 92 | + |
| 93 | + cctpMessageTransmitter = _cctpMessageTransmitter; |
| 94 | + |
| 95 | + SOLANA_SPOKE_POOL_BYTES32 = solanaSpokePool; |
| 96 | + SOLANA_SPOKE_POOL_ADDRESS = solanaSpokePool.toAddressUnchecked(); |
| 97 | + |
| 98 | + SOLANA_USDC_BYTES32 = solanaUsdc; |
| 99 | + SOLANA_USDC_ADDRESS = solanaUsdc.toAddressUnchecked(); |
| 100 | + |
| 101 | + SOLANA_SPOKE_POOL_USDC_VAULT = solanaSpokePoolUsdcVault; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @notice Send cross-chain message to target on Solana. |
| 106 | + * @dev Only allows sending messages to the Solana spoke pool. |
| 107 | + * @param target Program on Solana (translated as EVM address) that will receive message. |
| 108 | + * @param message Data to send to target. |
| 109 | + */ |
| 110 | + function relayMessage(address target, bytes calldata message) external payable override { |
| 111 | + if (target != SOLANA_SPOKE_POOL_ADDRESS) { |
| 112 | + revert InvalidRelayMessageTarget(target); |
| 113 | + } |
| 114 | + |
| 115 | + bytes4 selector = bytes4(message[:4]); |
| 116 | + if (selector == SpokePoolInterface.setEnableRoute.selector) { |
| 117 | + cctpMessageTransmitter.sendMessage( |
| 118 | + CircleDomainIds.Solana, |
| 119 | + SOLANA_SPOKE_POOL_BYTES32, |
| 120 | + _translateSetEnableRoute(message) |
| 121 | + ); |
| 122 | + } else { |
| 123 | + cctpMessageTransmitter.sendMessage(CircleDomainIds.Solana, SOLANA_SPOKE_POOL_BYTES32, message); |
| 124 | + } |
| 125 | + |
| 126 | + // TODO: consider if we need also to emit the translated message. |
| 127 | + emit MessageRelayed(target, message); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @notice Bridge tokens to Solana. |
| 132 | + * @dev Only allows bridging USDC to Solana spoke pool. |
| 133 | + * @param l1Token L1 token to deposit. |
| 134 | + * @param l2Token L2 token to receive. |
| 135 | + * @param amount Amount of L1 tokens to deposit and L2 tokens to receive. |
| 136 | + * @param to Bridge recipient. |
| 137 | + */ |
| 138 | + function relayTokens( |
| 139 | + address l1Token, |
| 140 | + address l2Token, |
| 141 | + uint256 amount, |
| 142 | + address to |
| 143 | + ) external payable override { |
| 144 | + if (l1Token != address(usdcToken)) { |
| 145 | + revert InvalidL1Token(l1Token); |
| 146 | + } |
| 147 | + if (l2Token != SOLANA_USDC_ADDRESS) { |
| 148 | + revert InvalidL2Token(l2Token); |
| 149 | + } |
| 150 | + if (amount > type(uint64).max) { |
| 151 | + revert InvalidAmount(amount); |
| 152 | + } |
| 153 | + if (to != SOLANA_SPOKE_POOL_ADDRESS) { |
| 154 | + revert InvalidTokenRecipient(to); |
| 155 | + } |
| 156 | + |
| 157 | + _transferUsdc(SOLANA_SPOKE_POOL_USDC_VAULT, amount); |
| 158 | + |
| 159 | + // TODO: consider if we need also to emit the translated addresses. |
| 160 | + emit TokensRelayed(l1Token, l2Token, amount, to); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @notice Translates a message to enable/disable a route on Solana spoke pool. |
| 165 | + * @param message Message to translate, expecting setEnableRoute(address,uint256,bool). |
| 166 | + * @return Translated message, using setEnableRoute(bytes32,uint64,bool). |
| 167 | + */ |
| 168 | + function _translateSetEnableRoute(bytes calldata message) internal view returns (bytes memory) { |
| 169 | + (address originToken, uint256 destinationChainId, bool enable) = abi.decode( |
| 170 | + message[4:], |
| 171 | + (address, uint256, bool) |
| 172 | + ); |
| 173 | + |
| 174 | + if (originToken != SOLANA_USDC_ADDRESS) { |
| 175 | + revert InvalidOriginToken(originToken); |
| 176 | + } |
| 177 | + |
| 178 | + if (destinationChainId > type(uint64).max) { |
| 179 | + revert InvalidDestinationChainId(destinationChainId); |
| 180 | + } |
| 181 | + |
| 182 | + return |
| 183 | + abi.encodeWithSignature( |
| 184 | + "setEnableRoute(bytes32,uint64,bool)", |
| 185 | + SOLANA_USDC_BYTES32, |
| 186 | + uint64(destinationChainId), |
| 187 | + enable |
| 188 | + ); |
| 189 | + } |
| 190 | +} |
0 commit comments