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

# TradingInteractionsUtils

*GNSTradingInteractions 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)*

## notDelegatedAction

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

*Modifier to prevent calling function from delegated action*

## initializeTrading

```solidity theme={null}
function initializeTrading(uint16 _marketOrdersTimeoutBlocks, address[] _usersByPassTriggerLink) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateMarketOrdersTimeoutBlocks

```solidity theme={null}
function updateMarketOrdersTimeoutBlocks(uint16 _valueBlocks) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateByPassTriggerLink

```solidity theme={null}
function updateByPassTriggerLink(address[] _users, bool[] _shouldByPass) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## setTradingDelegate

```solidity theme={null}
function setTradingDelegate(address _delegate) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## removeTradingDelegate

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

*Check ITradingInteractionsUtils interface for documentation*

## delegatedTradingAction

```solidity theme={null}
function delegatedTradingAction(address _trader, bytes _callData) internal returns (bytes)
```

*Check ITradingInteractionsUtils interface for documentation*

## openTrade

```solidity theme={null}
function openTrade(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## openTradeNative

```solidity theme={null}
function openTradeNative(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateMaxClosingSlippageP

```solidity theme={null}
function updateMaxClosingSlippageP(uint32 _index, uint16 _maxClosingSlippageP) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## closeTradeMarket

```solidity theme={null}
function closeTradeMarket(uint32 _index, uint64 _expectedPrice) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateOpenOrder

```solidity theme={null}
function updateOpenOrder(uint32 _index, uint64 _openPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## cancelOpenOrder

```solidity theme={null}
function cancelOpenOrder(uint32 _index) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateTp

```solidity theme={null}
function updateTp(uint32 _index, uint64 _newTp) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateSl

```solidity theme={null}
function updateSl(uint32 _index, uint64 _newSl) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateLeverage

```solidity theme={null}
function updateLeverage(uint32 _index, uint24 _newLeverage) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## increasePositionSize

```solidity theme={null}
function increasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice, uint16 _maxSlippageP) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## decreasePositionSize

```solidity theme={null}
function decreasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## triggerOrder

```solidity theme={null}
function triggerOrder(uint256 _packed) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## cancelOrderAfterTimeout

```solidity theme={null}
function cancelOrderAfterTimeout(uint32 _orderIndex) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## getWrappedNativeToken

```solidity theme={null}
function getWrappedNativeToken() internal view returns (address)
```

*Check ITradingInteractionsUtils interface for documentation*

## isWrappedNativeToken

```solidity theme={null}
function isWrappedNativeToken(address _token) internal view returns (bool)
```

*Check ITradingInteractionsUtils interface for documentation*

## getTradingDelegate

```solidity theme={null}
function getTradingDelegate(address _trader) internal view returns (address)
```

*Check ITradingInteractionsUtils interface for documentation*

## getMarketOrdersTimeoutBlocks

```solidity theme={null}
function getMarketOrdersTimeoutBlocks() internal view returns (uint16)
```

*Check ITradingInteractionsUtils interface for documentation*

## getByPassTriggerLink

```solidity theme={null}
function getByPassTriggerLink(address _user) internal view returns (bool)
```

*Check ITradingInteractionsUtils 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 ITradingInteractions.TradingInteractionsStorage 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.*

## \_openTrade

```solidity theme={null}
function _openTrade(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer, bool _isNative) internal
```

*Internal function for openTrade and openTradeNative*

### Parameters

| Name           | Type                                                                                                     | Description                                                   |
| -------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------- |
| \_trade        | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade) | trade data                                                    |
| \_maxSlippageP | uint16                                                                                                   | max slippage percentage (1e3 precision)                       |
| \_referrer     | address                                                                                                  | referrer address                                              |
| \_isNative     | bool                                                                                                     | if true we skip the collateral transfer from user to contract |

## \_checkNoPendingTrigger

```solidity theme={null}
function _checkNoPendingTrigger(struct ITradingStorage.Id _tradeId, enum ITradingStorage.PendingOrderType _orderType) internal view
```

*Revert if there is an active pending order for the trade*

### Parameters

| Name        | Type                                                                                               | Description |
| ----------- | -------------------------------------------------------------------------------------------------- | ----------- |
| \_tradeId   | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage#id) | trade id    |
| \_orderType | enum ITradingStorage.PendingOrderType                                                              | order type  |

## \_getPriceTriggerOrder

```solidity theme={null}
function _getPriceTriggerOrder(struct ITradingStorage.Trade _trade, struct ITradingStorage.Id _orderId, enum ITradingStorage.PendingOrderType _orderType, uint256 _positionSizeCollateral) internal
```

*Initiate price aggregator request for trigger order*

### Parameters

| Name                     | Type                                                                                                     | Description                                               |
| ------------------------ | -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------- |
| \_trade                  | [ITradingStorage.Trade](/developer/technical-reference/contracts/interfaces/types/itradingstorage#trade) | trade                                                     |
| \_orderId                | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage#id)       | order id                                                  |
| \_orderType              | enum ITradingStorage.PendingOrderType                                                                    | order type                                                |
| \_positionSizeCollateral | uint256                                                                                                  | position size in collateral tokens (collateral precision) |

## \_wrapNativeToken

```solidity theme={null}
function _wrapNativeToken(uint8 _collateralIndex) internal returns (uint120)
```

*Receives native token and sends back wrapped token to user*

### Parameters

| Name              | Type  | Description             |
| ----------------- | ----- | ----------------------- |
| \_collateralIndex | uint8 | index of the collateral |

## \_msgSender

```solidity theme={null}
function _msgSender() internal view returns (address)
```

*Returns the caller of the transaction (overriden by trader address if delegatedAction is called)*
