|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.22; |
| 3 | + |
| 4 | +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; |
| 5 | +import { IERC20MintBurnExtension } from "./interfaces/IERC20MintBurnExtension.sol"; |
| 6 | +import { IOVault } from "./interfaces/IOVault.sol"; |
| 7 | +import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; |
| 8 | + |
| 9 | +import { IOFT, SendParam, MessagingFee } from "@layerzerolabs/oft-evm/contracts/interfaces/IOFT.sol"; |
| 10 | +import { IOAppCore } from "@layerzerolabs/oapp-evm/contracts/oapp/interfaces/IOAppCore.sol"; |
| 11 | +import { OFTComposeMsgCodec } from "@layerzerolabs/oft-evm/contracts/libs/OFTComposeMsgCodec.sol"; |
| 12 | + |
| 13 | +import { IOVaultComposer, FailedMessage, FailedState } from "./interfaces/IOVaultComposer.sol"; |
| 14 | +import { IOVault } from "./interfaces/IOVault.sol"; |
| 15 | +import { IERC4626Adapter } from "./interfaces/IERC4626Adapter.sol"; |
| 16 | + |
| 17 | +contract OVaultComposer is IOVaultComposer, ReentrancyGuard { |
| 18 | + using OFTComposeMsgCodec for bytes; |
| 19 | + using OFTComposeMsgCodec for bytes32; |
| 20 | + |
| 21 | + address public immutable ASSET_OFT; |
| 22 | + address public immutable SHARE_OFT; |
| 23 | + address public immutable OVAULT; |
| 24 | + address public immutable ENDPOINT; |
| 25 | + |
| 26 | + mapping(bytes32 guid => FailedMessage) public failedMessages; |
| 27 | + |
| 28 | + constructor(address _ovault) { |
| 29 | + address share = IERC4626Adapter(_ovault).share(); |
| 30 | + address asset = IERC4626Adapter(_ovault).asset(); |
| 31 | + if (!IERC20MintBurnExtension(share).ERC4626AdapterCompliant()) { |
| 32 | + revert IOVault.ShareNotERC4626AdapterCompliant(); |
| 33 | + } |
| 34 | + |
| 35 | + OVAULT = _ovault; |
| 36 | + SHARE_OFT = IOVault(_ovault).SHARE_OFT(); |
| 37 | + ASSET_OFT = IOVault(_ovault).ASSET_OFT(); |
| 38 | + ENDPOINT = address(IOAppCore(ASSET_OFT).endpoint()); |
| 39 | + |
| 40 | + // Approve the adapter to spend the share tokens held by this contract |
| 41 | + IERC20(share).approve(OVAULT, type(uint256).max); |
| 42 | + IERC20(asset).approve(OVAULT, type(uint256).max); |
| 43 | + } |
| 44 | + |
| 45 | + function lzCompose( |
| 46 | + address _refundOFT, |
| 47 | + bytes32 _guid, |
| 48 | + bytes calldata _message, |
| 49 | + address /*_executor*/, |
| 50 | + bytes calldata /*_extraData*/ |
| 51 | + ) external payable virtual override { |
| 52 | + if (msg.sender != ENDPOINT) revert OnlyEndpoint(msg.sender); |
| 53 | + if (_refundOFT != ASSET_OFT && _refundOFT != SHARE_OFT) revert OnlyOFT(_refundOFT); |
| 54 | + |
| 55 | + /// @dev Route to the correct target OFT |
| 56 | + address oft = _refundOFT == ASSET_OFT ? SHARE_OFT : ASSET_OFT; |
| 57 | + |
| 58 | + /// @dev Extracted from the _message header. Will always be part of the _message since it is created by lzReceive |
| 59 | + uint256 amount = OFTComposeMsgCodec.amountLD(_message); |
| 60 | + bytes memory sendParamEncoded = OFTComposeMsgCodec.composeMsg(_message); |
| 61 | + |
| 62 | + SendParam memory refundSendParam; |
| 63 | + refundSendParam.dstEid = OFTComposeMsgCodec.srcEid(_message); |
| 64 | + refundSendParam.to = OFTComposeMsgCodec.composeFrom(_message); |
| 65 | + refundSendParam.amountLD = amount; |
| 66 | + |
| 67 | + SendParam memory sendParam; |
| 68 | + |
| 69 | + /// @dev Try decoding the composeMsg as a SendParam |
| 70 | + try this.decodeSendParam(sendParamEncoded) returns (SendParam memory sendParamDecoded) { |
| 71 | + /// @dev In the case of a valid decode we have the raw SendParam to be forwarded to the target OFT (oft) |
| 72 | + sendParam = sendParamDecoded; |
| 73 | + sendParam.amountLD = 0; |
| 74 | + } catch { |
| 75 | + /// @dev In the case of a failed decode we store the failed message and emit an event. |
| 76 | + /// @dev This message can only be refunded back to the source chain. |
| 77 | + failedMessages[_guid] = FailedMessage(address(0), sendParam, _refundOFT, refundSendParam); |
| 78 | + emit DecodeFailed(_guid, _refundOFT, sendParamEncoded); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + /// @dev Try to early catch issues surrounding LayerZero config. This quoteSend catches issues like: invalid peer, dvn config, etc. |
| 83 | + try this.validateTargetOFTConfig(oft, sendParam) {} catch (bytes memory errMsg) { |
| 84 | + /// @dev When erroring out we want to NOT make a swap and the user can only go back to the source chain. |
| 85 | + failedMessages[_guid] = FailedMessage(address(0), sendParam, _refundOFT, refundSendParam); |
| 86 | + emit GenericError(_guid, oft, errMsg); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + /// @dev Try to execute the action on the target OFT. If we hit an issue then it rolls back the storage changes. |
| 91 | + try this.executeOVaultAction(_refundOFT, amount, sendParam) returns (uint256 vaultAmount) { |
| 92 | + sendParam.amountLD = vaultAmount; |
| 93 | + } catch (bytes memory errMsg) { |
| 94 | + failedMessages[_guid] = FailedMessage(oft, sendParam, _refundOFT, refundSendParam); |
| 95 | + emit GenericError(_guid, oft, errMsg); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + /// @dev Try sending the message to the target OFT |
| 100 | + try this.send{ value: msg.value }(oft, sendParam) { |
| 101 | + emit Sent(_guid, oft); |
| 102 | + } catch { |
| 103 | + /// @dev A failed send can happen due to not enough msg.value |
| 104 | + /// @dev Since we have the target tokens in the composer, we can retry with more gas. |
| 105 | + failedMessages[_guid] = FailedMessage(oft, sendParam, address(0), refundSendParam); |
| 106 | + emit SendFailed(_guid, oft); |
| 107 | + return; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// @dev External call for try...catch logic in lzCompose() |
| 112 | + function decodeSendParam(bytes calldata sendParamBytes) external pure returns (SendParam memory sendParam) { |
| 113 | + sendParam = abi.decode(sendParamBytes, (SendParam)); |
| 114 | + } |
| 115 | + |
| 116 | + /// @dev External call for try...catch logic in lzCompose() |
| 117 | + function executeOVaultAction( |
| 118 | + address _oft, |
| 119 | + uint256 _amount, |
| 120 | + SendParam calldata _sendParam |
| 121 | + ) external nonReentrant returns (uint256 vaultAmount) { |
| 122 | + if (msg.sender != address(this)) revert OnlySelf(msg.sender); |
| 123 | + vaultAmount = _executeOVaultAction(_oft, _amount); |
| 124 | + if (vaultAmount < _sendParam.minAmountLD) { |
| 125 | + /// @dev Will rollback on this function's storage changes (trade does not happen) |
| 126 | + revert NotEnoughTargetTokens(vaultAmount, _sendParam.minAmountLD); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /// @dev Dirty swapping amountLD and minAmountLD to 1e18 and 0 to avoid Slippage issue on the target OFT quoteSend() |
| 131 | + function validateTargetOFTConfig(address _oft, SendParam memory _sendParam) external view { |
| 132 | + _sendParam.amountLD = 1e18; |
| 133 | + _sendParam.minAmountLD = 0; |
| 134 | + |
| 135 | + IOFT(_oft).quoteSend(_sendParam, false); |
| 136 | + } |
| 137 | + |
| 138 | + /// @dev External call for try...catch logic in lzCompose() |
| 139 | + function send(address _oft, SendParam calldata _sendParam) external payable nonReentrant { |
| 140 | + if (msg.sender != address(this)) revert OnlySelf(msg.sender); |
| 141 | + _send(_oft, _sendParam); |
| 142 | + } |
| 143 | + |
| 144 | + /// @dev Permissionless function to send back the message to the source chain |
| 145 | + /// @dev Always possible unless the lzCompose() fails due to an Out-Of-Gas panic |
| 146 | + function refund(bytes32 _guid, bytes calldata _extraOptions) external payable nonReentrant { |
| 147 | + FailedMessage memory failedMessage = failedMessages[_guid]; |
| 148 | + SendParam memory refundSendParam = failedMessage.sendParam; |
| 149 | + if (failedGuidState(_guid) != FailedState.CanOnlyRefund) revert CanNotRefund(_guid); |
| 150 | + |
| 151 | + refundSendParam.extraOptions = _extraOptions; |
| 152 | + |
| 153 | + delete failedMessages[_guid]; |
| 154 | + _send(failedMessage.refundOFT, refundSendParam); |
| 155 | + emit Refunded(_guid, failedMessage.refundOFT); |
| 156 | + } |
| 157 | + |
| 158 | + /// @dev Permissionless function to retry the message with more gas |
| 159 | + /// @dev Probabilistically possible if the OFT.send() fails - ex: invalid peer |
| 160 | + function retry(bytes32 _guid, bytes calldata _extraOptions) external payable nonReentrant { |
| 161 | + FailedMessage memory failedMessage = failedMessages[_guid]; |
| 162 | + if (failedGuidState(_guid) != FailedState.CanOnlyRetry) revert CanNotRetry(_guid); |
| 163 | + |
| 164 | + SendParam memory sendParam = failedMessage.sendParam; |
| 165 | + |
| 166 | + sendParam.extraOptions = _extraOptions; |
| 167 | + |
| 168 | + delete failedMessages[_guid]; |
| 169 | + _send(failedMessage.oft, sendParam); |
| 170 | + emit Retried(_guid, failedMessage.oft); |
| 171 | + } |
| 172 | + |
| 173 | + /// @dev Retry mechanism for transactions that failed due to slippage. This can revert. |
| 174 | + function retryWithSwap(bytes32 _guid, bytes calldata _extraOptions) external payable { |
| 175 | + FailedMessage memory failedMessage = failedMessages[_guid]; |
| 176 | + if (failedGuidState(_guid) != FailedState.CanRetryWithSwap) revert CanNotRetry(_guid); |
| 177 | + |
| 178 | + SendParam memory sendParam = failedMessage.sendParam; |
| 179 | + sendParam.extraOptions = _extraOptions; |
| 180 | + |
| 181 | + uint256 amountLd = failedMessage.refundSendParam.amountLD; |
| 182 | + |
| 183 | + delete failedMessages[_guid]; |
| 184 | + sendParam.amountLD = _executeOVaultAction(failedMessage.refundOFT, amountLd); |
| 185 | + |
| 186 | + _send(failedMessage.oft, sendParam); |
| 187 | + emit Sent(_guid, failedMessage.oft); |
| 188 | + } |
| 189 | + |
| 190 | + /// @dev Internal function to send the message to the target OFT |
| 191 | + function _send(address _oft, SendParam memory _sendParam) internal { |
| 192 | + IOFT(_oft).send{ value: msg.value }(_sendParam, MessagingFee(msg.value, 0), tx.origin); |
| 193 | + } |
| 194 | + |
| 195 | + function _executeOVaultAction(address _oft, uint256 _amount) internal returns (uint256 vaultAmount) { |
| 196 | + if (_oft == ASSET_OFT) { |
| 197 | + vaultAmount = IERC4626Adapter(OVAULT).deposit(_amount, address(this)); |
| 198 | + } else { |
| 199 | + vaultAmount = IERC4626Adapter(OVAULT).redeem(_amount, address(this), address(this)); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + /// @dev Helper to view the state of a failed message |
| 204 | + function failedGuidState(bytes32 _guid) public view returns (FailedState) { |
| 205 | + FailedMessage memory failedMessage = failedMessages[_guid]; |
| 206 | + |
| 207 | + if (failedMessage.refundOFT == address(0) && failedMessage.oft == address(0)) { |
| 208 | + return FailedState.NotFound; |
| 209 | + } |
| 210 | + if (failedMessage.refundOFT != address(0) && failedMessage.oft == address(0)) { |
| 211 | + return FailedState.CanOnlyRefund; |
| 212 | + } |
| 213 | + if (failedMessage.refundOFT == address(0) && failedMessage.oft != address(0)) { |
| 214 | + return FailedState.CanOnlyRetry; |
| 215 | + } |
| 216 | + |
| 217 | + return FailedState.CanRetryWithSwap; |
| 218 | + } |
| 219 | + receive() external payable {} |
| 220 | +} |
0 commit comments