# 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.

<table data-full-width="false"><thead><tr><th width="246">Function Name</th><th>Description (Business Logic)</th></tr></thead><tbody><tr><td><mark style="color:red;"><strong>decodeDepositDataHash</strong></mark></td><td>This function converts a deposit data hash from a <mark style="color:red;"><strong>bytes</strong></mark> format to a <mark style="color:red;"><strong>bytes32</strong></mark> format. This conversion is essential for handling and verifying deposit data within the contract.</td></tr><tr><td><mark style="color:red;"><strong>encodeDepositDataHash</strong></mark></td><td>This function converts a deposit data hash from a <mark style="color:red;"><strong>bytes32</strong></mark> format back to a <mark style="color:red;"><strong>bytes</strong></mark> format. This is useful for encoding deposit data for storage or transmission.</td></tr><tr><td><mark style="color:red;"><strong>getDepositDataHash</strong></mark></td><td>This function generates a unique hash for the deposit data by assembling various pieces of information from the <mark style="color:red;"><strong>SwapData</strong></mark> struct and other parameters. It ensures the integrity and uniqueness of the deposit data, which is crucial for tracking and verifying deposits accurately.</td></tr><tr><td><mark style="color:red;"><strong>fillEncodedDepositData</strong></mark> </td><td>The <mark style="color:red;"><strong>fillEncodedDepositData</strong></mark> function is designed to populate a given variable with encoded deposit data. It takes three parameters: <mark style="color:red;"><strong>encodedDepositData</strong></mark>, which is the placeholder for the deposit data, <mark style="color:red;"><strong>networkId</strong></mark>, which identifies the sender’s network, and <mark style="color:red;"><strong>swapSequence</strong></mark>, 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.</td></tr><tr><td><mark style="color:red;"><strong>swap</strong></mark> </td><td>The <mark style="color:red;"><strong>swap</strong></mark> 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 <mark style="color:red;"><strong>swapWithoutSignature</strong></mark>, and then performs the call to the router. If the call is successful, it retrieves the output amount from the response.</td></tr><tr><td><mark style="color:red;"><strong>swapIn</strong></mark> </td><td>The <mark style="color:red;"><strong>swapIn</strong></mark> function executes an inbound swap operation using the provided data and addresses. It takes several parameters: <mark style="color:red;"><strong>swapData</strong></mark>, which contains the details of the swap; <mark style="color:red;"><strong>encodedDepositData</strong></mark>, which holds the encoded deposit data; <mark style="color:red;"><strong>fromAddress</strong></mark>, the address from which the swap originates; <mark style="color:red;"><strong>routerAddress</strong></mark>, the address of the router contract for the swap; and <mark style="color:red;"><strong>weth</strong></mark>, 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 <mark style="color:red;"><strong>toAddress</strong></mark> in <mark style="color:red;"><strong>swapData</strong></mark> matches the current contract address, reverting with an <mark style="color:red;"><strong>InvalidToAddress</strong></mark> error if it does not. It then verifies if the <mark style="color:red;"><strong>fromAssetAddress</strong></mark> is native and if the <mark style="color:red;"><strong>msg.value</strong></mark> is less than the <mark style="color:red;"><strong>amountIn</strong></mark>, reverting with an <mark style="color:red;"><strong>InvalidAmountIn</strong></mark> error if true. Depending on the asset addresses involved, the function performs different operations. If the fromAssetAddress is native and the <mark style="color:red;"><strong>toAssetAddress</strong></mark> is <mark style="color:red;"><strong>weth</strong></mark>, it wraps the native amount into <mark style="color:red;"><strong>weth</strong></mark>. If the <mark style="color:red;"><strong>fromAssetAddress</strong></mark> is <mark style="color:red;"><strong>weth</strong></mark> and the <mark style="color:red;"><strong>toAssetAddress</strong></mark> is native, it transfers the <mark style="color:red;"><strong>weth</strong></mark> from the <mark style="color:red;"><strong>fromAddress</strong></mark> to the contract and unwraps it. If both asset addresses are the same, it simply transfers the amount from the <mark style="color:red;"><strong>fromAddress</strong></mark> to the contract. For other cases, it handles the swap operation by transferring the <mark style="color:red;"><strong>fromAssetAddress</strong></mark> from the <mark style="color:red;"><strong>fromAddress</strong></mark> 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 <mark style="color:red;"><strong>SwapIn</strong></mark> event with details of the swap operation, including the <mark style="color:red;"><strong>fromAddress</strong></mark>, <mark style="color:red;"><strong>toAddress</strong></mark>, asset addresses, total amount, output amount, and the encoded deposit data.</td></tr><tr><td><mark style="color:red;"><strong>swapOut</strong></mark> </td><td>The <mark style="color:red;"><strong>swapOut</strong></mark> function executes an outbound swap operation using the provided swap and deposit data. It takes several parameters: <mark style="color:red;"><strong>swapData</strong></mark>, which contains the swap details; <mark style="color:red;"><strong>depositAmount</strong></mark>, which is the bridged amount to be swapped; <mark style="color:red;"><strong>depositDataHash</strong></mark>, a hash of the deposit data; <mark style="color:red;"><strong>routerAddress</strong></mark>, the address of the router contract for the swap; and <mark style="color:red;"><strong>weth</strong></mark>, 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 <mark style="color:red;"><strong>depositAmount</strong></mark> matches the sum of <mark style="color:red;"><strong>amountIn</strong></mark> and <mark style="color:red;"><strong>gasFee</strong></mark> from <mark style="color:red;"><strong>swapData</strong></mark>, reverting with an <mark style="color:red;"><strong>InvalidDepositAmount</strong></mark> 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 <mark style="color:red;"><strong>fromAssetAddress</strong></mark> is <mark style="color:red;"><strong>weth</strong></mark> and the <mark style="color:red;"><strong>toAssetAddress</strong></mark> is native, it unwraps the <mark style="color:red;"><strong>weth</strong></mark> and transfers the native amount to the <mark style="color:red;"><strong>toAddress</strong></mark>. If the <mark style="color:red;"><strong>fromAssetAddress</strong></mark> is native and the <mark style="color:red;"><strong>toAssetAddress</strong></mark> is <mark style="color:red;"><strong>weth</strong></mark>, it wraps the native amount into <mark style="color:red;"><strong>weth</strong></mark> and transfers it to the toAddress. If both asset addresses are the same, it simply transfers the amount to the <mark style="color:red;"><strong>toAddress</strong></mark>. For other cases, it prepares for the swap by approving the router to spend the <mark style="color:red;"><strong>fromAssetAddress</strong></mark> amount and then calls the swap function. If the <mark style="color:red;"><strong>swap</strong></mark> is unsuccessful, it reverts to transferring the <mark style="color:red;"><strong>fromAssetAddress</strong></mark> amount to the <mark style="color:red;"><strong>toAddress</strong></mark> and sets the <mark style="color:red;"><strong>toAssetAddress</strong></mark> to <mark style="color:red;"><strong>fromAssetAddress</strong></mark>. Finally, the function emits a <mark style="color:red;"><strong>SwapOut</strong></mark> event with details of the swap operation, including the sender, <mark style="color:red;"><strong>toAddress</strong></mark>, asset addresses, total amount, output amount, and the deposit data hash.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fly.trade/flycn/developers/fly.trade-contracts/libbridge.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
