all files / contracts/ RouterCrossTalkUpgradeable.sol

91.43% Statements 32/35
50% Branches 5/10
86.36% Functions 19/22
92.5% Lines 37/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256                                                              14×       14×       12×       12×                                                 36×   36×                                                                                                                                                                                                                                                                     12× 12×                                                
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeable.sol";
 
import "./interfaces/iGenericHandler.sol";
import "./interfaces/iRouterCrossTalkUpgradeable.sol";
 
/// @title RouterCrossTalkUpgradeable contract
/// @author Router Protocol
abstract contract RouterCrossTalkUpgradeable is
    Initializable,
    ContextUpgradeable,
    iRouterCrossTalkUpgradeable,
    ERC165Upgradeable
{
    using SafeERC20Upgradeable for IERC20Upgradeable;
    iGenericHandler private handler;
 
    address private linkSetter;
 
    address private feeToken;
 
    mapping(uint8 => address) private Chain2Addr; // CHain ID to Address
 
    mapping(bytes32 => ExecutesStruct) private executes;
 
    modifier isHandler() {
        Erequire(
            _msgSender() == address(handler),
            "RouterSync : Only GenericHandler can call this function"
        );
        _;
    }
 
    modifier isLinkUnSet(uint8 _chainID) {
        Erequire(
            Chain2Addr[_chainID] == address(0),
            "RouterSync : Cross Chain Contract to Chain ID not set"
        );
        _;
    }
 
    modifier isLinkSet(uint8 _chainID) {
        Erequire(
            Chain2Addr[_chainID] != address(0),
            "RouterCrossTalk : Cross Chain Contract to Chain ID already set"
        );
        _;
    }
 
    modifier isLinkSync(uint8 _srcChainID, address _srcAddress) {
        Erequire(
            Chain2Addr[_srcChainID] == _srcAddress,
            "RouterSync : Source Address Not linked"
        );
        _;
    }
 
    modifier isSelf() {
        Erequire(
            _msgSender() == address(this),
            "RouterCrossTalk : Can only be called by Current Contract"
        );
        _;
    }
 
    function __RouterCrossTalkUpgradeable_init(address _handler)
        internal
        initializer
    {
        __Context_init_unchained();
 
        handler = iGenericHandler(_handler);
    }
 
    function __RouterCrossTalkUpgradeable_init_unchained(address _handler)
        internal
        initializer
    {
        handler = iGenericHandler(_handler);
    }
 
    ///  @notice Used to set linker address, this function is internal and can only be set by contract owner or admins
    /// @param _addr Address of linker.
    function setLink(address _addr) internal {
        linkSetter = _addr;
    }
 
    /// @notice Used to set fee Token address, this function is internal and can only be set by contract owner or admins
    /// @param _addr Address of linker.
    function setFeeToken(address _addr) internal {
        feeToken = _addr;
    }
 
    function fetchHandler() external view override returns (address) {
        return address(handler);
    }
 
    function fetchLinkSetter() external view override returns (address) {
        return linkSetter;
    }
 
    function fetchLink(uint8 _chainID)
        external
        view
        override
        returns (address)
    {
        return Chain2Addr[_chainID];
    }
 
    function fetchFeeToken() external view override returns (address) {
        return feeToken;
    }
 
    function fetchExecutes(bytes32 hash)
        external
        view
        override
        returns (ExecutesStruct memory)
    {
        return executes[hash];
    }
 
    /// @notice routerSend This is internal function to generate a cross chain communication request.
    /// @param destChainId Destination ChainID.
    /// @param _selector Selector to interface on destination side.
    /// @param _data Data to be sent on Destination side.
    /// @param _gasLimit Gas limit provided for cross chain send.
    /// @param _gasPrice Gas price provided for cross chain send.
    function routerSend(
        uint8 destChainId,
        bytes4 _selector,
        bytes memory _data,
        uint256 _gasLimit,
        uint256 _gasPrice
    ) internal isLinkSet(destChainId) returns (bool, bytes32) {
        bytes memory data = abi.encode(_selector, _data);
        uint64 nonce = handler.genericDeposit(
            destChainId,
            data,
            _gasLimit,
            _gasPrice,
            feeToken
        );
        bytes32 hash = _hash(destChainId, nonce);
        executes[hash] = ExecutesStruct(destChainId, nonce);
        emitCrossTalkSendEvent(destChainId, _selector, _data, hash);
        return (true, hash);
    }
 
    function emitCrossTalkSendEvent(
        uint8 destChainId,
        bytes4 selector,
        bytes memory data,
        bytes32 hash
    ) private {
        emit CrossTalkSend(
            handler.fetch_chainID(),
            destChainId,
            address(this),
            Chain2Addr[destChainId],
            selector,
            data,
            hash
        );
    }
 
    function routerSync(
        uint8 srcChainID,
        address srcAddress,
        bytes memory data
    )
        external
        override
        isLinkSync(srcChainID, srcAddress)
        isHandler
        returns (bool, bytes memory)
    {
        uint8 cid = handler.fetch_chainID();
        (bytes4 _selector, bytes memory _data) = abi.decode(
            data,
            (bytes4, bytes)
        );
        (bool success, bytes memory _returnData) = _routerSyncHandler(
            _selector,
            _data
        );
        emit CrossTalkReceive(srcChainID, cid, srcAddress);
        return (success, _returnData);
    }
 
    function routerReplay(
        bytes32 hash,
        uint256 _gasLimit,
        uint256 _gasPrice
    ) internal {
        handler.replayGenericDeposit(
            executes[hash].chainID,
            executes[hash].nonce,
            _gasLimit,
            _gasPrice
        );
    }
 
    /// @notice _hash This is internal function to generate the hash of all data sent or received by the contract.
    /// @param _destChainId Destination ChainID.
    /// @param _nonce Nonce for the tx.
    function _hash(uint8 _destChainId, uint64 _nonce)
        internal
        pure
        returns (bytes32)
    {
        return keccak256(abi.encode(_destChainId, _nonce));
    }
 
    function Link(uint8 _chainID, address _linkedContract)
        external
        override
        isHandler
        isLinkUnSet(_chainID)
    {
        Chain2Addr[_chainID] = _linkedContract;
        emit Linkevent(_chainID, _linkedContract);
    }
 
    function Unlink(uint8 _chainID)
        external
        override
        isHandler
        isLinkSet(_chainID)
    {
        emit Unlinkevent(_chainID, Chain2Addr[_chainID]);
        Chain2Addr[_chainID] = address(0);
    }
 
    function approveFees(address _feeToken, uint256 _value) internal {
        IERC20Upgradeable token = IERC20Upgradeable(_feeToken);
        token.approve(address(handler), _value);
    }
 
    /// @notice _routerSyncHandler This is internal function to control the handling of various selectors and its corresponding .
    /// @param _selector Selector to interface.
    /// @param _data Data to be handled.
    function _routerSyncHandler(bytes4 _selector, bytes memory _data)
        internal
        virtual
        returns (bool, bytes memory);
 
    uint256[100] private __gap;
}