# LibBridge

provides functions for bridging assets into and out of a decentralized exchange system using either the Wormhole bridge or the Stargate bridge.

### **bridgeIn()**

The purpose of this function is to provide a unified way to bridge assets into a decentralized exchange system using either the Wormhole or Stargate bridge, depending on the bridge type specified in the `BridgeArgs` struct. This allows the decentralized exchange system to support multiple bridge types and switch between them as needed.

The function first checks the type of bridge specified in the `BridgeArgs` struct. If it is `Wormhole`, it calls the `bridgeIn` function of the `LibWormhole` library, passing in the necessary parameters. If it is `Stargate`, it calls the `bridgeIn` function of the `LibStargate` library. If the specified bridge type is neither `Wormhole` nor `Stargate`, the function reverts and throws an `InvalidBridgeType` error.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">recipientNetworkId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>network identifier of the target chain</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">BridgeArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct BridgeArgs {
    BridgeType bridgeType;
    bytes payload;
}
</code></pre></td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">amount
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>the amount that needs to be swapped</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">toAssetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>the target asset</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">tokenSequence
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint64
</code></pre></td><td>the sequence received after a successful bridgIn</td></tr></tbody></table>

### bridgeOut()

Similar to the `bridgeIn` function but handles withdrawals of assets instead. It takes in a `BridgeArgs` struct that specifies the bridge type and the payload data for the bridge transaction. It also takes in a Transaction struct that contains information about the transaction being processed, such as the recipient address and the amount being withdrawn. Finally, it takes in a `TransferKey` struct that contains information needed to validate the withdrawal transaction. The `bridgeOut` function then delegates to the appropriate bridge implementation based on the bridge type specified in the `BridgeArgs` struct. If the bridge type is `BridgeType.Wormhole`, it calls the `bridgeOut` function in `LibWormhole`. If the bridge type is `BridgeType.Stargate`, it calls the `bridgeOut` function in `LibStargate`.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">BridgeArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct BridgeArgs {
    BridgeType bridgeType;
    bytes payload;
}
</code></pre></td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transaction
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">Transaction
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct Transaction {
    DataTransferType dataTransferType;
    BridgeType bridgeType;
    uint16 recipientNetworkId;
    bytes32 fromAssetAddress;
    bytes32 toAssetAddress;
    bytes32 toAddress;
    bytes32 recipientAggregatorAddress;
    uint256 amountOutMin;
    uint256 swapOutGasFee;
}
</code></pre></td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transferKey
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">TransferKey
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct TransferKey {
    uint16 networkId;
    bytes32 senderAddress;
    uint64 swapSequence;
}
</code></pre></td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">amount
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount received after bridging out.</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/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/bridge/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.
