# LibLayerZero

The `LibLayerZero` library provides functions for updating Layer Zero settings, handling Layer Zero data transfers (in and out), decoding and encoding data transfer payloads, registering and retrieving extended payloads, and enforcing authorization for Layer Zero operations. It consist of two structs:

### updateSettings()

updates the LayerZero settings of the Magpie aggregator contract by storing the given LayerZeroSettings struct in the storage of the aggregator 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">layerZeroSettings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">LayerZeroSettings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct LayerZeroSettings {
    address routerAddress;
}
</code></pre></td></tr></tbody></table>

### addLayerZeroChainIds()

used to add a mapping of network IDs to chain IDs in LayerZero. Takes two arrays as input, networkIds and chainIds, where the i-th element of the networkIds array corresponds to the i-th element of the chainIds array.

**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">networkIds
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16[]
</code></pre></td><td>An array of network identifiers provided by magpie team.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">chainIds
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16[]
</code></pre></td><td>An array of layer zero blockchain identifiers</td></tr></tbody></table>

### addLayerZeroNetworkIds()

used to add a mapping of chain IDs to network IDs in LayerZero. Takes two arrays as input, chainIds and networkIds, where the i-th element of the chainIds array corresponds to the i-th element of the networkIds array.

**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">chainIds
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16[]
</code></pre></td><td>An array of layer zero blockchain identifiers</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">networkIds
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16[]
</code></pre></td><td>An array of network identifiers provided by magpie team.</td></tr></tbody></table>

### decodeDataTransferInPayload()

takes a byte array as input and returns a LayerZeroDataTransferInData struct. This struct contains the fee that is required to be paid for the data transfer.

**Input**

<table><thead><tr><th width="282">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">dataTransferInPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload required for transfering data from one chain to another</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="289">Field</th><th width="290">Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">dataTransferInData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">LayerZeroDataTransferInData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct LayerZeroDataTransferInData {
    uint256 fee;
}
</code></pre></td></tr></tbody></table>

### encodeRemoteAndLocalAddresses()

this function is responsible for encoding the remote and local addresses into a `bytes` array.

**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">remoteAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes32
</code></pre></td><td>target address</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">localAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>source address</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="338.3333333333333">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">encodedRemoteAndLocalAddresses
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>concatenated target and source address</td></tr></tbody></table>

### dataTransfer()

responsible for transferring data using LayerZero. It takes two parameters as input: payload, which is the data that is to be transferred, and protocol, which is a DataTransferInProtocol struct that contains the payload and network ID.

**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">payload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload necessary for data transfer</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">protocol
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">DataTransferInProtocol
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct DataTransferInProtocol {
    uint16 networkId;
    DataTransferType dataTransferType;
    bytes payload;
}
</code></pre></td></tr></tbody></table>

### getPayload()

takes a byte as input, which is the data transfer out payload, and returns the extended payload for the message using the sender network ID, sender address, and core sequence of the message.

**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">dataTransferOutPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload necessary for data transfer</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">extendedPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload + extra information like which data transfer type has been used</td></tr></tbody></table>

### registerPayload()

takes a TransferKey struct and a byte array as input and registers the extended payload for the message in the storage of the aggregator 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">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><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">extendedPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload + extra information like which data transfer type has been used</td></tr></tbody></table>

### lzReceive()

is called when a message is received by the contract. It takes three parameters as input: senderChainId, which is the ID of the chain that sent the message, localAndRemoteAddresses, which is a byte array that contains the local and remote addresses of the sender, and extendedPayload, which is the extended payload for the message. This function validates the message and registers the extended payload for the message in the storage of the aggregator contract.

**Input**

<table><thead><tr><th width="286">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">senderChainId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>The layer zero chain identifier for the source chain</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">localAndRemoteAddresses
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>The address that will receive the data from layerzero</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">extendedPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload + extra information like which data transfer type has been used</td></tr></tbody></table>

### Events:

```solidity
event UpdateLayerZeroSettings(address indexed sender, LayerZeroSettings layerZeroSettings);
```

```solidity
event AddLayerZeroChainIds(address indexed sender, uint16[] networkIds, uint16[] chainIds);
```

```solidity
event AddLayerZeroNetworkIds(address indexed sender, uint16[] chainIds, uint16[] networkIds);
```

```solidity
event LzReceive(TransferKey transferKey, bytes payload);
```


---

# 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/data-transfer/liblayerzero.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.
