> ## 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.

# TradingStorageUtils

*GNSTradingStorage facet external library*

## initializeTradingStorage

```solidity theme={null}
function initializeTradingStorage(address _gns, address _gnsStaking, address[] _collaterals, address[] _gTokens) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradingActivated

```solidity theme={null}
function updateTradingActivated(enum ITradingStorage.TradingActivated _activated) public
```

*Check ITradingStorageUtils interface for documentation*

## addCollateral

```solidity theme={null}
function addCollateral(address _collateral, address _gToken) public
```

*Check ITradingStorageUtils interface for documentation*

## toggleCollateralActiveState

```solidity theme={null}
function toggleCollateralActiveState(uint8 _collateralIndex) external
```

*Check ITradingStorageUtils interface for documentation*

## updateGToken

```solidity theme={null}
function updateGToken(address _collateral, address _gToken) external
```

*Check ITradingStorageUtils interface for documentation*

## storeTrade

```solidity theme={null}
function storeTrade(struct ITradingStorage.Trade _trade, struct ITradingStorage.TradeInfo _tradeInfo) external returns (struct ITradingStorage.Trade)
```

*Check ITradingStorageUtils interface for documentation*

## updateTradeMaxClosingSlippageP

```solidity theme={null}
function updateTradeMaxClosingSlippageP(struct ITradingStorage.Id _tradeId, uint16 _maxClosingSlippageP) external
```

## updateTradeCollateralAmount

```solidity theme={null}
function updateTradeCollateralAmount(struct ITradingStorage.Id _tradeId, uint120 _collateralAmount) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradePosition

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

*Check ITradingStorageUtils interface for documentation*

## updateOpenOrderDetails

```solidity theme={null}
function updateOpenOrderDetails(struct ITradingStorage.Id _tradeId, uint64 _openPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradeTp

```solidity theme={null}
function updateTradeTp(struct ITradingStorage.Id _tradeId, uint64 _newTp) external
```

*Check ITradingStorageUtils interface for documentation*

## updateTradeSl

```solidity theme={null}
function updateTradeSl(struct ITradingStorage.Id _tradeId, uint64 _newSl) external
```

*Check ITradingStorageUtils interface for documentation*

## closeTrade

```solidity theme={null}
function closeTrade(struct ITradingStorage.Id _tradeId, bool _isPnlPositive) external
```

*Check ITradingStorageUtils interface for documentation*

## storePendingOrder

```solidity theme={null}
function storePendingOrder(struct ITradingStorage.PendingOrder _pendingOrder) external returns (struct ITradingStorage.PendingOrder)
```

*Check ITradingStorageUtils interface for documentation*

## closePendingOrder

```solidity theme={null}
function closePendingOrder(struct ITradingStorage.Id _orderId) external
```

*Check ITradingStorageUtils interface for documentation*

## getCollateral

```solidity theme={null}
function getCollateral(uint8 _index) external view returns (struct ITradingStorage.Collateral)
```

*Check ITradingStorageUtils interface for documentation*

## isCollateralActive

```solidity theme={null}
function isCollateralActive(uint8 _index) public view returns (bool)
```

*Check ITradingStorageUtils interface for documentation*

## isCollateralListed

```solidity theme={null}
function isCollateralListed(uint8 _index) external view returns (bool)
```

*Check ITradingStorageUtils interface for documentation*

## getCollateralsCount

```solidity theme={null}
function getCollateralsCount() external view returns (uint8)
```

*Check ITradingStorageUtils interface for documentation*

## getCollaterals

```solidity theme={null}
function getCollaterals() external view returns (struct ITradingStorage.Collateral[])
```

*Check ITradingStorageUtils interface for documentation*

## getCollateralIndex

```solidity theme={null}
function getCollateralIndex(address _collateral) external view returns (uint8)
```

*Check ITradingStorageUtils interface for documentation*

## getTradingActivated

```solidity theme={null}
function getTradingActivated() external view returns (enum ITradingStorage.TradingActivated)
```

*Check ITradingStorageUtils interface for documentation*

## getTraderStored

```solidity theme={null}
function getTraderStored(address _trader) external view returns (bool)
```

*Check ITradingStorageUtils interface for documentation*

## getTrade

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

*Check ITradingStorageUtils interface for documentation*

## getTradeInfo

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

*Check ITradingStorageUtils interface for documentation*

## getPendingOrder

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

*Check ITradingStorageUtils interface for documentation*

## getTradePendingOrderBlock

```solidity theme={null}
function getTradePendingOrderBlock(struct ITradingStorage.Id _tradeId, enum ITradingStorage.PendingOrderType _orderType) external view returns (uint256)
```

*Check ITradingStorageUtils interface for documentation*

## getCounters

```solidity theme={null}
function getCounters(address _trader, enum ITradingStorage.CounterType _type) external view returns (struct ITradingStorage.Counter)
```

*Check ITradingStorageUtils interface for documentation*

## getGToken

```solidity theme={null}
function getGToken(uint8 _collateralIndex) external view returns (address)
```

*Check ITradingStorageUtils interface for documentation*

## getTradeLiquidationParams

```solidity theme={null}
function getTradeLiquidationParams(address _trader, uint32 _index) external view returns (struct IPairsStorage.GroupLiquidationParams)
```

*Check ITradingStorageUtils interface for documentation*

## getCurrentContractsVersion

```solidity theme={null}
function getCurrentContractsVersion() external pure returns (enum ITradingStorage.ContractsVersion)
```

*Check ITradingStorageUtils interface for documentation*

## getTradersCount

```solidity theme={null}
function getTradersCount() external view returns (uint256)
```

*Check ITradingStorageUtils 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 ITradingStorage.TradingStorage 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.*

## \_limitTpDistance

```solidity theme={null}
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 theme={null}
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 theme={null}
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#trade) | trade struct |
| \_newSl | uint64                                                                                                   |              |

## \_validateTrade

```solidity theme={null}
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#trade) | trade struct to validate |
