@@ -8,55 +8,27 @@ import { SwapperV2 } from "../Helpers/SwapperV2.sol";
88import { ReentrancyGuard } from "../Helpers/ReentrancyGuard.sol " ;
99import { Validatable } from "../Helpers/Validatable.sol " ;
1010import { LibSwap } from "../Libraries/LibSwap.sol " ;
11- import { InvalidConfig, InvalidCallData, InvalidNonEVMReceiver, InvalidReceiver } from "../Errors/GenericErrors.sol " ;
12- import { LiFiData } from "../Helpers/LiFiData.sol " ;
1311
1412/// @title Allbridge Facet
15- /// @author LI.FI (https://li.fi )
13+ /// @author Li.Finance (https://li.finance )
1614/// @notice Provides functionality for bridging through AllBridge
17- /// @custom:version 2.1.0
18- contract AllBridgeFacet is
19- ILiFi ,
20- ReentrancyGuard ,
21- SwapperV2 ,
22- Validatable ,
23- LiFiData
24- {
25- uint32 private constant ALLBRIDGE_ID_ETHEREUM = 1 ;
26- uint32 private constant ALLBRIDGE_ID_BSC = 2 ;
27- uint32 private constant ALLBRIDGE_ID_TRON = 3 ;
28- uint32 private constant ALLBRIDGE_ID_SOLANA = 4 ;
29- uint32 private constant ALLBRIDGE_ID_POLYGON = 5 ;
30- uint32 private constant ALLBRIDGE_ID_ARBITRUM = 6 ;
31- uint32 private constant ALLBRIDGE_ID_AVALANCHE = 8 ;
32- uint32 private constant ALLBRIDGE_ID_BASE = 9 ;
33- uint32 private constant ALLBRIDGE_ID_OPTIMISM = 10 ;
34- uint32 private constant ALLBRIDGE_ID_CELO = 11 ;
35- uint32 private constant ALLBRIDGE_ID_SUI = 13 ;
36- uint256 internal constant LIFI_CHAIN_ID_ARBITRUM = 42161 ;
37- uint256 internal constant LIFI_CHAIN_ID_AVALANCHE = 43114 ;
38- uint256 internal constant LIFI_CHAIN_ID_BASE = 8453 ;
39- uint256 internal constant LIFI_CHAIN_ID_BSC = 56 ;
40- uint256 internal constant LIFI_CHAIN_ID_CELO = 42220 ;
41- uint256 internal constant LIFI_CHAIN_ID_POLYGON = 137 ;
42-
43- error UnsupportedAllBridgeChainId ();
44-
15+ /// @custom:version 2.0.0
16+ contract AllBridgeFacet is ILiFi , ReentrancyGuard , SwapperV2 , Validatable {
4517 /// @notice The contract address of the AllBridge router on the source chain.
4618 // solhint-disable-next-line immutable-vars-naming
47- IAllBridge private immutable ALLBRIDGE ;
19+ IAllBridge private immutable allBridge ;
4820
4921 /// @notice The struct for the AllBridge data.
50- /// @param recipient The address of the token receiver after bridging.
5122 /// @param fees The amount of token to pay the messenger and the bridge
23+ /// @param recipient The address of the token receiver after bridging.
5224 /// @param destinationChainId The destination chain id.
5325 /// @param receiveToken The token to receive on the destination chain.
5426 /// @param nonce A random nonce to associate with the tx.
5527 /// @param messenger The messenger protocol enum
5628 /// @param payFeeWithSendingAsset Whether to pay the relayer fee with the sending asset or not
5729 struct AllBridgeData {
58- bytes32 recipient;
5930 uint256 fees;
31+ bytes32 recipient;
6032 uint256 destinationChainId;
6133 bytes32 receiveToken;
6234 uint256 nonce;
@@ -67,9 +39,7 @@ contract AllBridgeFacet is
6739 /// @notice Initializes the AllBridge contract
6840 /// @param _allBridge The address of the AllBridge contract
6941 constructor (IAllBridge _allBridge ) {
70- if (address (_allBridge) == address (0 )) revert InvalidConfig ();
71-
72- ALLBRIDGE = _allBridge;
42+ allBridge = _allBridge;
7343 }
7444
7545 /// @notice Bridge tokens to another chain via AllBridge
@@ -126,38 +96,14 @@ contract AllBridgeFacet is
12696 ILiFi.BridgeData memory _bridgeData ,
12797 AllBridgeData calldata _allBridgeData
12898 ) internal {
129- // make sure destinationChainId matches in bridgeData and allBridgeData
130- if (
131- _allBridgeData.destinationChainId !=
132- _getAllBridgeChainId (_bridgeData.destinationChainId)
133- ) revert InvalidCallData ();
134-
135- // validate receiver address
136- if (_bridgeData.receiver == NON_EVM_ADDRESS) {
137- // destination chain is non-EVM
138- // make sure it's non-zero (we cannot validate further)
139- if (_allBridgeData.recipient == bytes32 (0 ))
140- revert InvalidNonEVMReceiver ();
141- } else {
142- // destination chain is EVM
143- // make sure that bridgeData and allBridgeData receiver addresses match
144- if (
145- _bridgeData.receiver !=
146- address (uint160 (uint256 (_allBridgeData.recipient)))
147- ) revert InvalidReceiver ();
148- }
149-
150- // set max approval to allBridge, if current allowance is insufficient
15199 LibAsset.maxApproveERC20 (
152100 IERC20 (_bridgeData.sendingAssetId),
153- address (ALLBRIDGE ),
101+ address (allBridge ),
154102 _bridgeData.minAmount
155103 );
156104
157- // check if bridge fee should be paid with sending or native asset
158105 if (_allBridgeData.payFeeWithSendingAsset) {
159- // pay fee with sending asset
160- ALLBRIDGE.swapAndBridge (
106+ allBridge.swapAndBridge (
161107 bytes32 (uint256 (uint160 (_bridgeData.sendingAssetId))),
162108 _bridgeData.minAmount,
163109 _allBridgeData.recipient,
@@ -168,8 +114,7 @@ contract AllBridgeFacet is
168114 _allBridgeData.fees
169115 );
170116 } else {
171- // pay fee with native asset
172- ALLBRIDGE.swapAndBridge { value: _allBridgeData.fees }(
117+ allBridge.swapAndBridge { value: _allBridgeData.fees }(
173118 bytes32 (uint256 (uint160 (_bridgeData.sendingAssetId))),
174119 _bridgeData.minAmount,
175120 _allBridgeData.recipient,
@@ -183,41 +128,4 @@ contract AllBridgeFacet is
183128
184129 emit LiFiTransferStarted (_bridgeData);
185130 }
186-
187- /// @notice Converts LiFi internal chain IDs to AllBridge chain IDs
188- /// @param _destinationChainId The LiFi chain ID to convert
189- /// @return The corresponding Chainflip chain ID
190- /// @dev Reverts if the destination chain is not supported
191- function _getAllBridgeChainId (
192- uint256 _destinationChainId
193- ) internal pure returns (uint256 ) {
194- // split possible values in half for more efficient search/matching
195-
196- // first try to match cases where chainId is the same and does not need to be mapped
197- if (
198- _destinationChainId == ALLBRIDGE_ID_ETHEREUM ||
199- _destinationChainId == ALLBRIDGE_ID_OPTIMISM
200- ) return _destinationChainId;
201- // all others have custom chainIds
202- else if (_destinationChainId == LIFI_CHAIN_ID_BSC)
203- return ALLBRIDGE_ID_BSC;
204- else if (_destinationChainId == LIFI_CHAIN_ID_TRON)
205- return ALLBRIDGE_ID_TRON;
206- else if (_destinationChainId == LIFI_CHAIN_ID_SOLANA)
207- return ALLBRIDGE_ID_SOLANA;
208- else if (_destinationChainId == LIFI_CHAIN_ID_POLYGON)
209- return ALLBRIDGE_ID_POLYGON;
210- else if (_destinationChainId == LIFI_CHAIN_ID_ARBITRUM)
211- return ALLBRIDGE_ID_ARBITRUM;
212- else if (_destinationChainId == LIFI_CHAIN_ID_AVALANCHE)
213- return ALLBRIDGE_ID_AVALANCHE;
214- else if (_destinationChainId == LIFI_CHAIN_ID_BASE)
215- return ALLBRIDGE_ID_BASE;
216- else if (_destinationChainId == LIFI_CHAIN_ID_CELO)
217- return ALLBRIDGE_ID_CELO;
218- else if (_destinationChainId == LIFI_CHAIN_ID_SUI)
219- return ALLBRIDGE_ID_SUI;
220- // revert if no match found
221- else revert UnsupportedAllBridgeChainId ();
222- }
223131}
0 commit comments