# TradingStorageUtils

*GNSTradingStorage facet external library*

## initializeTradingStorage

```solidity
function initializeTradingStorage(address _gns, address _gnsStaking, address[] _collaterals, address[] _gTokens) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradingActivated

```solidity
function updateTradingActivated(enum ITradingStorage.TradingActivated _activated) public
```

*Check ITradingStorageUtils interface for documentation*

## addCollateral

```solidity
function addCollateral(address _collateral, address _gToken) public
```

*Check ITradingStorageUtils interface for documentation*

## toggleCollateralActiveState

```solidity
function toggleCollateralActiveState(uint8 _collateralIndex) external
```

*Check ITradingStorageUtils interface for documentation*

## updateGToken

```solidity
function updateGToken(address _collateral, address _gToken) external
```

*Check ITradingStorageUtils interface for documentation*

## storeTrade

```solidity
function storeTrade(struct ITradingStorage.Trade _trade, struct ITradingStorage.TradeInfo _tradeInfo) external returns (struct ITradingStorage.Trade)
```

*Check ITradingStorageUtils interface for documentation*

## updateTradeMaxClosingSlippageP

```solidity
function updateTradeMaxClosingSlippageP(struct ITradingStorage.Id _tradeId, uint16 _maxClosingSlippageP) external
```

## updateTradeCollateralAmount

```solidity
function updateTradeCollateralAmount(struct ITradingStorage.Id _tradeId, uint120 _collateralAmount) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradePosition

```solidity
function updateTradePosition(struct ITradingStorage.Id _tradeId, uint120 _collateralAmount, uint24 _leverage, uint64 _openPrice, bool _isPartialIncrease, bool _isPnlPositive) external
```

*Check ITradingStorageUtils interface for documentation*

## updateOpenOrderDetails

```solidity
function updateOpenOrderDetails(struct ITradingStorage.Id _tradeId, uint64 _openPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradeTp

```solidity
function updateTradeTp(struct ITradingStorage.Id _tradeId, uint64 _newTp) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradeSl

```solidity
function updateTradeSl(struct ITradingStorage.Id _tradeId, uint64 _newSl) external
```

*Check ITradingStorageUtils interface for documentation*

## closeTrade

```solidity
function closeTrade(struct ITradingStorage.Id _tradeId, bool _isPnlPositive) external
```

*Check ITradingStorageUtils interface for documentation*

## storePendingOrder

```solidity
function storePendingOrder(struct ITradingStorage.PendingOrder _pendingOrder) external returns (struct ITradingStorage.PendingOrder)
```

*Check ITradingStorageUtils interface for documentation*

## closePendingOrder

```solidity
function closePendingOrder(struct ITradingStorage.Id _orderId) external
```

*Check ITradingStorageUtils interface for documentation*

## getCollateral

```solidity
function getCollateral(uint8 _index) external view returns (struct ITradingStorage.Collateral)
```

*Check ITradingStorageUtils interface for documentation*

## isCollateralActive

```solidity
function isCollateralActive(uint8 _index) public view returns (bool)
```

*Check ITradingStorageUtils interface for documentation*

## isCollateralListed

```solidity
function isCollateralListed(uint8 _index) external view returns (bool)
```

*Check ITradingStorageUtils interface for documentation*

## getCollateralsCount

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

*Check ITradingStorageUtils interface for documentation*

## getCollaterals

```solidity
function getCollaterals() external view returns (struct ITradingStorage.Collateral[])
```

*Check ITradingStorageUtils interface for documentation*

## getCollateralIndex

```solidity
function getCollateralIndex(address _collateral) external view returns (uint8)
```

*Check ITradingStorageUtils interface for documentation*

## getTradingActivated

```solidity
function getTradingActivated() external view returns (enum ITradingStorage.TradingActivated)
```

*Check ITradingStorageUtils interface for documentation*

## getTraderStored

```solidity
function getTraderStored(address _trader) external view returns (bool)
```

*Check ITradingStorageUtils interface for documentation*

## getTrade

```solidity
function getTrade(address _trader, uint32 _index) external view returns (struct ITradingStorage.Trade)
```

*Check ITradingStorageUtils interface for documentation*

## getTradeInfo

```solidity
function getTradeInfo(address _trader, uint32 _index) external view returns (struct ITradingStorage.TradeInfo)
```

*Check ITradingStorageUtils interface for documentation*

## getPendingOrder

```solidity
function getPendingOrder(struct ITradingStorage.Id _orderId) external view returns (struct ITradingStorage.PendingOrder)
```

*Check ITradingStorageUtils interface for documentation*

## getTradePendingOrderBlock

```solidity
function getTradePendingOrderBlock(struct ITradingStorage.Id _tradeId, enum ITradingStorage.PendingOrderType _orderType) external view returns (uint256)
```

*Check ITradingStorageUtils interface for documentation*

## getCounters

```solidity
function getCounters(address _trader, enum ITradingStorage.CounterType _type) external view returns (struct ITradingStorage.Counter)
```

*Check ITradingStorageUtils interface for documentation*

## getGToken

```solidity
function getGToken(uint8 _collateralIndex) external view returns (address)
```

*Check ITradingStorageUtils interface for documentation*

## getTradeLiquidationParams

```solidity
function getTradeLiquidationParams(address _trader, uint32 _index) external view returns (struct IPairsStorage.GroupLiquidationParams)
```

*Check ITradingStorageUtils interface for documentation*

## getCurrentContractsVersion

```solidity
function getCurrentContractsVersion() external pure returns (enum ITradingStorage.ContractsVersion)
```

*Check ITradingStorageUtils interface for documentation*

## getTradersCount

```solidity
function getTradersCount() external view returns (uint256)
```

*Check ITradingStorageUtils interface for documentation*

## \_getSlot

```solidity
function _getSlot() internal pure returns (uint256)
```

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

## \_getStorage

```solidity
function _getStorage() internal pure returns (struct ITradingStorage.TradingStorage s)
```

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

## \_getMultiCollatDiamond

```solidity
function _getMultiCollatDiamond() internal view returns (contract IGNSMultiCollatDiamond)
```

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

## \_limitTpDistance

```solidity
function _limitTpDistance(uint64 _openPrice, uint24 _leverage, uint64 _tp, bool _long) public pure returns (uint64)
```

\_Limits take profit price distance for long/short based on '\_openPrice', '\_tp, '\_leverage' and sets an automatic TP if '*tp' is zero.*

### Parameters

| Name        | Type   | Description                              |
| ----------- | ------ | ---------------------------------------- |
| \_openPrice | uint64 | trade open price (1e10 precision)        |
| \_leverage  | uint24 | trade leverage (1e3 precision)           |
| \_tp        | uint64 | trade take profit price (1e10 precision) |
| \_long      | bool   | trade direction                          |

## \_limitSlDistance

```solidity
function _limitSlDistance(uint64 _openPrice, uint24 _leverage, uint64 _sl, bool _long, uint256 _liqPnlThresholdP) public pure returns (uint64)
```

\_Limits stop loss price distance for long/short based on '\_openPrice', '\_sl, '*leverage'.*

### Parameters

| Name               | Type    | Description                                 |
| ------------------ | ------- | ------------------------------------------- |
| \_openPrice        | uint64  | trade open price (1e10 precision)           |
| \_leverage         | uint24  | trade leverage (1e3 precision)              |
| \_sl               | uint64  | trade stop loss price (1e10 precision)      |
| \_long             | bool    | trade direction                             |
| \_liqPnlThresholdP | uint256 | liquidation pnl threshold percentage (1e10) |

## \_limitTradeSlDistance

```solidity
function _limitTradeSlDistance(struct ITradingStorage.Trade _trade, uint64 _newSl) public view returns (uint64)
```

*Limits trade stop loss price distance*

### Parameters

| Name    | Type                                                                                                        | Description  |
| ------- | ----------------------------------------------------------------------------------------------------------- | ------------ |
| \_trade | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#trade) | trade struct |
| \_newSl | uint64                                                                                                      |              |

## \_validateTrade

```solidity
function _validateTrade(struct ITradingStorage.Trade _trade) internal view
```

*Validation for trade struct (used by storeTrade and storePendingOrder for market open orders)*

### Parameters

| Name    | Type                                                                                                        | Description              |
| ------- | ----------------------------------------------------------------------------------------------------------- | ------------------------ |
| \_trade | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#trade) | trade struct to validate |


---

# 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/libraries/tradingstorageutils.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.
