> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gains.trade/llms.txt
> Use this file to discover all available pages before exploring further.

# TradingCallbacksUtils

*GNSTradingCallbacks facet internal library*

## tradingActivated

```solidity theme={null}
modifier tradingActivated()
```

*Modifier to only allow trading action when trading is activated (= revert if not activated)*

## tradingActivatedOrCloseOnly

```solidity theme={null}
modifier tradingActivatedOrCloseOnly()
```

*Modifier to only allow trading action when trading is activated or close only (= revert if paused)*

## initializeCallbacks

```solidity theme={null}
function initializeCallbacks(uint8 _vaultClosingFeeP) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## updateVaultClosingFeeP

```solidity theme={null}
function updateVaultClosingFeeP(uint8 _valueP) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## updateTreasuryAddress

```solidity theme={null}
function updateTreasuryAddress(address _treasury) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## claimPendingGovFees

```solidity theme={null}
function claimPendingGovFees() internal
```

*Check ITradingCallbacksUtils interface for documentation*

## openTradeMarketCallback

```solidity theme={null}
function openTradeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## closeTradeMarketCallback

```solidity theme={null}
function closeTradeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## executeTriggerOpenOrderCallback

```solidity theme={null}
function executeTriggerOpenOrderCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## executeTriggerCloseOrderCallback

```solidity theme={null}
function executeTriggerCloseOrderCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## updateLeverageCallback

```solidity theme={null}
function updateLeverageCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## increasePositionSizeMarketCallback

```solidity theme={null}
function increasePositionSizeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## decreasePositionSizeMarketCallback

```solidity theme={null}
function decreasePositionSizeMarketCallback(struct ITradingCallbacks.AggregatorAnswer _a) internal
```

*Check ITradingCallbacksUtils interface for documentation*

## getVaultClosingFeeP

```solidity theme={null}
function getVaultClosingFeeP() internal view returns (uint8)
```

*Check ITradingCallbacksUtils interface for documentation*

## getPendingGovFeesCollateral

```solidity theme={null}
function getPendingGovFeesCollateral(uint8 _collateralIndex) internal view returns (uint256)
```

*Check ITradingCallbacksUtils interface for documentation*

## validateTriggerOpenOrderCallback

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

*Check ITradingCallbacksUtils interface for documentation*

## validateTriggerCloseOrderCallback

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

*Check ITradingCallbacksUtils interface for documentation*

## \_getSlot

```solidity theme={null}
function _getSlot() internal pure returns (uint256)
```

*Returns storage slot to use when fetching storage relevant to library*

## \_getStorage

```solidity theme={null}
function _getStorage() internal pure returns (struct ITradingCallbacks.TradingCallbacksStorage s)
```

*Returns storage pointer for storage struct in diamond contract, at defined slot*

## \_getMultiCollatDiamond

```solidity theme={null}
function _getMultiCollatDiamond() internal view returns (contract IGNSMultiCollatDiamond)
```

*Returns current address as multi-collateral diamond interface to call other facets functions.*

## \_registerTrade

```solidity theme={null}
function _registerTrade(struct ITradingStorage.Trade _trade, struct ITradingStorage.PendingOrder _pendingOrder) internal returns (struct ITradingStorage.Trade)
```

*Registers a trade in storage, and handles all fees and rewards*

### Parameters

| Name           | Type                                                                                                                   | Description                 |
| -------------- | ---------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| \_trade        | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade)               | Trade to register           |
| \_pendingOrder | [ITradingStorage.PendingOrder](/developer/technical-reference/contracts/interfaces/types/itradingstorage#pendingorder) | Corresponding pending order |

### Return Values

| Name | Type                                                                                                     | Description            |
| ---- | -------------------------------------------------------------------------------------------------------- | ---------------------- |
| \[0] | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade) | Final registered trade |

## \_unregisterTrade

```solidity theme={null}
function _unregisterTrade(struct ITradingStorage.Trade _trade, int256 _profitP, enum ITradingStorage.PendingOrderType _orderType, uint256 _oraclePrice, uint256 _liqPrice) internal returns (uint256 tradeValueCollateral)
```

*Unregisters a trade from storage, and handles all fees and rewards*

### Parameters

| Name          | Type                                                                                                     | Description                                       |
| ------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------- |
| \_trade       | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade) | Trade to unregister                               |
| \_profitP     | int256                                                                                                   | Profit percentage (1e10)                          |
| \_orderType   | enum ITradingStorage.PendingOrderType                                                                    | pending order type                                |
| \_oraclePrice | uint256                                                                                                  | oracle price without closing spread/impact (1e10) |
| \_liqPrice    | uint256                                                                                                  | trade liquidation price (1e10)                    |

### Return Values

| Name                 | Type    | Description                                                                  |
| -------------------- | ------- | ---------------------------------------------------------------------------- |
| tradeValueCollateral | uint256 | Amount of collateral sent to trader, collateral + pnl (collateral precision) |

## \_openTradePrep

```solidity theme={null}
function _openTradePrep(struct ITradingStorage.Trade _trade, uint256 _executionPrice, uint256 _oraclePrice, uint256 _spreadP, uint256 _maxSlippageP) internal view returns (uint256 priceImpactP, uint256 priceAfterImpact, enum ITradingCallbacks.CancelReason cancelReason)
```

*Makes pre-trade checks: price impact, if trade should be cancelled based on parameters like: PnL, leverage, slippage, etc.*

### Parameters

| Name             | Type                                                                                                     | Description                      |
| ---------------- | -------------------------------------------------------------------------------------------------------- | -------------------------------- |
| \_trade          | [ITradingStorage](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade).Trade | trade input                      |
| \_executionPrice | uint256                                                                                                  | execution price (1e10 precision) |
| \_oraclePrice    | uint256                                                                                                  | oracle price (1e10 precision)    |
| \_spreadP        | uint256                                                                                                  | spread % (1e10 precision)        |
| \_maxSlippageP   | uint256                                                                                                  | max slippage % (1e3 precision)   |

## \_getPendingOrder

```solidity theme={null}
function _getPendingOrder(struct ITradingStorage.Id _orderId) internal view returns (struct ITradingStorage.PendingOrder)
```

*Returns pending order from storage*

### Parameters

| Name      | Type                                                                                               | Description |
| --------- | -------------------------------------------------------------------------------------------------- | ----------- |
| \_orderId | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage#id) | Order ID    |

### Return Values

| Name | Type                                                                                                                   | Description   |
| ---- | ---------------------------------------------------------------------------------------------------------------------- | ------------- |
| \[0] | [ITradingStorage.PendingOrder](/developer/technical-reference/contracts/interfaces/types/itradingstorage#pendingorder) | Pending order |

## \_getCollateralPriceUsd

```solidity theme={null}
function _getCollateralPriceUsd(uint8 _collateralIndex) internal view returns (uint256)
```

*Returns collateral price in USD*

### Parameters

| Name              | Type  | Description      |
| ----------------- | ----- | ---------------- |
| \_collateralIndex | uint8 | Collateral index |

### Return Values

| Name | Type    | Description             |
| ---- | ------- | ----------------------- |
| \[0] | uint256 | Collateral price in USD |

## \_getTrade

```solidity theme={null}
function _getTrade(address _trader, uint32 _index) internal view returns (struct ITradingStorage.Trade)
```

*Returns trade from storage*

### Parameters

| Name     | Type    | Description    |
| -------- | ------- | -------------- |
| \_trader | address | Trader address |
| \_index  | uint32  | Trade index    |

### Return Values

| Name | Type                                                                                                     | Description |
| ---- | -------------------------------------------------------------------------------------------------------- | ----------- |
| \[0] | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade) | Trade       |

## \_getTradeInfo

```solidity theme={null}
function _getTradeInfo(address _trader, uint32 _index) internal view returns (struct ITradingStorage.TradeInfo)
```

*Returns trade info from storage*

### Parameters

| Name     | Type    | Description    |
| -------- | ------- | -------------- |
| \_trader | address | Trader address |
| \_index  | uint32  | Trade index    |

### Return Values

| Name | Type                                                                                                             | Description |
| ---- | ---------------------------------------------------------------------------------------------------------------- | ----------- |
| \[0] | [ITradingStorage.TradeInfo](/developer/technical-reference/contracts/interfaces/types/itradingstorage#tradeinfo) | TradeInfo   |
