LibBridge

The LibBridge library integrates several interfaces and libraries essential for its functionality, such as IMagpieRouterV3, IBridge, LibAsset, and LibRouter. It defines a DepositData struct to manage deposit details and includes custom error types to handle various error scenarios. The library uses LibAsset for address operations and includes functions like getFee, decodeDepositDataHash, encodeDepositDataHash, and getDepositDataHash to manage deposit data and fees. The fillEncodedDepositData function populates variables with encoded deposit data, while the swap and swapIn functions handle swap operations using specified routers and amounts. The swapOut function manages outbound swaps, verifying deposit amounts and handling different asset scenarios, and emits events detailing the swap operations.

Function Name
Description (Business Logic)

decodeDepositDataHash

This function converts a deposit data hash from a bytes format to a bytes32 format. This conversion is essential for handling and verifying deposit data within the contract.

encodeDepositDataHash

This function converts a deposit data hash from a bytes32 format back to a bytes format. This is useful for encoding deposit data for storage or transmission.

getDepositDataHash

This function generates a unique hash for the deposit data by assembling various pieces of information from the SwapData struct and other parameters. It ensures the integrity and uniqueness of the deposit data, which is crucial for tracking and verifying deposits accurately.

fillEncodedDepositData

The fillEncodedDepositData function is designed to populate a given variable with encoded deposit data. It takes three parameters: encodedDepositData, which is the placeholder for the deposit data, networkId, which identifies the sender’s network, and swapSequence, which is the current swap sequence number. The function uses inline assembly to copy the deposit data into the specified variable and then appends the network ID, the address, and the swap sequence to the encoded data.

swap

The swap function executes a swap operation using a specified router and a given amount of native currency. It takes the router’s address and the native amount as parameters and returns the amount received from the swap operation and a success flag. The function uses inline assembly to prepare the input data for the swap, including the function selector for swapWithoutSignature, and then performs the call to the router. If the call is successful, it retrieves the output amount from the response.

swapIn

The swapIn function executes an inbound swap operation using the provided data and addresses. It takes several parameters: swapData, which contains the details of the swap; encodedDepositData, which holds the encoded deposit data; fromAddress, the address from which the swap originates; routerAddress, the address of the router contract for the swap; and weth, the address of the Wrapped Ether contract. The function returns the amount received as output from the swap operation. The function first checks if the toAddress in swapData matches the current contract address, reverting with an InvalidToAddress error if it does not. It then verifies if the fromAssetAddress is native and if the msg.value is less than the amountIn, reverting with an InvalidAmountIn error if true. Depending on the asset addresses involved, the function performs different operations. If the fromAssetAddress is native and the toAssetAddress is weth, it wraps the native amount into weth. If the fromAssetAddress is weth and the toAssetAddress is native, it transfers the weth from the fromAddress to the contract and unwraps it. If both asset addresses are the same, it simply transfers the amount from the fromAddress to the contract. For other cases, it handles the swap operation by transferring the fromAssetAddress from the fromAddress to the contract, approving the router to spend the amount, and then calling the swap function. If the swap is unsuccessful, it reverts with the returned data. Finally, the function emits a SwapIn event with details of the swap operation, including the fromAddress, toAddress, asset addresses, total amount, output amount, and the encoded deposit data.

swapOut

The swapOut function executes an outbound swap operation using the provided swap and deposit data. It takes several parameters: swapData, which contains the swap details; depositAmount, which is the bridged amount to be swapped; depositDataHash, a hash of the deposit data; routerAddress, the address of the router contract for the swap; and weth, the address of the Wrapped Ether contract. The function returns the amount received as output from the swap operation. The function first checks if the depositAmount matches the sum of amountIn and gasFee from swapData, reverting with an InvalidDepositAmount error if it does not. If there is a gas fee, it transfers this fee to the sender. For the main swap operation, the function handles different scenarios based on the asset addresses involved. If the fromAssetAddress is weth and the toAssetAddress is native, it unwraps the weth and transfers the native amount to the toAddress. If the fromAssetAddress is native and the toAssetAddress is weth, it wraps the native amount into weth and transfers it to the toAddress. If both asset addresses are the same, it simply transfers the amount to the toAddress. For other cases, it prepares for the swap by approving the router to spend the fromAssetAddress amount and then calls the swap function. If the swap is unsuccessful, it reverts to transferring the fromAssetAddress amount to the toAddress and sets the toAssetAddress to fromAssetAddress. Finally, the function emits a SwapOut event with details of the swap operation, including the sender, toAddress, asset addresses, total amount, output amount, and the deposit data hash.

Last updated