# PriceImpactUtils

\_GNSPriceImpact facet internal library

This is a library to help manage a price impact decay algorithm .

When a trade is placed, OI is added to the window corresponding to time of open. When a trade is removed, OI is removed from the window corresponding to time of open.

When calculating price impact, only the most recent X windows are taken into account.\_

## validWindowsDuration

```solidity
modifier validWindowsDuration(uint48 _windowsDuration)
```

*Validates new windowsDuration value*

## initializePriceImpact

```solidity
function initializePriceImpact(uint48 _windowsDuration, uint48 _windowsCount) internal
```

*Check IPriceImpactUtils interface for documentation*

## initializeNegPnlCumulVolMultiplier

```solidity
function initializeNegPnlCumulVolMultiplier(uint40 _negPnlCumulVolMultiplier) internal
```

*Check IPriceImpactUtils interface for documentation*

## initializePairFactors

```solidity
function initializePairFactors(uint16[] _pairIndices, uint40[] _protectionCloseFactors, uint32[] _protectionCloseFactorBlocks, uint40[] _cumulativeFactors) internal
```

*Check IPriceImpactUtils interface for documentation*

## setPriceImpactWindowsCount

```solidity
function setPriceImpactWindowsCount(uint48 _newWindowsCount) internal
```

*Check IPriceImpactUtils interface for documentation*

## setPriceImpactWindowsDuration

```solidity
function setPriceImpactWindowsDuration(uint48 _newWindowsDuration, uint256 _pairsCount) internal
```

*Check IPriceImpactUtils interface for documentation*

## setNegPnlCumulVolMultiplier

```solidity
function setNegPnlCumulVolMultiplier(uint40 _negPnlCumulVolMultiplier) internal
```

*Check IPriceImpactUtils interface for documentation*

## setProtectionCloseFactorWhitelist

```solidity
function setProtectionCloseFactorWhitelist(address[] _traders, bool[] _whitelisted) internal
```

*Check IPriceImpactUtils interface for documentation*

## setPairDepths

```solidity
function setPairDepths(uint256[] _indices, uint128[] _depthsAboveUsd, uint128[] _depthsBelowUsd) internal
```

*Check IPriceImpactUtils interface for documentation*

## setProtectionCloseFactors

```solidity
function setProtectionCloseFactors(uint16[] _pairIndices, uint40[] _protectionCloseFactors) internal
```

*Check IPriceImpactUtils interface for documentation*

## setProtectionCloseFactorBlocks

```solidity
function setProtectionCloseFactorBlocks(uint16[] _pairIndices, uint32[] _protectionCloseFactorBlocks) internal
```

*Check IPriceImpactUtils interface for documentation*

## setCumulativeFactors

```solidity
function setCumulativeFactors(uint16[] _pairIndices, uint40[] _cumulativeFactors) internal
```

*Check IPriceImpactUtils interface for documentation*

## setExemptOnOpen

```solidity
function setExemptOnOpen(uint16[] _pairIndices, bool[] _exemptOnOpen) internal
```

*Check IPriceImpactUtils interface for documentation*

## setExemptAfterProtectionCloseFactor

```solidity
function setExemptAfterProtectionCloseFactor(uint16[] _pairIndices, bool[] _exemptAfterProtectionCloseFactor) internal
```

*Check IPriceImpactUtils interface for documentation*

## addPriceImpactOpenInterest

```solidity
function addPriceImpactOpenInterest(address _trader, uint32 _index, uint256 _oiDeltaCollateral, bool _open, bool _isPnlPositive) internal
```

*Check IPriceImpactUtils interface for documentation*

## getPriceImpactOi

```solidity
function getPriceImpactOi(uint256 _pairIndex, bool _long) internal view returns (uint256 activeOi)
```

*Check IPriceImpactUtils interface for documentation*

## getTradePriceImpact

```solidity
function getTradePriceImpact(address _trader, uint256 _marketPrice, uint256 _pairIndex, bool _long, uint256 _tradeOpenInterestUsd, bool _isPnlPositive, bool _open, uint256 _lastPosIncreaseBlock, enum ITradingStorage.ContractsVersion _contractsVersion) internal view returns (uint256 priceImpactP, uint256 priceAfterImpact)
```

*Check IPriceImpactUtils interface for documentation*

## getPairDepth

```solidity
function getPairDepth(uint256 _pairIndex) internal view returns (struct IPriceImpact.PairDepth)
```

*Check IPriceImpactUtils interface for documentation*

## getOiWindowsSettings

```solidity
function getOiWindowsSettings() internal view returns (struct IPriceImpact.OiWindowsSettings)
```

*Check IPriceImpactUtils interface for documentation*

## getOiWindow

```solidity
function getOiWindow(uint48 _windowsDuration, uint256 _pairIndex, uint256 _windowId) internal view returns (struct IPriceImpact.PairOi)
```

*Check IPriceImpactUtils interface for documentation*

## getOiWindows

```solidity
function getOiWindows(uint48 _windowsDuration, uint256 _pairIndex, uint256[] _windowIds) internal view returns (struct IPriceImpact.PairOi[])
```

*Check IPriceImpactUtils interface for documentation*

## getPairDepths

```solidity
function getPairDepths(uint256[] _indices) internal view returns (struct IPriceImpact.PairDepth[])
```

*Check IPriceImpactUtils interface for documentation*

## getPairFactors

```solidity
function getPairFactors(uint256[] _indices) internal view returns (struct IPriceImpact.PairFactors[] pairFactors)
```

*Check IPriceImpactUtils interface for documentation*

## getNegPnlCumulVolMultiplier

```solidity
function getNegPnlCumulVolMultiplier() internal view returns (uint40)
```

*Check IPriceImpactUtils interface for documentation*

## getProtectionCloseFactorWhitelist

```solidity
function getProtectionCloseFactorWhitelist(address _trader) internal view returns (bool)
```

*Check IPriceImpactUtils 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 IPriceImpact.PriceImpactStorage 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.*

## \_transferPriceImpactOiForPairs

```solidity
function _transferPriceImpactOiForPairs(uint256 _pairsCount, mapping(uint256 => mapping(uint256 => struct IPriceImpact.PairOi)) _prevPairOiWindows, mapping(uint256 => mapping(uint256 => struct IPriceImpact.PairOi)) _newPairOiWindows, struct IPriceImpact.OiWindowsSettings _settings, uint48 _newWindowsDuration) internal
```

\_Transfers total long / short OI from last '\_settings.windowsCount' windows of `_prevPairOiWindows` to current window of `_newPairOiWindows` for `_pairsCount` pairs.

Emits a {PriceImpactOiTransferredPairs} event.\_

### Parameters

| Name                 | Type                                                                                                                          | Description                                                 |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| \_pairsCount         | uint256                                                                                                                       | number of pairs                                             |
| \_prevPairOiWindows  | mapping(uint256 => mapping(uint256 => struct IPriceImpact.PairOi))                                                            | previous pair OI windows (previous windowsDuration mapping) |
| \_newPairOiWindows   | mapping(uint256 => mapping(uint256 => struct IPriceImpact.PairOi))                                                            | new pair OI windows (new windowsDuration mapping)           |
| \_settings           | [IPriceImpact.OiWindowsSettings](/developer/technical-reference/contracts/interfaces/types/ipriceimpact.md#oiwindowssettings) | current OI windows settings                                 |
| \_newWindowsDuration | uint48                                                                                                                        | new windows duration                                        |

## \_transferPriceImpactOiForPair

```solidity
function _transferPriceImpactOiForPair(uint256 _pairIndex, uint256 _prevCurrentWindowId, uint256 _prevEarliestWindowId, mapping(uint256 => struct IPriceImpact.PairOi) _prevPairOiWindows, struct IPriceImpact.PairOi _newPairOiWindow) internal
```

\_Transfers total long / short OI from `prevEarliestWindowId` to `prevCurrentWindowId` windows of `_prevPairOiWindows` to `_newPairOiWindow` window.

Emits a {PriceImpactOiTransferredPair} event.\_

### Parameters

| Name                   | Type                                                                                                    | Description                                                 |
| ---------------------- | ------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| \_pairIndex            | uint256                                                                                                 | index of the pair                                           |
| \_prevCurrentWindowId  | uint256                                                                                                 | previous current window ID                                  |
| \_prevEarliestWindowId | uint256                                                                                                 | previous earliest active window ID                          |
| \_prevPairOiWindows    | mapping(uint256 => struct IPriceImpact.PairOi)                                                          | previous pair OI windows (previous windowsDuration mapping) |
| \_newPairOiWindow      | [IPriceImpact.PairOi](/developer/technical-reference/contracts/interfaces/types/ipriceimpact.md#pairoi) | new pair OI window (new windowsDuration mapping)            |

## \_getWindowId

```solidity
function _getWindowId(uint48 _timestamp, struct IPriceImpact.OiWindowsSettings _settings) internal pure returns (uint256)
```

*Returns window id at `_timestamp` given `_settings`.*

### Parameters

| Name        | Type                                                                                                                          | Description         |
| ----------- | ----------------------------------------------------------------------------------------------------------------------------- | ------------------- |
| \_timestamp | uint48                                                                                                                        | timestamp           |
| \_settings  | [IPriceImpact.OiWindowsSettings](/developer/technical-reference/contracts/interfaces/types/ipriceimpact.md#oiwindowssettings) | OI windows settings |

## \_getCurrentWindowId

```solidity
function _getCurrentWindowId(struct IPriceImpact.OiWindowsSettings _settings) internal view returns (uint256)
```

*Returns window id at current timestamp given `_settings`.*

### Parameters

| Name       | Type                                                                                                                          | Description         |
| ---------- | ----------------------------------------------------------------------------------------------------------------------------- | ------------------- |
| \_settings | [IPriceImpact.OiWindowsSettings](/developer/technical-reference/contracts/interfaces/types/ipriceimpact.md#oiwindowssettings) | OI windows settings |

## \_getEarliestActiveWindowId

```solidity
function _getEarliestActiveWindowId(uint256 _currentWindowId, uint48 _windowsCount) internal pure returns (uint256)
```

*Returns earliest active window id given `_currentWindowId` and `_windowsCount`.*

### Parameters

| Name              | Type    | Description          |
| ----------------- | ------- | -------------------- |
| \_currentWindowId | uint256 | current window id    |
| \_windowsCount    | uint48  | active windows count |

## \_isWindowPotentiallyActive

```solidity
function _isWindowPotentiallyActive(uint256 _windowId, uint256 _currentWindowId) internal pure returns (bool)
```

\_Returns whether '*windowId' can be potentially active id given `_currentWindowId`*

### Parameters

| Name              | Type    | Description       |
| ----------------- | ------- | ----------------- |
| \_windowId        | uint256 | window id         |
| \_currentWindowId | uint256 | current window id |

## \_getTradePriceImpact

```solidity
function _getTradePriceImpact(uint256 _marketPrice, bool _long, uint256 _startOpenInterestUsd, uint256 _tradeOpenInterestUsd, uint256 _onePercentDepthUsd, bool _open, uint256 _protectionCloseFactor, uint256 _cumulativeFactor, enum ITradingStorage.ContractsVersion _contractsVersion) internal pure returns (uint256 priceImpactP, uint256 priceAfterImpact)
```

*Returns trade price impact % and opening price after impact.*

### Parameters

| Name                    | Type                                  | Description                                                          |
| ----------------------- | ------------------------------------- | -------------------------------------------------------------------- |
| \_marketPrice           | uint256                               | market price (1e10 precision)                                        |
| \_long                  | bool                                  | true for long, false for short                                       |
| \_startOpenInterestUsd  | uint256                               | existing open interest of pair on trade side in USD (1e18 precision) |
| \_tradeOpenInterestUsd  | uint256                               | open interest of trade in USD (1e18 precision)                       |
| \_onePercentDepthUsd    | uint256                               | one percent depth of pair in USD on trade side                       |
| \_open                  | bool                                  | true for open, false for close                                       |
| \_protectionCloseFactor | uint256                               | protection close factor (1e10 precision)                             |
| \_cumulativeFactor      | uint256                               | cumulative factor (1e10 precision)                                   |
| \_contractsVersion      | enum ITradingStorage.ContractsVersion | trade contracts version                                              |


---

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