@etherspot/contracts
Version:
Etherspot Solidity contracts
38 lines • 88.4 kB
JSON
{
"language": "Solidity",
"sources": {
"src/bridges/facets/StargateFacet.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.4;\n\nimport {IStargateRouter} from \"../interfaces/IStargateRouter.sol\";\nimport {IStargateReceiver} from \"../interfaces/IStargateReceiver.sol\";\nimport {IERC20} from \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\nimport {SafeERC20} from \"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol\";\nimport {ReentrancyGuard} from \"../../common/helpers/DiamondReentrancyGuard.sol\";\nimport {CannotBridgeToSameNetwork, InvalidAmount, InvalidConfig} from \"../errors/GenericErrors.sol\";\nimport {SenderNotStargateRouter, NoMsgValueForCrossChainMessage, StargateRouterAddressZero, InvalidSourcePoolId, InvalidDestinationPoolId} from \"../errors/StargateErrors.sol\";\nimport {LibDiamond} from \"../libs/LibDiamond.sol\";\n\n/// @title StargateFacet\n/// @author Luke Wickens <luke@pillarproject.io>\n/// @notice Stargate/LayerZero intergration for bridging tokens\n\ncontract StargateFacet is IStargateReceiver, ReentrancyGuard {\n using SafeERC20 for IERC20;\n\n //////////////////////////////////////////////////////////////\n /////////////////////////// Events ///////////////////////////\n //////////////////////////////////////////////////////////////\n event SGInitialized(address stargate, uint16 chainId);\n event SGTransferStarted(\n string bridgeUsed,\n address fromToken,\n address toToken,\n address from,\n address to,\n uint256 amount,\n uint16 chainIdTo\n );\n event SGReceivedOnDestination(address token, uint256 amount);\n event SGUpdatedRouter(address newAddress);\n event SGUpdatedSlippageTolerance(uint256 newSlippage);\n event SGAddedPool(uint16 chainId, address token, uint16 poolId);\n\n //////////////////////////////////////////////////////////////\n ////////////////////////// Storage ///////////////////////////\n //////////////////////////////////////////////////////////////\n\n bytes32 internal constant NAMESPACE =\n keccak256(\"io.etherspot.facets.stargate\");\n struct Storage {\n address stargateRouter;\n uint16 chainId;\n uint256 dstGas;\n uint256 slippage;\n mapping(uint16 => mapping(address => uint16)) poolIds;\n }\n\n //////////////////////////////////////////////////////////////\n ////////////////////////// Structs ///////////////////////////\n //////////////////////////////////////////////////////////////\n\n struct StargateData {\n uint256 qty;\n address fromToken;\n address toToken;\n uint16 dstChainId;\n address to;\n address destStargateComposed;\n }\n\n /// @notice initializes state variables for the Stargate facet\n /// @param _stargateRouter - address of the Stargate router contract\n /// @param _chainId - current chain id\n function sgInitialize(address _stargateRouter, uint16 _chainId) external {\n if (_stargateRouter == address(0)) revert InvalidConfig();\n LibDiamond.enforceIsContractOwner();\n Storage storage s = getStorage();\n s.stargateRouter = address(_stargateRouter);\n s.chainId = _chainId;\n s.slippage = 50; // equates to 0.5%\n // Adding pre-existing pools => USDC: 1, USDT: 2, BUSD: 5\n sgAddPool(1, 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48, 1);\n sgAddPool(1, 0xdAC17F958D2ee523a2206206994597C13D831ec7, 2);\n sgAddPool(2, 0x55d398326f99059fF775485246999027B3197955, 2);\n sgAddPool(2, 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56, 5);\n sgAddPool(6, 0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E, 1);\n sgAddPool(6, 0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7, 2);\n sgAddPool(9, 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174, 1);\n sgAddPool(9, 0xc2132D05D31c914a87C6611C10748AEb04B58e8F, 2);\n sgAddPool(10, 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8, 1);\n sgAddPool(10, 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9, 2);\n sgAddPool(11, 0x7F5c764cBc14f9669B88837ca1490cCa17c31607, 1);\n sgAddPool(12, 0x04068DA6C83AFCFA0e13ba15A6696662335D5B75, 1);\n emit SGInitialized(_stargateRouter, _chainId);\n }\n\n /// @notice initializes state variables for the stargate facet\n /// @param _sgData - struct containing information required to execute bridge\n function sgBridgeTokens(StargateData memory _sgData)\n external\n payable\n nonReentrant\n {\n // if (msg.value <= 0) revert NoMsgValueForCrossChainMessage();\n if (_sgData.qty <= 0) revert InvalidAmount();\n if (\n _sgData.fromToken == address(0) ||\n _sgData.toToken == address(0) ||\n _sgData.to == address(0) ||\n _sgData.destStargateComposed == address(0)\n ) revert InvalidConfig();\n\n // access storage\n Storage storage s = getStorage();\n\n // check pool ids are valid\n uint16 srcPoolId = sgRetrievePoolId(s.chainId, _sgData.fromToken);\n if (srcPoolId == 0) revert InvalidSourcePoolId();\n uint16 dstPoolId = sgRetrievePoolId(\n _sgData.dstChainId,\n _sgData.toToken\n );\n\n // calculate cross chain fees\n uint256 fees = sgCalculateFees(\n _sgData.dstChainId,\n _sgData.to,\n s.stargateRouter\n );\n\n // calculate slippage\n uint256 minAmountOut = sgMinAmountOut(_sgData.qty);\n\n // encode sgReceive implemented\n bytes memory destination = abi.encodePacked(\n _sgData.destStargateComposed\n );\n\n // encode payload data to send to destination contract, which it will handle with sgReceive()\n bytes memory payload = abi.encode(_sgData.to);\n\n // this contract calls stargate swap()\n IERC20(_sgData.fromToken).safeTransferFrom(\n msg.sender,\n address(this),\n _sgData.qty\n );\n\n IERC20(_sgData.fromToken).safeApprove(\n address(s.stargateRouter),\n _sgData.qty\n );\n\n // Stargate's Router.swap() function sends the tokens to the destination chain.\n IStargateRouter(s.stargateRouter).swap{value: fees}(\n _sgData.dstChainId, // the destination chain id\n srcPoolId, // the source Stargate poolId\n dstPoolId, // the destination Stargate poolId\n payable(msg.sender), // refund adddress. if msg.sender pays too much gas, return extra eth\n _sgData.qty, // total tokens to send to destination chain\n minAmountOut, // min amount allowed out\n IStargateRouter.lzTxObj(200000, 0, \"0x\"), // default lzTxObj\n destination, // destination address, the sgReceive() implementer\n payload // bytes payload\n );\n\n emit SGTransferStarted(\n \"stargate\",\n _sgData.fromToken,\n _sgData.toToken,\n msg.sender,\n _sgData.to,\n _sgData.qty,\n _sgData.dstChainId\n );\n }\n\n /// @notice required to receive tokens on destination chain\n /// @param _chainId The remote chainId sending the tokens\n /// @param _srcAddress The remote Bridge address\n /// @param _nonce The message ordering nonce\n /// @param _token The token contract on the local chain\n /// @param amountLD The qty of local _token contract tokens\n /// @param _payload The bytes containing the toAddress\n function sgReceive(\n uint16 _chainId,\n bytes memory _srcAddress,\n uint256 _nonce,\n address _token,\n uint256 amountLD,\n bytes memory _payload\n ) external override {\n Storage storage s = getStorage();\n if (msg.sender != address(s.stargateRouter))\n revert SenderNotStargateRouter();\n\n address _toAddr = abi.decode(_payload, (address));\n IERC20(_token).transfer(_toAddr, amountLD);\n emit SGReceivedOnDestination(_token, amountLD);\n }\n\n /// @notice Calculates cross chain fee\n /// @param _destChain Destination chain id\n /// @param _receiver Receiver on destination chain\n /// @param _router Address of stargate router\n function sgCalculateFees(\n uint16 _destChain,\n address _receiver,\n address _router\n ) public view returns (uint256) {\n (uint256 nativeFee, ) = IStargateRouter(_router).quoteLayerZeroFee(\n _destChain, // destination chain id\n 1, // 1 = swap\n abi.encodePacked(_receiver), // receiver on destination chain\n \"0x\", // payload, using abi.encode()\n IStargateRouter.lzTxObj(200000, 0, \"0x\")\n );\n return nativeFee;\n }\n\n /// @notice Calculates the minimum amount out using slippage tolerance\n /// @param _amount Transfer amount\n function sgMinAmountOut(uint256 _amount) public view returns (uint256) {\n Storage storage s = getStorage();\n // equates to 0.5% slippage\n return (_amount * (10000 - s.slippage)) / (10000);\n }\n\n /// @notice Updates stargate router address for deployed chain\n /// @param _newAddress Address of the new router\n function sgUpdateRouter(address _newAddress) external {\n LibDiamond.enforceIsContractOwner();\n if (_newAddress == address(0)) revert StargateRouterAddressZero();\n Storage storage s = getStorage();\n s.stargateRouter = address(_newAddress);\n emit SGUpdatedRouter(_newAddress);\n }\n\n /// @notice Updates slippage tolerance amount\n /// @param _newSlippage New slippage amount\n function sgUpdateSlippageTolerance(uint256 _newSlippage) external {\n LibDiamond.enforceIsContractOwner();\n Storage storage s = getStorage();\n s.slippage = _newSlippage;\n emit SGUpdatedSlippageTolerance(_newSlippage);\n }\n\n /// @notice Adds a new pool for a specific token and chain\n /// @param _chainId Chain id of new pool (NOT actual chain id - check stargate pool ids docs)\n /// @param _token Address of token\n /// @param _poolId Pool id (check stargate pool ids docs)\n function sgAddPool(\n uint16 _chainId,\n address _token,\n uint16 _poolId\n ) public {\n LibDiamond.enforceIsContractOwner();\n Storage storage s = getStorage();\n s.poolIds[_chainId][_token] = _poolId;\n emit SGAddedPool(_chainId, _token, _poolId);\n }\n\n /// @notice Checks for a valid token pool on specific chain\n /// @param _chainId Chain id of new pool (NOT actual chain id - check stargate pool ids docs)\n /// @param _token Address of token\n /// @param _poolId Pool id (check stargate pool ids docs)\n function sgCheckPoolId(\n uint16 _chainId,\n address _token,\n uint16 _poolId\n ) external view returns (bool) {\n Storage storage s = getStorage();\n return s.poolIds[_chainId][_token] == _poolId ? true : false;\n }\n\n /// @notice Retrieves pool id for a token on a specified chain\n /// @param _chainId Chain id of new pool (NOT actual chain id - check stargate pool ids docs)\n /// @param _token Address of token\n function sgRetrievePoolId(uint16 _chainId, address _token)\n public\n view\n returns (uint16)\n {\n Storage storage s = getStorage();\n return s.poolIds[_chainId][_token];\n }\n\n //////////////////////////////////////////////////////////////\n ////////////////////// Private Functions /////////////////////\n //////////////////////////////////////////////////////////////\n\n /// @dev fetch local storage\n function getStorage() private pure returns (Storage storage s) {\n bytes32 namespace = NAMESPACE;\n // solhint-disable-next-line no-inline-assembly\n assembly {\n s.slot := namespace\n }\n }\n}\n"
},
"src/bridges/interfaces/IStargateRouter.sol": {
"content": "// SPDX-License-Identifier:MIT\n\npragma solidity 0.8.4;\npragma abicoder v2;\n\ninterface IStargateRouter {\n struct lzTxObj {\n uint256 dstGasForCall;\n uint256 dstNativeAmount;\n bytes dstNativeAddr;\n }\n\n function addLiquidity(\n uint256 _poolId,\n uint256 _amountLD,\n address _to\n ) external;\n\n function swap(\n uint16 _dstChainId,\n uint256 _srcPoolId,\n uint256 _dstPoolId,\n address payable _refundAddress,\n uint256 _amountLD,\n uint256 _minAmountLD,\n lzTxObj memory _lzTxParams,\n bytes calldata _to,\n bytes calldata _payload\n ) external payable;\n\n function redeemRemote(\n uint16 _dstChainId,\n uint256 _srcPoolId,\n uint256 _dstPoolId,\n address payable _refundAddress,\n uint256 _amountLP,\n uint256 _minAmountLD,\n bytes calldata _to,\n lzTxObj memory _lzTxParams\n ) external payable;\n\n function instantRedeemLocal(\n uint16 _srcPoolId,\n uint256 _amountLP,\n address _to\n ) external returns (uint256);\n\n function redeemLocal(\n uint16 _dstChainId,\n uint256 _srcPoolId,\n uint256 _dstPoolId,\n address payable _refundAddress,\n uint256 _amountLP,\n bytes calldata _to,\n lzTxObj memory _lzTxParams\n ) external payable;\n\n function sendCredits(\n uint16 _dstChainId,\n uint256 _srcPoolId,\n uint256 _dstPoolId,\n address payable _refundAddress\n ) external payable;\n\n function quoteLayerZeroFee(\n uint16 _dstChainId,\n uint8 _functionType,\n bytes calldata _toAddress,\n bytes calldata _transferAndCallPayload,\n lzTxObj memory _lzTxParams\n ) external view returns (uint256, uint256);\n}\n"
},
"src/bridges/interfaces/IStargateReceiver.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity 0.8.4;\n\ninterface IStargateReceiver {\n function sgReceive(\n uint16 _srcChainId, // the remote chainId sending the tokens\n bytes memory _srcAddress, // the remote Bridge address\n uint256 _nonce,\n address _token, // the token contract on the local chain\n uint256 amountLD, // the qty of local _token contract tokens\n bytes memory payload\n ) external;\n}\n"
},
"@openzeppelin/contracts/token/ERC20/IERC20.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Interface of the ERC20 standard as defined in the EIP.\n */\ninterface IERC20 {\n /**\n * @dev Emitted when `value` tokens are moved from one account (`from`) to\n * another (`to`).\n *\n * Note that `value` may be zero.\n */\n event Transfer(address indexed from, address indexed to, uint256 value);\n\n /**\n * @dev Emitted when the allowance of a `spender` for an `owner` is set by\n * a call to {approve}. `value` is the new allowance.\n */\n event Approval(address indexed owner, address indexed spender, uint256 value);\n\n /**\n * @dev Returns the amount of tokens in existence.\n */\n function totalSupply() external view returns (uint256);\n\n /**\n * @dev Returns the amount of tokens owned by `account`.\n */\n function balanceOf(address account) external view returns (uint256);\n\n /**\n * @dev Moves `amount` tokens from the caller's account to `to`.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transfer(address to, uint256 amount) external returns (bool);\n\n /**\n * @dev Returns the remaining number of tokens that `spender` will be\n * allowed to spend on behalf of `owner` through {transferFrom}. This is\n * zero by default.\n *\n * This value changes when {approve} or {transferFrom} are called.\n */\n function allowance(address owner, address spender) external view returns (uint256);\n\n /**\n * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * IMPORTANT: Beware that changing an allowance with this method brings the risk\n * that someone may use both the old and the new allowance by unfortunate\n * transaction ordering. One possible solution to mitigate this race\n * condition is to first reduce the spender's allowance to 0 and set the\n * desired value afterwards:\n * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\n *\n * Emits an {Approval} event.\n */\n function approve(address spender, uint256 amount) external returns (bool);\n\n /**\n * @dev Moves `amount` tokens from `from` to `to` using the\n * allowance mechanism. `amount` is then deducted from the caller's\n * allowance.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transferFrom(\n address from,\n address to,\n uint256 amount\n ) external returns (bool);\n}\n"
},
"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)\n\npragma solidity ^0.8.0;\n\nimport \"../IERC20.sol\";\nimport \"../../../utils/Address.sol\";\n\n/**\n * @title SafeERC20\n * @dev Wrappers around ERC20 operations that throw on failure (when the token\n * contract returns false). Tokens that return no value (and instead revert or\n * throw on failure) are also supported, non-reverting calls are assumed to be\n * successful.\n * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,\n * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.\n */\nlibrary SafeERC20 {\n using Address for address;\n\n function safeTransfer(\n IERC20 token,\n address to,\n uint256 value\n ) internal {\n _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));\n }\n\n function safeTransferFrom(\n IERC20 token,\n address from,\n address to,\n uint256 value\n ) internal {\n _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));\n }\n\n /**\n * @dev Deprecated. This function has issues similar to the ones found in\n * {IERC20-approve}, and its usage is discouraged.\n *\n * Whenever possible, use {safeIncreaseAllowance} and\n * {safeDecreaseAllowance} instead.\n */\n function safeApprove(\n IERC20 token,\n address spender,\n uint256 value\n ) internal {\n // safeApprove should only be called when setting an initial allowance,\n // or when resetting it to zero. To increase and decrease it, use\n // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'\n require(\n (value == 0) || (token.allowance(address(this), spender) == 0),\n \"SafeERC20: approve from non-zero to non-zero allowance\"\n );\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));\n }\n\n function safeIncreaseAllowance(\n IERC20 token,\n address spender,\n uint256 value\n ) internal {\n uint256 newAllowance = token.allowance(address(this), spender) + value;\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\n }\n\n function safeDecreaseAllowance(\n IERC20 token,\n address spender,\n uint256 value\n ) internal {\n unchecked {\n uint256 oldAllowance = token.allowance(address(this), spender);\n require(oldAllowance >= value, \"SafeERC20: decreased allowance below zero\");\n uint256 newAllowance = oldAllowance - value;\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\n }\n }\n\n /**\n * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement\n * on the return value: the return value is optional (but if data is returned, it must not be false).\n * @param token The token targeted by the call.\n * @param data The call data (encoded using abi.encode or one of its variants).\n */\n function _callOptionalReturn(IERC20 token, bytes memory data) private {\n // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since\n // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that\n // the target address contains contract code and also asserts for success in the low-level call.\n\n bytes memory returndata = address(token).functionCall(data, \"SafeERC20: low-level call failed\");\n if (returndata.length > 0) {\n // Return data is optional\n require(abi.decode(returndata, (bool)), \"SafeERC20: ERC20 operation did not succeed\");\n }\n }\n}\n"
},
"src/common/helpers/DiamondReentrancyGuard.sol": {
"content": "// SPDX-License-Identifier: UNLICENSED\npragma solidity 0.8.4;\n\n/// @title Reentrancy Guard\n/// @notice Abstract contract to provide protection against reentrancy\nabstract contract ReentrancyGuard {\n //////////////////////////////////////////////////////////////\n ////////////////////////// Storage ///////////////////////////\n //////////////////////////////////////////////////////////////\n\n bytes32 private constant NAMESPACE =\n keccak256(\"io.etherspot.helpers.reentrancyguard\");\n\n //////////////////////////////////////////////////////////////\n ////////////////////////// Structs ///////////////////////////\n //////////////////////////////////////////////////////////////\n\n struct ReentrancyStorage {\n uint256 status;\n }\n\n //////////////////////////////////////////////////////////////\n ////////////////////////// Errors ////////////////////////////\n //////////////////////////////////////////////////////////////\n\n error ReentrancyError();\n\n //////////////////////////////////////////////////////////////\n ///////////////////////// Constants //////////////////////////\n //////////////////////////////////////////////////////////////\n\n uint256 private constant _NOT_ENTERED = 0;\n uint256 private constant _ENTERED = 1;\n\n //////////////////////////////////////////////////////////////\n ///////////////////////// Modifiers ///////////////////////////\n //////////////////////////////////////////////////////////////\n\n modifier nonReentrant() {\n ReentrancyStorage storage s = reentrancyStorage();\n if (s.status == _ENTERED) revert ReentrancyError();\n s.status = _ENTERED;\n _;\n s.status = _NOT_ENTERED;\n }\n\n //////////////////////////////////////////////////////////////\n ////////////////////// Private Functions /////////////////////\n //////////////////////////////////////////////////////////////\n\n /// @dev fetch local storage\n function reentrancyStorage()\n private\n pure\n returns (ReentrancyStorage storage data)\n {\n bytes32 position = NAMESPACE;\n // solhint-disable-next-line no-inline-assembly\n assembly {\n data.slot := position\n }\n }\n}\n"
},
"src/bridges/errors/GenericErrors.sol": {
"content": "// SPDX-License-Identifier: MIT\n// solhint-disable-next-line\npragma solidity 0.8.4;\n\nerror InvalidAmount();\nerror TokenAddressIsZero();\nerror CannotBridgeToSameNetwork();\nerror ZeroPostSwapBalance();\nerror InvalidBridgeConfigLength();\nerror NoSwapDataProvided();\nerror NativeValueWithERC();\nerror ContractCallNotAllowed();\nerror NullAddrIsNotAValidSpender();\nerror NullAddrIsNotAnERC20Token();\nerror NoTransferToNullAddress();\nerror NativeAssetTransferFailed();\nerror InvalidContract();\nerror InvalidConfig();\nerror ZeroAddressProvided();\n"
},
"src/bridges/errors/StargateErrors.sol": {
"content": "// SPDX-License-Identifier: MIT\n// solhint-disable-next-line\npragma solidity 0.8.4;\n\nerror SenderNotStargateRouter();\nerror NoMsgValueForCrossChainMessage();\nerror StargateRouterAddressZero();\nerror InvalidSourcePoolId();\nerror InvalidDestinationPoolId();\n"
},
"src/bridges/libs/LibDiamond.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport {IDiamondCut} from \"../interfaces/IDiamondCut.sol\";\n\nlibrary LibDiamond {\n bytes32 internal constant DIAMOND_STORAGE_POSITION =\n keccak256(\"diamond.standard.diamond.storage\");\n\n struct FacetAddressAndPosition {\n address facetAddress;\n uint96 functionSelectorPosition; // position in facetFunctionSelectors.functionSelectors array\n }\n\n struct FacetFunctionSelectors {\n bytes4[] functionSelectors;\n uint256 facetAddressPosition; // position of facetAddress in facetAddresses array\n }\n\n struct DiamondStorage {\n // maps function selector to the facet address and\n // the position of the selector in the facetFunctionSelectors.selectors array\n mapping(bytes4 => FacetAddressAndPosition) selectorToFacetAndPosition;\n // maps facet addresses to function selectors\n mapping(address => FacetFunctionSelectors) facetFunctionSelectors;\n // facet addresses\n address[] facetAddresses;\n // Used to query if a contract implements an interface.\n // Used to implement ERC-165.\n mapping(bytes4 => bool) supportedInterfaces;\n // owner of the contract\n address contractOwner;\n }\n\n function diamondStorage()\n internal\n pure\n returns (DiamondStorage storage ds)\n {\n bytes32 position = DIAMOND_STORAGE_POSITION;\n // solhint-disable-next-line no-inline-assembly\n assembly {\n ds.slot := position\n }\n }\n\n event OwnershipTransferred(\n address indexed previousOwner,\n address indexed newOwner\n );\n\n function setContractOwner(address _newOwner) internal {\n DiamondStorage storage ds = diamondStorage();\n address previousOwner = ds.contractOwner;\n ds.contractOwner = _newOwner;\n emit OwnershipTransferred(previousOwner, _newOwner);\n }\n\n function contractOwner() internal view returns (address contractOwner_) {\n contractOwner_ = diamondStorage().contractOwner;\n }\n\n function enforceIsContractOwner() internal view {\n require(\n msg.sender == diamondStorage().contractOwner,\n \"LibDiamond: Must be contract owner\"\n );\n }\n\n event DiamondCut(\n IDiamondCut.FacetCut[] _diamondCut,\n address _init,\n bytes _calldata\n );\n\n // Internal function version of diamondCut\n function diamondCut(\n IDiamondCut.FacetCut[] memory _diamondCut,\n address _init,\n bytes memory _calldata\n ) internal {\n for (\n uint256 facetIndex;\n facetIndex < _diamondCut.length;\n facetIndex++\n ) {\n IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action;\n if (action == IDiamondCut.FacetCutAction.Add) {\n addFunctions(\n _diamondCut[facetIndex].facetAddress,\n _diamondCut[facetIndex].functionSelectors\n );\n } else if (action == IDiamondCut.FacetCutAction.Replace) {\n replaceFunctions(\n _diamondCut[facetIndex].facetAddress,\n _diamondCut[facetIndex].functionSelectors\n );\n } else if (action == IDiamondCut.FacetCutAction.Remove) {\n removeFunctions(\n _diamondCut[facetIndex].facetAddress,\n _diamondCut[facetIndex].functionSelectors\n );\n } else {\n revert(\"LibDiamondCut: Incorrect FacetCutAction\");\n }\n }\n emit DiamondCut(_diamondCut, _init, _calldata);\n initializeDiamondCut(_init, _calldata);\n }\n\n function addFunctions(\n address _facetAddress,\n bytes4[] memory _functionSelectors\n ) internal {\n require(\n _functionSelectors.length > 0,\n \"LibDiamondCut: No selectors in facet to cut\"\n );\n DiamondStorage storage ds = diamondStorage();\n require(\n _facetAddress != address(0),\n \"LibDiamondCut: Add facet can't be address(0)\"\n );\n uint96 selectorPosition = uint96(\n ds.facetFunctionSelectors[_facetAddress].functionSelectors.length\n );\n // add new facet address if it does not exist\n if (selectorPosition == 0) {\n addFacet(ds, _facetAddress);\n }\n for (\n uint256 selectorIndex;\n selectorIndex < _functionSelectors.length;\n selectorIndex++\n ) {\n bytes4 selector = _functionSelectors[selectorIndex];\n address oldFacetAddress = ds\n .selectorToFacetAndPosition[selector]\n .facetAddress;\n require(\n oldFacetAddress == address(0),\n \"LibDiamondCut: Can't add function that already exists\"\n );\n addFunction(ds, selector, selectorPosition, _facetAddress);\n selectorPosition++;\n }\n }\n\n function replaceFunctions(\n address _facetAddress,\n bytes4[] memory _functionSelectors\n ) internal {\n require(\n _functionSelectors.length > 0,\n \"LibDiamondCut: No selectors in facet to cut\"\n );\n DiamondStorage storage ds = diamondStorage();\n require(\n _facetAddress != address(0),\n \"LibDiamondCut: Add facet can't be address(0)\"\n );\n uint96 selectorPosition = uint96(\n ds.facetFunctionSelectors[_facetAddress].functionSelectors.length\n );\n // add new facet address if it does not exist\n if (selectorPosition == 0) {\n addFacet(ds, _facetAddress);\n }\n for (\n uint256 selectorIndex;\n selectorIndex < _functionSelectors.length;\n selectorIndex++\n ) {\n bytes4 selector = _functionSelectors[selectorIndex];\n address oldFacetAddress = ds\n .selectorToFacetAndPosition[selector]\n .facetAddress;\n require(\n oldFacetAddress != _facetAddress,\n \"LibDiamondCut: Can't replace function with same function\"\n );\n removeFunction(ds, oldFacetAddress, selector);\n addFunction(ds, selector, selectorPosition, _facetAddress);\n selectorPosition++;\n }\n }\n\n function removeFunctions(\n address _facetAddress,\n bytes4[] memory _functionSelectors\n ) internal {\n require(\n _functionSelectors.length > 0,\n \"LibDiamondCut: No selectors in facet to cut\"\n );\n DiamondStorage storage ds = diamondStorage();\n // if function does not exist then do nothing and return\n require(\n _facetAddress == address(0),\n \"LibDiamondCut: Remove facet address must be address(0)\"\n );\n for (\n uint256 selectorIndex;\n selectorIndex < _functionSelectors.length;\n selectorIndex++\n ) {\n bytes4 selector = _functionSelectors[selectorIndex];\n address oldFacetAddress = ds\n .selectorToFacetAndPosition[selector]\n .facetAddress;\n removeFunction(ds, oldFacetAddress, selector);\n }\n }\n\n function addFacet(DiamondStorage storage ds, address _facetAddress)\n internal\n {\n enforceHasContractCode(\n _facetAddress,\n \"LibDiamondCut: New facet has no code\"\n );\n ds.facetFunctionSelectors[_facetAddress].facetAddressPosition = ds\n .facetAddresses\n .length;\n ds.facetAddresses.push(_facetAddress);\n }\n\n function addFunction(\n DiamondStorage storage ds,\n bytes4 _selector,\n uint96 _selectorPosition,\n address _facetAddress\n ) internal {\n ds\n .selectorToFacetAndPosition[_selector]\n .functionSelectorPosition = _selectorPosition;\n ds.facetFunctionSelectors[_facetAddress].functionSelectors.push(\n _selector\n );\n ds.selectorToFacetAndPosition[_selector].facetAddress = _facetAddress;\n }\n\n function removeFunction(\n DiamondStorage storage ds,\n address _facetAddress,\n bytes4 _selector\n ) internal {\n require(\n _facetAddress != address(0),\n \"LibDiamondCut: Can't remove function that doesn't exist\"\n );\n // an immutable function is a function defined directly in a diamond\n require(\n _facetAddress != address(this),\n \"LibDiamondCut: Can't remove immutable function\"\n );\n // replace selector with last selector, then delete last selector\n uint256 selectorPosition = ds\n .selectorToFacetAndPosition[_selector]\n .functionSelectorPosition;\n uint256 lastSelectorPosition = ds\n .facetFunctionSelectors[_facetAddress]\n .functionSelectors\n .length - 1;\n // if not the same then replace _selector with lastSelector\n if (selectorPosition != lastSelectorPosition) {\n bytes4 lastSelector = ds\n .facetFunctionSelectors[_facetAddress]\n .functionSelectors[lastSelectorPosition];\n ds.facetFunctionSelectors[_facetAddress].functionSelectors[\n selectorPosition\n ] = lastSelector;\n ds\n .selectorToFacetAndPosition[lastSelector]\n .functionSelectorPosition = uint96(selectorPosition);\n }\n // delete the last selector\n ds.facetFunctionSelectors[_facetAddress].functionSelectors.pop();\n delete ds.selectorToFacetAndPosition[_selector];\n\n // if no more selectors for facet address then delete the facet address\n if (lastSelectorPosition == 0) {\n // replace facet address with last facet address and delete last facet address\n uint256 lastFacetAddressPosition = ds.facetAddresses.length - 1;\n uint256 facetAddressPosition = ds\n .facetFunctionSelectors[_facetAddress]\n .facetAddressPosition;\n if (facetAddressPosition != lastFacetAddressPosition) {\n address lastFacetAddress = ds.facetAddresses[\n lastFacetAddressPosition\n ];\n ds.facetAddresses[facetAddressPosition] = lastFacetAddress;\n ds\n .facetFunctionSelectors[lastFacetAddress]\n .facetAddressPosition = facetAddressPosition;\n }\n ds.facetAddresses.pop();\n delete ds\n .facetFunctionSelectors[_facetAddress]\n .facetAddressPosition;\n }\n }\n\n function initializeDiamondCut(address _init, bytes memory _calldata)\n internal\n {\n if (_init == address(0)) {\n require(\n _calldata.length == 0,\n \"LibDiamondCut: _init is address(0) but_calldata is not empty\"\n );\n } else {\n require(\n _calldata.length > 0,\n \"LibDiamondCut: _calldata is empty but _init is not address(0)\"\n );\n if (_init != address(this)) {\n enforceHasContractCode(\n _init,\n \"LibDiamondCut: _init address has no code\"\n );\n }\n // solhint-disable-next-line avoid-low-level-calls\n (bool success, bytes memory error) = _init.delegatecall(_calldata);\n if (!success) {\n if (error.length > 0) {\n // bubble up the error\n revert(string(error));\n } else {\n revert(\"LibDiamondCut: _init function reverted\");\n }\n }\n }\n }\n\n function enforceHasContractCode(\n address _contract,\n string memory _errorMessage\n ) internal view {\n uint256 contractSize;\n // solhint-disable-next-line no-inline-assembly\n assembly {\n contractSize := extcodesize(_contract)\n }\n require(contractSize > 0, _errorMessage);\n }\n}\n"
},
"@openzeppelin/contracts/utils/Address.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.5.0) (utils/Address.sol)\n\npragma solidity ^0.8.1;\n\n/**\n * @dev Collection of functions related to the address type\n */\nlibrary Address {\n /**\n * @dev Returns true if `account` is a contract.\n *\n * [IMPORTANT]\n * ====\n * It is unsafe to assume that an address for which this function returns\n * false is an externally-owned account (EOA) and not a contract.\n *\n * Among others, `isContract` will return false for the following\n * types of addresses:\n *\n * - an externally-owned account\n * - a contract in construction\n * - an address where a contract will be created\n * - an address where a contract lived, but was destroyed\n * ====\n *\n * [IMPORTANT]\n * ====\n * You shouldn't rely on `isContract` to protect against flash loan attacks!\n *\n * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets\n * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract\n * constructor.\n * ====\n */\n function isContract(address account) internal view returns (bool) {\n // This method relies on extcodesize/address.code.length, which returns 0\n // for contracts in construction, since the code is only stored at the end\n // of the constructor execution.\n\n return account.code.length > 0;\n }\n\n /**\n * @dev Replacement for Solidity's `transfer`: sends `amount` wei to\n * `recipient`, forwarding all available gas and reverting on errors.\n *\n * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost\n * of certain opcodes, possibly making contracts go over the 2300 gas limit\n * imposed by `transfer`, making them unable to receive funds via\n * `transfer`. {sendValue} removes this limitation.\n *\n * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].\n *\n * IMPORTANT: because control is transferred to `recipient`, care must be\n * taken to not create reentrancy vulnerabilities. Consider using\n * {ReentrancyGuard} or the\n * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].\n */\n function sendValue(address payable recipient, uint256 amount) internal {\n require(address(this).balance >= amount, \"Address: insufficient balance\");\n\n (bool success, ) = recipient.call{value: amount}(\"\");\n require(success, \"Address: unable to send value, recipient may have reverted\");\n }\n\n /**\n * @dev Performs a Solidity function call using a low level `call`. A\n * plain `call` is an unsafe replacement for a function call: use this\n * function instead.\n *\n * If `target` reverts with a revert reason, it is bubbled up by this\n * function (like regular Solidity function calls).\n *\n * Returns the raw returned data. To convert to the expected return value,\n * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].\n *\n * Requirements:\n *\n * - `target` must be a contract.\n * - calling `target` with `data` must not revert.\n *\n * _Available since v3.1._\n */\n function functionCall(address target, bytes memory data) internal returns (bytes memory) {\n return functionCall(target, data, \"Address: low-level call failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with\n * `errorMessage` as a fallback revert reason when `target` reverts.\n *\n * _Available since v3.1._\n */\n function functionCall(\n address target,\n bytes memory data,\n string memory errorMessage\n ) internal returns (bytes memory) {\n return functionCallWithValue(target, data, 0, errorMessage);\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\n * but also transferring `value` wei to `target`.\n *\n * Requirements:\n *\n * - the calling contract must have an ETH balance of at least `value`.\n * - the called Solidity function must be `payable`.\n *\n * _Available since v3.1._\n */\n function functionCallWithValue(\n address target,\n bytes memory data,\n uint256 value\n ) internal returns (bytes memory) {\n return functionCallWithValue(target, data, value, \"Address: low-level call with value failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but\n * with `errorMessage` as a fallback revert reason when `target` reverts.\n *\n * _Available since v3.1._\n */\n function functionCallWithValue(\n address target,\n bytes memory data,\n uint256 value,\n string memory errorMessage\n ) internal returns (bytes memory) {\n require(address(this).balance >= value, \"Address: insufficient balance for call\");\n require(isContract(target), \"Address: call to non-contract\");\n\n (bool success, bytes memory returndata) = target.call{value: value}(data);\n return verifyCallResult(success, returndata, errorMessage);\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\n * but performing a static call.\n *\n * _Available since v3.3._\n */\n function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {\n return functionStaticCall(target, data, \"Address: low-level static call failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\n * but performing a static call.\n *\n * _Available since v3.3._\n */\n function functionStaticCall(\n address target,\n bytes memory data,\n string memory errorMessage\n ) internal view returns (bytes memory) {\n require(isContract(target), \"Address: static call to non-contract\");\n\n (bool success, bytes memory returndata) = target.staticcall(data);\n return verifyCallResult(success, returndata, errorMessage);\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\n * but performing a delegate call.\n *\n * _Available since v3.4._\n */\n function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {\n return functionDelegateCall(target, data, \"Address: low-level delegate call failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\n * but performing a delegate call.\n *\n * _Available since v3.4._\n */\n function functionDelegateCall(\n address target,\n bytes memory data,\n string memory errorMessage\n ) internal returns (bytes memory) {\n require(isContract(target), \"Address: delegate call to non-contract\");\n\n (bool success, bytes memory returndata) = target.delegatecall(data);\n return verifyCallResult(success, returndata, errorMessage);\n }\n\n /**\n * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the\n * revert reason using the provided one.\n *\n * _Available since v4.3._\n */\n function verifyCallResult(\n bool success,\n bytes memory returndata,\n string memory errorMessage\n ) internal pure returns (bytes memory) {\n if (success) {\n return returndata;\n } else {\n // Look for revert reason and bubble it up if present\n if (returndata.length > 0) {\n // The easiest way to bubble the revert reason is using memory via assembly\n\n assembly {\n let returndata_size := mload(returndata)\n revert(add(32, returndata), returndata_size)\n }\n } else {\n revert(errorMessage);\n }\n }\n }\n}\n"
},
"src/bridges/interfaces/IDiamondCut.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\ninterface IDiamondCut {\n enum FacetCutAction {\n Add,\n Replace,\n Remove\n }\n // Add=0, Replace=1, Remove=2\n\n struct FacetCut {\n address facetAddress;\n FacetCutAction action;\n bytes4[] functionSelectors;\n }\n\n /// @notice Add/replace/remove any number of functions and optionally execute\n /// a function with delegatecall\n /// @param _diamondCut Contains the facet addresses and function selectors\n /// @param _init The address of the contract or facet to execute _calldata\n /// @param _calldata A function call, including function selector and arguments\n /// _calldata is executed with delegatecall on _init\n function diamondCut(\n FacetCut[] calldata _diamondCut,\n address _init,\n bytes calldata _calldata\n ) external;\n\n event DiamondCut(FacetCut[] _diamondCut, address _init, bytes _calldata);\n}\n"
},
"src/bridges/libs/LibAsset.sol": {
"content": "// SPDX-License-Identifier: UNLICENSED\n// solhint-disable-next-line\npragma solidity 0.8.4;\nimport {NullAddrIsNotAnERC20Token, NullAddrIsNotAValidSpender, NoTransferToNullAddress, InvalidAmount, NativeValueWithERC, NativeAssetTransferFailed} from \"../errors/GenericErrors.sol\";\nimport \"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol\";\nimport \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\n\n/// @title LibAsset\n/// @author Connext <support@connext.network>\n/// @notice This library contains helpers for dealing with onchain transfers\n/// of assets, including accounting for the native asset `assetId`\n/// conventions and any noncompliant ERC20 transfers\nlibrary LibAsset {\n uint256 private constant MAX_INT = type(uint256).max;\n\n address internal constant NULL_ADDRESS =\n 0x0000000000000000000000000000000000000000; //address(0)\n\n /// @dev All native assets use the empty address for their asset id\n /// by convention\n\n address internal constant NATIVE_ASSETID = NULL_ADDRESS; //address(0)\n\n /// @notice Gets the balance of the inheriting contract for the given asset\n /// @param assetId The asset identifier to get the balance of\n /// @return Balance held by contracts using this library\n function getOwnBalance(address assetId) internal view returns (uint256) {\n return\n assetId == NATIVE_ASSETID\n ? address(this).balance\n : IERC20(assetId).balanceOf(address(this));\n }\n\n /// @notice Transfers ether from the inheriting contract to a given\n /// recipient\n /// @param recipient Address to send ether to\n /// @param amount Amount to send to given recipient\n function transferNativeAsset(address payable recipient, uint256 amount)\n private\n {\n if (recipient == NULL_ADDRESS) revert NoTransferToNullAddress();\n // solhint-disable-next-line avoid-low-level-calls\n (bool success, ) = recipient.call{value: amount}(\"\");\n if (!success) revert NativeAssetTransferFailed();\n }\n\n /// @notice Gives MAX approval for another address to spend tokens\n /// @param assetId Token address to transfer\n /// @param spender Address to give spend approval to\n /// @param amount Amount to approve for spending\n function maxApproveERC20(\n IERC20 assetId,\n address spender,\n uint256 amount\n ) internal {\n if (address(assetId) == NATIVE_ASSETID) return