> For the complete documentation index, see [llms.txt](https://docs.fly.trade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fly.trade/flycn/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/bridge/libwormhole.md).

# LibWormhole

It provides functions for interacting with the Wormhole bridge in a decentralized exchange system.

### WormhokeBridgeInData

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">recipientBridgeChainId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>the receipient chain id as per wormhole standard.</td></tr></tbody></table>

### updateSettings()

updates the `stargateSettings` struct in the contract's `AppStorage` struct.

**Input**

<table><thead><tr><th width="273">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre class="language-solidity"><code class="lang-solidity">wormholeBridgeSettings
</code></pre></td><td><pre class="language-solidity"><code class="lang-solidity">WormholeBridgeSettings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct WormholeBridgeSettings {
    address bridgeAddress;
}
</code></pre></td></tr></tbody></table>

### &#x20;normalize()

normalizes an amount from one decimal precision to another.

**Input**

<table><thead><tr><th width="288.3333333333333">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">fromDecimals
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint8
</code></pre></td><td>decimals of the source asset</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">toDecimals
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint8
</code></pre></td><td>decimals of the target asset</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>amount that needs to be normalized</td></tr></tbody></table>

**Output**

<table><thead><tr><th>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 normalized amount</td></tr></tbody></table>

### &#x20;denormalize()

denormalizes an amount from one decimal precision to another.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">fromDecimals
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint8
</code></pre></td><td>decimal of the source asset</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">toDecimals
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint8
</code></pre></td><td>decimal of the target asset</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>amount that needs to be denormalized</td></tr></tbody></table>

**Output**

<table><thead><tr><th>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 denormalized amount</td></tr></tbody></table>

### getRecipientBridgeChainId

this function is responsible for extracting the recipient bridge chain ID from the provided bridge in payload.

**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">bridgeInPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload necessary for bridging in.</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="265">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">recipientBridgeChainId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>The receipient chain id fetched from the payload</td></tr></tbody></table>

### bridgeIn

this function is responsible for executing the bridge-in operation using the Wormhole bridge. It performs dust management operations for token amounts, approves the spending of tokens, and calls the `transferTokens` function of the Wormhole bridge contract to initiate the token transfer. The recipient bridge chain ID, Magpie aggregator address, and timestamp-based nonce are provided as arguments to the `transferTokens` function.

**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>The network identifier of the destination 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><pre><code>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 final token that needs to be received</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 bridging in</td></tr></tbody></table>

### bridgeOut

this function is responsible for executing the bridge-out operation using the Wormhole bridge. It verifies the token sequence, extracts the amount of tokens from the payload, performs denormalization if necessary, and completes the transfer by calling the `completeTransfer` function of the Wormhole bridge contract.

**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">bridgeOutPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload contains data that is necessary for bridging out</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></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>

### Event:

```solidity
event UpdateWormholeBridgeSettings(address indexed sender, 
WormholeBridgeSettings wormholeBridgeSettings);
```
