> 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/libraries/libswap.md).

# LibSwap

```solidity
struct SwapData {
    uint16 amountsOffset;
    uint16 dataOffset;
    uint16 commandsOffset;
    uint16 commandsOffsetEnd;
    uint16 outputsLength;
    uint256 amountIn;
    address toAddress;
    address fromAssetAddress;
    address toAssetAddress;
    uint256 deadline;
    uint256 amountOutMin;
}
```

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>amountsOffset
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>This is a 16-bit unsigned integer representing the offset for the amounts section in the transaction calldata. Offsets are used to locate specific data within a block of calldata.</td></tr><tr><td><pre><code>dataOffset
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>Similar to amountsOffset, this is a 16-bit unsigned integer representing the offset for another data section in the calldata. The specific nature of this data depends on the context in which SwapData is used.</td></tr><tr><td><pre><code>commandsOffset
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>This is a 16-bit unsigned integer indicating the starting point of the commands section in the calldata. Commands in the context of a swap refers to specific actions.</td></tr><tr><td><pre><code>commandsOffsetEnd
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>This is a 16-bit unsigned integer marking the end of the commands section in the calldata. Knowing the start and end of the commands section helps in parsing and executing them correctly.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">outputsLength
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>Represents the length of all of the commands.</td></tr><tr><td><pre><code>amountIn
</code></pre></td><td><pre><code>uint256
</code></pre></td><td>A 256-bit unsigned integer representing the amount of the asset being provided in the swap.</td></tr><tr><td><pre><code>toAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>This is the Ethereum address to which the output of the swap (the swapped asset) will be sent.</td></tr><tr><td><pre><code>fromAssetAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>The Ethereum address of the asset being swapped from. This could be the contract address of an ERC-20 token or another type of asset.</td></tr><tr><td><pre><code>toAssetAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>The Ethereum address of the asset being swapped to. Like fromAssetAddress, this is typically the contract address of the target ERC-20 token or other asset types.</td></tr><tr><td><pre><code>deadline
</code></pre></td><td><pre><code>uint256
</code></pre></td><td>A timestamp (usually in seconds since the Unix epoch) indicating the deadline by which the swap must be completed.</td></tr><tr><td><pre><code>amountOutMin
</code></pre></td><td><pre><code>uint256
</code></pre></td><td>The minimum amount of the output asset that must be received for the swap to be considered successful.</td></tr></tbody></table>

### getAmountIn():

getAmountIn is a function that iterates over a portion of the transaction calldata, extracting and summing up amounts of an asset.

**Input:**

| Field          | Type   | Description                          |
| -------------- | ------ | ------------------------------------ |
| startOffset    | uint16 | The location starting position       |
| endOffset      | uint16 | The location ending position         |
| positionOffset | uint16 | The location of the current position |

**Ouput:**

| Field    | Type    | Description                      |
| -------- | ------- | -------------------------------- |
| amountIn | uint256 | The Amount for the current swap. |

### getFirstAmountIn():

the getFirstAmountIn function is a utility function used to extract the first amount of an asset involved in a swap operation directly from the calldata of a transaction.

**Input:**

| Field          | Type   | Description                                     |
| -------------- | ------ | ----------------------------------------------- |
| swapArgsOffset | uint16 | The location where the swapping data is stored. |

**Output:**

| Field    | Type    | Description          |
| -------- | ------- | -------------------- |
| amountIn | uint256 | The first amount in. |

### getData():

getData is a function that parses and organizes data from a transaction's calldata for a token swap operation.

**Input:**

| Field          | Type   | Description                                     |
| -------------- | ------ | ----------------------------------------------- |
| swapArgsOffset | uint16 | The location where the swapping data is stored. |

**Output:**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>swapData</td><td>SwapData memory</td><td><pre class="language-solidity"><code class="lang-solidity">struct SwapData {
    uint16 amountsOffset;
    uint16 dataOffset;
    uint16 commandsOffset;
    uint16 commandsOffsetEnd;
    uint256 amountIn;
    address toAddress;
    address fromAssetAddress;
    address toAssetAddress;
    uint256 deadline;
    uint256 amountOutMin;
}
</code></pre></td></tr></tbody></table>
