# GmxV2CauldronRouterOrder

### init | For Order Creation

Always called by `GmxV2CauldronOrderAgent` during the order creation process. This function not only initializes the order but also triggers the order creation on the GMX Router.

```solidity
function init(address _cauldron, address _user, GmRouterOrderParams memory params) external payable
```

**Parameters**

| Name        | Type                  | Note                                                       |
| ----------- | --------------------- | ---------------------------------------------------------- |
| `_cauldron` | `address`             | Address of the Cauldron contract, linking the order to it. |
| `_user`     | `address`             | User address for whom the order is being initialized.      |
| `params`    | `GmRouterOrderParams` | Parameters defining the order's characteristics.           |

***

### cancelOrder | Specific to Cauldron

Cancels an open order on the GMX Router. This function is called by the Cauldron contract, typically in scenarios like liquidation or as part of the corresponding \_additionalCookActions.\
It is necessary to call it before processing certain actions as any open order could receive tokens and callbacks from GMX which would lead to unintended results.

```solidity
function cancelOrder() external onlyCauldron 
```

No parameters.

***

### withdrawFromOrder | Specific to Cauldron

Enables the Cauldron contract to withdraw from an order, depositing the token back into the degenBox. This function is always called as part of the additional cook action in the Cauldron contract.\
It can also call closeOrder on the cauldron after the withdrawing part, sending all remaining balance to the user.

This function is used to withdraw the short token from the order. Which happens as part of the deleverage process to the repay MIM.

```solidity
function withdrawFromOrder(address token, address to, uint256 amount, bool closeOrder) external onlyCauldron
```

**Parameters**

| Name         | Type      | Note                                         |
| ------------ | --------- | -------------------------------------------- |
| `token`      | `address` | Token address for the withdrawal.            |
| `to`         | `address` | Recipient address for the withdrawn funds.   |
| `amount`     | `uint256` | Amount to withdraw from the order.           |
| `closeOrder` | `bool`    | Whether to close the order after withdrawal. |

***

### sendValueInCollateral | Specific to Cauldron

Transfers a specified amount of DegenBox shares to the liquidator during a liquidation process. This function is used exclusively in liquidation scenarios.

This function transfers an amount of the short token to the liquidator, this amount is calculated using an amount of collateralShare as an input to be compatible with the liquidation function. It computes the value of this in shortToken and transfers it.

```solidity
function sendValueInCollateral(address recipient, uint256 shareMarketToken) external onlyCauldron
```

**Parameters**

| Name               | Type      | Note                                                |
| ------------------ | --------- | --------------------------------------------------- |
| `recipient`        | `address` | The recipient to receive the collateral.            |
| `shareMarketToken` | `uint256` | Share of the market token to be sent as collateral. |

***

### orderValueInCollateral | Public View

Calculates the value of the order in terms of collateral, used in the `_isSolvent` function in the Cauldron contract to include the order's value in solvency checks.

```solidity
function orderValueInCollateral() public view returns (uint256)
```

No parameters.

***

### isActive | Public View

Checks if the order is active, used in liquidation processes to know if the order needs to be cancelled.

```solidity
function isActive() public view returns (bool)
```

No parameters.

***

### afterDepositExecution | Callback for Deposit Handler

This callback function is triggered after a deposit order is executed. It verifies that the deposit originated from this contract and then proceeds to deposit the received market tokens as collateral back into the cauldron. This action effectively converts the tokens from the deposit into collateral within the cauldron and then closes the order associated with the user.

```solidity
function afterDepositExecution(bytes32 /*key*/, IGmxV2Deposit.Props memory deposit, IGmxV2EventUtils.EventLogData memory /*eventData*/) external override onlyDepositHandler
```

**Parameters**

| Name        | Type                            | Note                                    |
| ----------- | ------------------------------- | --------------------------------------- |
| `key`       | `bytes32`                       | The unique key identifying the deposit. |
| `deposit`   | `IGmxV2Deposit.Props`           | Properties of the executed deposit.     |
| `eventData` | `IGmxV2EventUtils.EventLogData` | Additional event data (unused here).    |

***

### afterWithdrawalCancellation | Callback for Withdrawal Handler

This callback is invoked after a withdrawal order is cancelled. It ensures that the withdrawal was associated with this contract and then follows the same process as `afterDepositExecution`, depositing the market tokens as collateral in the cauldron.

When a withdrawal cancels or fails, GMX will send back to the order gm tokens, not the short token. In this case, we use this callback to ensure those gm tokens are deposited back as collateral.

```solidity
function afterWithdrawalCancellation(bytes32 /*key*/, IGmxV2Withdrawal.Props memory withdrawal, IGmxV2EventUtils.EventLogData memory /*eventData*/) external override onlyWithdrawalHandler
```

**Parameters**

| Name         | Type                            | Note                                       |
| ------------ | ------------------------------- | ------------------------------------------ |
| `key`        | `bytes32`                       | The unique key identifying the withdrawal. |
| `withdrawal` | `IGmxV2Withdrawal.Props`        | Properties of the cancelled withdrawal.    |
| `eventData`  | `IGmxV2EventUtils.EventLogData` | Additional event data (unused here).       |

***

#### Context for Callback Functions

The `afterDepositExecution` and `afterWithdrawalCancellation` callbacks are called by Gmx's keeper as part of the Deposit or Withdrawal Handler. Our implementation of these function utilize the internal function `_depositMarketTokensAsCollateral`. This function takes the market tokens received from the executed deposit or cancelled withdrawal, transfers them to the DegenBox, and then deposits them as collateral in the Cauldron contract. This process adds the tokens back as collateral, directly impacting the user's position within the cauldron, and finalizes the order by closing it.
