# ITradingCallbacksUtils

*Interface for GNSTradingCallbacks facet (inherits types and also contains functions, events, and custom errors)*

## initializeCallbacks

```solidity
function initializeCallbacks(uint8 _vaultClosingFeeP) external
```

### Parameters

| Name               | Type  | Description                         |
| ------------------ | ----- | ----------------------------------- |
| \_vaultClosingFeeP | uint8 | the % of closing fee going to vault |

## initializeTreasuryAddress

```solidity
function initializeTreasuryAddress(address _treasury) external
```

*Initialize the treasury address*

### Parameters

| Name       | Type    | Description          |
| ---------- | ------- | -------------------- |
| \_treasury | address | the treasury address |

## updateVaultClosingFeeP

```solidity
function updateVaultClosingFeeP(uint8 _valueP) external
```

*Update the % of closing fee going to vault*

### Parameters

| Name     | Type  | Description                         |
| -------- | ----- | ----------------------------------- |
| \_valueP | uint8 | the % of closing fee going to vault |

## updateTreasuryAddress

```solidity
function updateTreasuryAddress(address _treasury) external
```

*Updates the treasury address*

### Parameters

| Name       | Type    | Description              |
| ---------- | ------- | ------------------------ |
| \_treasury | address | the new treasury address |

## claimPendingGovFees

```solidity
function claimPendingGovFees() external
```

*Claim the pending gov fees for all collaterals*

## openTradeMarketCallback

```solidity
function openTradeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending open trade market order*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## closeTradeMarketCallback

```solidity
function closeTradeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending close trade market order*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## executeTriggerOpenOrderCallback

```solidity
function executeTriggerOpenOrderCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending open trigger order (for limit/stop orders)*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## executeTriggerCloseOrderCallback

```solidity
function executeTriggerCloseOrderCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending close trigger order (for tp/sl/liq orders)*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## updateLeverageCallback

```solidity
function updateLeverageCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending update leverage order*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## increasePositionSizeMarketCallback

```solidity
function increasePositionSizeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending increase position size market order*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## decreasePositionSizeMarketCallback

```solidity
function decreasePositionSizeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) external
```

*Executes a pending decrease position size market order*

### Parameters

| Name | Type                                                                                                                                  | Description                                         |
| ---- | ------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- |
| \_a  | [ITradingCallbacks.AggregatorAnswer](/developer/technical-reference/contracts/interfaces/types/itradingcallbacks.md#aggregatoranswer) | the price aggregator answer (order id, price, etc.) |

## getVaultClosingFeeP

```solidity
function getVaultClosingFeeP() external view returns (uint8)
```

*Returns the current vaultClosingFeeP value (%)*

## getPendingGovFeesCollateral

```solidity
function getPendingGovFeesCollateral(uint8 _collateralIndex) external view returns (uint256)
```

*Returns the current pending gov fees for a collateral index (collateral precision)*

## validateTriggerOpenOrderCallback

```solidity
function validateTriggerOpenOrderCallback(struct ITradingStorage.Id _tradeId, enum ITradingStorage.PendingOrderType _orderType, uint64 _open, uint64 _high, uint64 _low) external view returns (struct ITradingStorage.Trade t, enum ITradingCallbacks.CancelReason cancelReason, struct ITradingCallbacks.Values v)
```

*Makes open trigger (STOP/LIMIT) checks like slippage, price impact, missed targets and returns cancellation reason if any*

### Parameters

| Name        | Type                                                                                                  | Description                                |
| ----------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------ |
| \_tradeId   | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#id) | the id of the trade                        |
| \_orderType | enum ITradingStorage.PendingOrderType                                                                 | the pending order type                     |
| \_open      | uint64                                                                                                | the `open` value from an aggregator answer |
| \_high      | uint64                                                                                                | the `high` value from an aggregator answer |
| \_low       | uint64                                                                                                | the `low` value from an aggregator answer  |

## validateTriggerCloseOrderCallback

```solidity
function validateTriggerCloseOrderCallback(struct ITradingStorage.Id _tradeId, enum ITradingStorage.PendingOrderType _orderType, uint64 _open, uint64 _high, uint64 _low) external view returns (struct ITradingStorage.Trade t, enum ITradingCallbacks.CancelReason cancelReason, struct ITradingCallbacks.Values v)
```

*Makes close trigger (SL/TP/LIQ) checks like slippage and price impact and returns cancellation reason if any*

### Parameters

| Name        | Type                                                                                                  | Description                                |
| ----------- | ----------------------------------------------------------------------------------------------------- | ------------------------------------------ |
| \_tradeId   | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#id) | the id of the trade                        |
| \_orderType | enum ITradingStorage.PendingOrderType                                                                 | the pending order type                     |
| \_open      | uint64                                                                                                | the `open` value from an aggregator answer |
| \_high      | uint64                                                                                                | the `high` value from an aggregator answer |
| \_low       | uint64                                                                                                | the `low` value from an aggregator answer  |

## VaultClosingFeePUpdated

```solidity
event VaultClosingFeePUpdated(uint8 valueP)
```

*Emitted when vaultClosingFeeP is updated*

### Parameters

| Name   | Type  | Description                         |
| ------ | ----- | ----------------------------------- |
| valueP | uint8 | the % of closing fee going to vault |

## PendingGovFeesClaimed

```solidity
event PendingGovFeesClaimed(uint8 collateralIndex, uint256 amountCollateral)
```

*Emitted when gov fees are claimed for a collateral*

### Parameters

| Name             | Type    | Description                                       |
| ---------------- | ------- | ------------------------------------------------- |
| collateralIndex  | uint8   | the collateral index                              |
| amountCollateral | uint256 | the amount of fees claimed (collateral precision) |

## MarketExecuted

```solidity
event MarketExecuted(struct ITradingStorage.Id orderId, address user, uint32 index, struct ITradingStorage.Trade t, bool open, uint256 oraclePrice, uint256 marketPrice, uint256 liqPrice, uint256 priceImpactP, int256 percentProfit, uint256 amountSentToTrader, uint256 collateralPriceUsd)
```

*Emitted when a market order is executed (open/close)*

### Parameters

| Name               | Type                         | Description                                                  |
| ------------------ | ---------------------------- | ------------------------------------------------------------ |
| orderId            | struct ITradingStorage.Id    | the id of the corresponding pending market order             |
| user               | address                      | trade user                                                   |
| index              | uint32                       | trade index                                                  |
| t                  | struct ITradingStorage.Trade | the trade object                                             |
| open               | bool                         | true for a market open order, false for a market close order |
| oraclePrice        | uint256                      | the oracle price without spread/impact (1e10 precision)      |
| marketPrice        | uint256                      | the price at which the trade was executed (1e10 precision)   |
| liqPrice           | uint256                      | trade liquidation price (1e10 precision)                     |
| priceImpactP       | uint256                      | the price impact in percentage (1e10 precision)              |
| percentProfit      | int256                       | the profit in percentage (1e10 precision)                    |
| amountSentToTrader | uint256                      | the final amount of collateral sent to the trader            |
| collateralPriceUsd | uint256                      | the price of the collateral in USD (1e8 precision)           |

## LimitExecuted

```solidity
event LimitExecuted(struct ITradingStorage.Id orderId, address user, uint32 index, uint32 limitIndex, struct ITradingStorage.Trade t, address triggerCaller, enum ITradingStorage.PendingOrderType orderType, uint256 oraclePrice, uint256 marketPrice, uint256 liqPrice, uint256 priceImpactP, int256 percentProfit, uint256 amountSentToTrader, uint256 collateralPriceUsd, bool exactExecution)
```

*Emitted when a limit/stop order is executed*

### Parameters

| Name               | Type                                  | Description                                                |
| ------------------ | ------------------------------------- | ---------------------------------------------------------- |
| orderId            | struct ITradingStorage.Id             | the id of the corresponding pending trigger order          |
| user               | address                               | trade user                                                 |
| index              | uint32                                | trade index                                                |
| limitIndex         | uint32                                | limit index                                                |
| t                  | struct ITradingStorage.Trade          | the trade object                                           |
| triggerCaller      | address                               | the address that triggered the limit order                 |
| orderType          | enum ITradingStorage.PendingOrderType | the type of the pending order                              |
| oraclePrice        | uint256                               | the oracle price without spread/impact (1e10 precision)    |
| marketPrice        | uint256                               | the price at which the trade was executed (1e10 precision) |
| liqPrice           | uint256                               | trade liquidation price (1e10 precision)                   |
| priceImpactP       | uint256                               | the price impact in percentage (1e10 precision)            |
| percentProfit      | int256                                | the profit in percentage (1e10 precision)                  |
| amountSentToTrader | uint256                               | the final amount of collateral sent to the trader          |
| collateralPriceUsd | uint256                               | the price of the collateral in USD (1e8 precision)         |
| exactExecution     | bool                                  | true if guaranteed execution was used                      |

## MarketOpenCanceled

```solidity
event MarketOpenCanceled(struct ITradingStorage.Id orderId, address trader, uint256 pairIndex, enum ITradingCallbacks.CancelReason cancelReason)
```

*Emitted when a pending market open order is canceled*

### Parameters

| Name         | Type                                | Description                               |
| ------------ | ----------------------------------- | ----------------------------------------- |
| orderId      | struct ITradingStorage.Id           | order id of the pending market open order |
| trader       | address                             | address of the trader                     |
| pairIndex    | uint256                             | index of the trading pair                 |
| cancelReason | enum ITradingCallbacks.CancelReason | reason for the cancellation               |

## MarketCloseCanceled

```solidity
event MarketCloseCanceled(struct ITradingStorage.Id orderId, address trader, uint256 pairIndex, uint256 index, enum ITradingCallbacks.CancelReason cancelReason)
```

*Emitted when a pending market close order is canceled*

### Parameters

| Name         | Type                                | Description                                |
| ------------ | ----------------------------------- | ------------------------------------------ |
| orderId      | struct ITradingStorage.Id           | order id of the pending market close order |
| trader       | address                             | address of the trader                      |
| pairIndex    | uint256                             | index of the trading pair                  |
| index        | uint256                             | index of the trade for trader              |
| cancelReason | enum ITradingCallbacks.CancelReason | reason for the cancellation                |

## TriggerOrderCanceled

```solidity
event TriggerOrderCanceled(struct ITradingStorage.Id orderId, address triggerCaller, enum ITradingStorage.PendingOrderType orderType, enum ITradingCallbacks.CancelReason cancelReason)
```

*Emitted when a pending trigger order is canceled*

### Parameters

| Name          | Type                                  | Description                           |
| ------------- | ------------------------------------- | ------------------------------------- |
| orderId       | struct ITradingStorage.Id             | order id of the pending trigger order |
| triggerCaller | address                               | address of the trigger caller         |
| orderType     | enum ITradingStorage.PendingOrderType | type of the pending trigger order     |
| cancelReason  | enum ITradingCallbacks.CancelReason   | reason for the cancellation           |

## BorrowingFeeCharged

```solidity
event BorrowingFeeCharged(address trader, uint32 index, uint8 collateralIndex, uint256 amountCollateral)
```

### Parameters

| Name             | Type    | Description                           |
| ---------------- | ------- | ------------------------------------- |
| trader           | address | address of the trader                 |
| index            | uint32  | index of the trade                    |
| collateralIndex  | uint8   | index of the collateral               |
| amountCollateral | uint256 | amount charged (collateral precision) |


---

# 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.gains.trade/developer/technical-reference/contracts/interfaces/libraries/itradingcallbacksutils.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.
