# TradingInteractionsUtils

*GNSTradingInteractions facet internal library*

## tradingActivated

```solidity
modifier tradingActivated()
```

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

## tradingActivatedOrCloseOnly

```solidity
modifier tradingActivatedOrCloseOnly()
```

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

## notDelegatedAction

```solidity
modifier notDelegatedAction()
```

*Modifier to prevent calling function from delegated action*

## initializeTrading

```solidity
function initializeTrading(uint16 _marketOrdersTimeoutBlocks, address[] _usersByPassTriggerLink) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateMarketOrdersTimeoutBlocks

```solidity
function updateMarketOrdersTimeoutBlocks(uint16 _valueBlocks) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateByPassTriggerLink

```solidity
function updateByPassTriggerLink(address[] _users, bool[] _shouldByPass) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## setTradingDelegate

```solidity
function setTradingDelegate(address _delegate) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## removeTradingDelegate

```solidity
function removeTradingDelegate() internal
```

*Check ITradingInteractionsUtils interface for documentation*

## delegatedTradingAction

```solidity
function delegatedTradingAction(address _trader, bytes _callData) internal returns (bytes)
```

*Check ITradingInteractionsUtils interface for documentation*

## openTrade

```solidity
function openTrade(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## openTradeNative

```solidity
function openTradeNative(struct ITradingStorage.Trade _trade, uint16 _maxSlippageP, address _referrer) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateMaxClosingSlippageP

```solidity
function updateMaxClosingSlippageP(uint32 _index, uint16 _maxClosingSlippageP) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## closeTradeMarket

```solidity
function closeTradeMarket(uint32 _index, uint64 _expectedPrice) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateOpenOrder

```solidity
function updateOpenOrder(uint32 _index, uint64 _openPrice, uint64 _tp, uint64 _sl, uint16 _maxSlippageP) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## cancelOpenOrder

```solidity
function cancelOpenOrder(uint32 _index) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateTp

```solidity
function updateTp(uint32 _index, uint64 _newTp) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateSl

```solidity
function updateSl(uint32 _index, uint64 _newSl) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## updateLeverage

```solidity
function updateLeverage(uint32 _index, uint24 _newLeverage) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## increasePositionSize

```solidity
function increasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice, uint16 _maxSlippageP) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## decreasePositionSize

```solidity
function decreasePositionSize(uint32 _index, uint120 _collateralDelta, uint24 _leverageDelta, uint64 _expectedPrice) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## triggerOrder

```solidity
function triggerOrder(uint256 _packed) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## cancelOrderAfterTimeout

```solidity
function cancelOrderAfterTimeout(uint32 _orderIndex) internal
```

*Check ITradingInteractionsUtils interface for documentation*

## getWrappedNativeToken

```solidity
function getWrappedNativeToken() internal view returns (address)
```

*Check ITradingInteractionsUtils interface for documentation*

## isWrappedNativeToken

```solidity
function isWrappedNativeToken(address _token) internal view returns (bool)
```

*Check ITradingInteractionsUtils interface for documentation*

## getTradingDelegate

```solidity
function getTradingDelegate(address _trader) internal view returns (address)
```

*Check ITradingInteractionsUtils interface for documentation*

## getMarketOrdersTimeoutBlocks

```solidity
function getMarketOrdersTimeoutBlocks() internal view returns (uint16)
```

*Check ITradingInteractionsUtils interface for documentation*

## getByPassTriggerLink

```solidity
function getByPassTriggerLink(address _user) internal view returns (bool)
```

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

## \_openTrade

```solidity
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.md#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
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.md#id) | trade id    |
| \_orderType | enum ITradingStorage.PendingOrderType                                                                 | order type  |

## \_getPriceTriggerOrder

```solidity
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.md#trade) | trade                                                     |
| \_orderId                | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#id)       | order id                                                  |
| \_orderType              | enum ITradingStorage.PendingOrderType                                                                       | order type                                                |
| \_positionSizeCollateral | uint256                                                                                                     | position size in collateral tokens (collateral precision) |

## \_wrapNativeToken

```solidity
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
function _msgSender() internal view returns (address)
```

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


---

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