MagpieCelerBridgeV2
Last updated
Last updated
The MagpieCelerBridgeV2 Solidity contract facilitates cross-chain liquidity aggregation using the Celer cBridge. It inherits from Ownable2Step and Pausable contracts, providing ownership management and pausable functionality. The contract manages various state variables, including mappings for internal callers, deposits, and refund addresses, and stores addresses for WETH, Celer addresses, and a combined network ID and router address. It features custom modifiers to restrict access to certain functions and includes functions to update internal callers, WETH address, network ID, router address, and Celer Network address. Key functions like swapInWithMagpieSignature, swapInWithUserSignature, and swapIn handle swap operations, utilizing the LibRouter library for swap data and execution, with additional checks for native assets. The bridgeIn function manages the bridging of assets, while swapOut handles outbound swaps. The contract also processes inbound messages with asset transfers through executeMessageWithTransfer and handles refunds for failed transfers via executeMessageWithTransferRefund. Additionally, it includes a multicall function for executing multiple calls in a single transaction and a receive function to accept Ether payments.
Function Name | Description (Business Logic) |
---|---|
swapInWithMagpieSignature
The swapInWithMagpieSignature function handles swap operations and can be called by anyone when the contract is not paused. It utilizes the LibRouter library to retrieve the necessary swap data and execute the swap. This function ensures that the swap operation is performed correctly, including any additional checks required for native assets.
swapInWithUserSignature
The swapInWithUserSignature function also handles swap operations but is restricted to internal callers. Like swapInWithMagpieSignature, it uses the LibRouter library to retrieve swap data and execute the swap. This function includes additional checks specifically for native assets to ensure the integrity and security of the swap operation.
verifySignature
The verifySignature function is responsible for verifying the signature of a swap operation. This function takes in a SwapData struct, which contains the details of the swap, and a boolean flag useCaller that indicates whether the callerβs address should be used for verification. The function begins by determining the length of the message based on whether the swap includes an affiliate. It then uses inline assembly to construct the message that will be hashed and verified. The message includes various parameters such as the source and destination addresses, asset details, amounts, and network IDs. The assembly code dynamically allocates memory for the message and populates it with the relevant data from the swapData struct. Depending on whether an affiliate is involved, it uses different keccak256 hash values to identify the type of swap. Finally, the function calls LibRouter.verifySignature with the constructed message and other parameters to verify the signature. If the signature is valid, it returns the address of the signer.
swapIn
The swapIn function executes a swap operation. It takes a SwapData struct and a boolean flag useCaller as parameters and returns the amount received from the swap operation. The function begins by incrementing the swapSequence to track the current swap operation. It then retrieves the network ID and router address from the stored networkIdAndRouterAddress using inline assembly. Next, the function verifies the signature of the swap operation by calling the verifySignature function. If the swap data includes a permit, it calls LibRouter.permit to handle the permit. It also transfers any associated fees using LibRouter.transferFees. The function then prepares the deposit data by creating a new byte array and filling it with the necessary information using LibBridge.fillEncodedDepositData. It calculates the hash of the deposit data and stores the refund address in the refundAddresses mapping. The actual swap operation is performed by calling LibBridge.swapIn, which returns the output amount. The function then calls bridgeIn to handle the bridging of assets, passing the necessary parameters including the fee, asset address, output amount, deposit data hash, and current swap sequence. Finally, the function checks for reentrancy by comparing the current swap sequence with the stored swapSequence. If they do not match, it reverts with a ReentrancyError.
bridgeIn
The bridgeIn function in the MagpieCelerBridgeV2 contract handles the bridging of an inbound asset transfer into the contract. It takes several parameters, including the bridge fee, the address of the asset being bridged, the amount of the asset, the encoded hash related to the cross-chain transaction, and the current swap sequence. The function begins by creating an instance of the IMessageBus interface using the stored celerAddress. It retrieves the address of the liquidity bridge from the message bus. If the asset being bridged is not a native token, it approves the liquidity bridge to transfer the specified amount. Next, the function uses inline assembly to extract the receiver address, chain ID, and slippage from the calldata. These values are necessary for the bridging operation. It then calls the send method of the ILiquidityBridge interface, passing the receiver address, asset address, amount, chain ID, current swap sequence, and slippage. This method handles the actual transfer of assets across chains. To ensure the transaction is tracked, the function calculates a transferId by hashing various parameters, including the contract address, receiver address, asset address, amount, chain ID, current swap sequence, and the current chain Id. Finally, the function calls sendMessageWithTransfer on the message bus, passing the bridge fee, receiver address, chain ID, liquidity bridge address, transfer ID, and the encoded deposit data hash. This method sends a message along with the transfer to ensure the transaction is properly recorded and processed.
swapOut
The swapOut function in the MagpieCelerBridgeV2 contract handles the outbound swap operation. It is restricted to internal callers and returns the amount received from the swap. The function retrieves the network ID and router address using inline assembly and then gets the swap data from LibRouter. It calculates the deposit data hash and retrieves the deposit amount. If the deposit amount is zero and the asset is native, it unwraps the WETH and updates the deposit amount. The function then calls LibBridge.swapOut to perform the swap and returns the output amount.
executeMessageWithTransfer
The executeMessageWithTransfer function processes inbound messages with asset transfers. It decodes the deposit data hash from the payload and updates the deposit mapping with the received amount. An event is emitted to log the deposit, and the function returns a success status.
executeMessageWithTransferRefund
The executeMessageWithTransferRefund function handles refunds for failed transfers. It decodes the deposit data hash and retrieves the refund address. If the refund address is invalid, it reverts. The function then transfers the refund amount to the receiver and emits a refund event. It returns a success status.
Multicall
The multicall function allows the contract owner to execute multiple function calls in a single transaction. It iterates over the provided data array, executing each call using Address.functionDelegateCall, and returns the results.
receive
receive function handles incoming Ether transfers.