diff --git a/src/interface/ICrosschain.sol b/src/interface/ICrosschain.sol deleted file mode 100644 index b8de6212..00000000 --- a/src/interface/ICrosschain.sol +++ /dev/null @@ -1,52 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -pragma solidity ^0.8.20; - -interface ICrosschain { - - /** - * @notice Sends a cross-chain transaction. - * @param _destinationChain The destination chain ID. - * @param _callAddress The address of the contract on the destination chain. - * @param _payload The payload to send to the destination chain. - * @param _extraArgs The extra arguments to pass - * @dev extraArgs may contain items such as token, amount, feeTokenAddress, receipient, gasLimit, etc - */ - function sendCrossChainTransaction( - uint64 _destinationChain, - address _callAddress, - bytes calldata _payload, - bytes calldata _extraArgs - ) external payable; - - /** - * @notice callback function for when a cross-chain transaction is sent. - * @param _destinationChain The destination chain ID. - * @param _callAddress The address of the contract on the destination chain. - * @param _payload The payload sent to the destination chain. - * @param _extraArgs The extra arguments sent to the callAddress on the destination chain. - */ - function onCrossChainTransactionSent( - uint64 _destinationChain, - address _callAddress, - bytes calldata _payload, - bytes calldata _extraArgs - ) internal; - - /** - * @notice callback function for when a cross-chain transaction is received. - * @param _sourceChain The source chain ID. - * @param _sourceAddress The address of the contract on the source chain. - * @param _payload The payload sent to the destination chain. - * @param _extraArgs The extra arguments sent to the callAddress on the destination chain. - */ - function onCrossChainTransactionReceived( - uint64 _sourceChain, - address _sourceAddress, - bytes calldata _payload, - bytes calldata _extraArgs - ) internal; - - function setRouter(address _router) external; - function getRouter() external view returns (address); - -} diff --git a/src/module/token/crosschain/chainlink.sol b/src/module/token/crosschain/chainlink.sol index cf190f5b..c1f7fee5 100644 --- a/src/module/token/crosschain/chainlink.sol +++ b/src/module/token/crosschain/chainlink.sol @@ -7,8 +7,10 @@ import {Role} from "../../../Role.sol"; import {IERC20} from "../../../interface/IERC20.sol"; import {IInstallationCallback} from "../../../interface/IInstallationCallback.sol"; +import {CrossChain} from "./CrossChain.sol"; import {OwnableRoles} from "@solady/auth/OwnableRoles.sol"; +import {CCIPReceiver} from "@chainlink/ccip/applications/CCIPReceiver.sol"; import {IRouterClient} from "@chainlink/ccip/interfaces/IRouterClient.sol"; import {Client} from "@chainlink/ccip/libraries/Client.sol"; @@ -33,10 +35,12 @@ library ChainlinkCrossChainStorage { } -contract ChainlinkCrossChain is Module { +contract ChainlinkCrossChain is Module, CrossChain, CCIPReceiver { error NotEnoughBalance(uint256 currentBalance, uint256 calculatedFees); + constructor(address _router, address _link) CCIPReceiver(_router) {} + /*////////////////////////////////////////////////////////////// MODULE CONFIG //////////////////////////////////////////////////////////////*/ @@ -85,7 +89,7 @@ contract ChainlinkCrossChain is Module { FALLBACK FUNCTIONS //////////////////////////////////////////////////////////////*/ - function getRouter() external view returns (address) { + function getRouter() public view override(CrossChain, CCIPReceiver) returns (address) { return _chainlinkCrossChainStorage().router; } @@ -93,7 +97,7 @@ contract ChainlinkCrossChain is Module { return _chainlinkCrossChainStorage().linkToken; } - function setRouter(address router) external { + function setRouter(address router) external override { _chainlinkCrossChainStorage().router = router; } @@ -104,13 +108,16 @@ contract ChainlinkCrossChain is Module { function sendCrossChainTransaction( uint64 _destinationChain, address _callAddress, - address _recipient, - address _token, - uint256 _amount, bytes calldata _data, bytes calldata _extraArgs - ) external { - (address _feeTokenAddress, bytes memory ccipMessageExtraArgs) = abi.decode(_extraArgs, (address, bytes)); + ) external payable override { + ( + address _recipient, + address _token, + uint256 _amount, + address _feeTokenAddress, + bytes memory ccipMessageExtraArgs + ) = abi.decode(_extraArgs, (address, address, uint256, address, bytes)); if (_feeTokenAddress == address(0)) { _sendMessagePayNative(_destinationChain, _recipient, _data, _token, _amount, ccipMessageExtraArgs); @@ -119,12 +126,32 @@ contract ChainlinkCrossChain is Module { _destinationChain, _recipient, _data, _token, _amount, _feeTokenAddress, ccipMessageExtraArgs ); } + + onCrossChainTransactionSent(_destinationChain, _callAddress, _data, _extraArgs); } /*////////////////////////////////////////////////////////////// INTERNAL FUNCTIONS //////////////////////////////////////////////////////////////*/ + function onCrossChainTransactionSent( + uint64 _destinationChain, + address _callAddress, + bytes calldata _payload, + bytes calldata _extraArgs + ) internal override { + /// post cross chain transaction sent logic goes here + } + + function onCrossChainTransactionReceived( + uint64 _sourceChain, + address _sourceAddress, + bytes memory _payload, + bytes memory _extraArgs + ) internal override { + /// post cross chain transaction received logic goes here + } + function _sendMessagePayToken( uint64 _destinationChain, address _recipient, @@ -190,6 +217,12 @@ contract ChainlinkCrossChain is Module { }); } + function _ccipReceive(Client.Any2EVMMessage memory message) internal override { + address sender = abi.decode(message.sender, (address)); + bytes memory payload = ""; + onCrossChainTransactionReceived(message.sourceChainSelector, sender, message.data, payload); + } + function _chainlinkCrossChainStorage() internal pure returns (ChainlinkCrossChainStorage.Data storage) { return ChainlinkCrossChainStorage.data(); }