# MagpieRouterV2

The MagpieRouterV2 contract serves as the implementation of the IMagpieRouterV2 interface. It provides functions for executing token swaps, managing operational states, and dynamically updating function selectors for handling commands. The functions enforce necessary checks and interact with underlying libraries like LibSwap, LibCommand, and LibUniswapV3 to execute operations. Key functionalities include pausing and unpausing the contract, handling swaps and silent swaps, and estimating gas usage for swaps. Access control is strictly enforced using OpenZeppelin's Ownable2Step, ensuring that only the contract owner can perform critical updates and operations.

### pause():

this function is used to pause the contract incase of any emergency.

### unpause():

this function is used to unpause the contract after the crisis has been resolved the the contract is redy to be used again.

### getSelector():

this function is used to retrieve the selector for the given command type from the Magpie Storage.

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">commandType
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>The different commands like approval, transfer, transferFrom etc.</td></tr></tbody></table>

Output:

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes4
</code></pre></td><td>The function selector for each of the commands.</td></tr></tbody></table>

### updateSelector():

the updateSelector function is an administrative tool within a smart contract that allows the owner to update the mapping of command types to function selectors.

**Input**

| Field       | Type   | Description                                                       |
| ----------- | ------ | ----------------------------------------------------------------- |
| commandType | uint16 | The different commands like approval, transfer, transferFrom etc. |
| selector    | bytes4 | The function selector for each of these commands.                 |

### getSelector():

Gets the selector at the specific commandType.

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">commandType
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>Identifier for each command. We have one selector / command.</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">selector
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes4
</code></pre></td><td>The function selector for the specified command.</td></tr></tbody></table>

### enforceDeadline():

the enforceDeadline function is used to enforce time constraints on certain operations within a smart contract. By checking if the current time has exceeded a specified deadline and reverting if it has, the function ensures that operations are only executed within their valid time windows.

**Input**

| Field    | Type    | Description                                                            |
| -------- | ------- | ---------------------------------------------------------------------- |
| deadline | uint256 | The timestamp in epochs beyond which the transaction will get expired. |

### fallback():

Handle uniswapV3SwapCallback requests from any protocol that is based on UniswapV3.&#x20;

**Input**

| Field        | Type           | Description                                                             |
| ------------ | -------------- | ----------------------------------------------------------------------- |
| amount0Delta | int256         | the changes in the amount of the first token involved in the swap       |
| amount1Delta | int256         | the changes in the amount of the second token involved in the swap      |
| data         | bytes calldata | a byte array containing additional information needed for the callback. |

### isTokenMovement():

This function is useful for determining whether a specific command action within a swap sequence involves moving tokens.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>commandAction</td><td>CommandAction</td><td><pre><code>enum CommandAction {
    Call,
    Approval,
    TransferFrom,
    Transfer,
    Wrap,
    Unwrap,
    Balance,
    UniswapV2,
    UniswapV3,
    TraderJoeV2_1,
    Solidly,
    KyberSwapClassic,
    Ambient
}
</code></pre></td></tr></tbody></table>

**Output**

| Field | Type | Description                                                                                                  |
| ----- | ---- | ------------------------------------------------------------------------------------------------------------ |
|       | bool | It will return true if there is a movement in tokens like approval, transfer, transferFrom, wrap and unwrap. |

### estimateSwapGas():

the estimateSwapGas function provides an external interface for estimating the gas cost of a token swap operation without actually executing the swap.

**Input**

| Field | Type           | Description                                             |
| ----- | -------------- | ------------------------------------------------------- |
|       | bytes calldata | A bytes array containing input parameters for swapping. |

**Output**

| Field     | Type    | Description                                |
| --------- | ------- | ------------------------------------------ |
| amountOut | uint256 | The amount received after swapping.        |
| gasUsed   | uint256 | The cost of gas while performing the swap. |

### swap():

the swap function provides an external interface for performing token swaps, with the additional feature of triggering events. It simplifies the swap process for external callers by handling the complexities internally through the execute function. The inclusion of event triggering makes this function suitable for scenarios where tracking swap operations is necessary

**Input**

| Field | Type           | Description                                             |
| ----- | -------------- | ------------------------------------------------------- |
|       | bytes calldata | A bytes array containing input parameters for swapping. |

**Output**

| Field     | Type    | Description                         |
| --------- | ------- | ----------------------------------- |
| amountOut | uint256 | The amount received after swapping. |

### silentSwap():

the silentSwap function provides an external interface for performing token swaps. It simplifies the swap process for external callers by handling the complexities internally through the execute function. The function's design allows for straightforward token swaps without additional overhead like event logging or gas estimation, making it suitable for users or contracts seeking to perform swaps with minimal extra features.

**Input**

| Field | Type           | Description                                             |
| ----- | -------------- | ------------------------------------------------------- |
|       | bytes calldata | A bytes array containing input parameters for swapping. |

**Output**

| Field     | Type    | Description                         |
| --------- | ------- | ----------------------------------- |
| amountOut | uint256 | The amount received after swapping. |

### execute():

the execute function is a comprehensive and versatile function that handles the execution of a sequence of commands, typically for the swap operation. It includes advanced features like gas estimation and event triggering.

**Input**

| Field        | Type | Description                                                   |
| ------------ | ---- | ------------------------------------------------------------- |
| triggerEvent | bool | An indicator if the function needs to trigger the swap event. |

**Output**

| Field     | Type    | Description                         |
| --------- | ------- | ----------------------------------- |
| amountOut | uint256 | The amount received after swapping. |
| gasUsed   | uint256 | The gas utilised during swapping.   |


---

# 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/flycn/developers/deprecated-magpie-contracts/magpierouterv2/magpierouterv2.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.
