> 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/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/libraries/libtransferkey.md).

# LibTransferKey

The `LibTransferKey` library provides functionality for encoding, decoding, and validating `TransferKey` structs, which are used to represent transfer keys in the application.

```solidity
struct TransferKey {
    uint16 networkId;
    bytes32 senderAddress;
    uint64 swapSequence;
}
```

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>networkId
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>networkId of the current chain in Magpie protocol, it is different from the actual networkId<br>ethereum: 1<br>polygon: 2<br>bsc: 3<br>avalanche: 4<br>arbitrum: 5 <br>optimism: 6</td></tr><tr><td><pre><code>senderAddress
</code></pre></td><td><pre><code>bytes32
</code></pre></td><td>The address who initiated the transfer.</td></tr><tr><td><pre><code>swapSequence
</code></pre></td><td><pre><code>uint64
</code></pre></td><td>The magpie sequence for the current swap.</td></tr></tbody></table>

### encode():

In the `encode` function, a new bytes array (`payload`) of size 42 is created to store the encoded transfer key.

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">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>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>The transferKey concatenated and converted to bytes</td></tr></tbody></table>

### decode():

In the `decode` function, the assembly block is used to extract the values of each field from the `payload` array and store them in the `transferKey` struct.

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">payload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>The data which contains the transferKey.</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">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>

### validate():

The `validate` function checks if the `networkId`, `senderAddress`, and `swapSequence` fields of both `TransferKey` structs are equal. If any of the fields differ, indicating an invalid transfer key, the function reverts with an `InvalidTransferKey` error.

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">self
</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">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>
