Gains Network
  • 📚Home
  • Gains Network
    • 🔎Overview
    • 🍏GNS Token
    • 📔Contract Addresses
      • Apechain Mainnet
      • Arbitrum Mainnet
      • Base Mainnet
      • Polygon Mainnet
  • gTrade (Leveraged Trading)
    • 🔎Overview
    • 🎛️Setting up to trade
      • 🟣Polygon Network Setup
      • 🔵Arbitrum Network Setup
    • 🏦Opening / Closing trades
    • 💲Trade Collaterals
      • Spotlight: WETH
    • 📖Asset classes
      • 🪙Cryptocurrencies
      • 💵Forex
      • 🛢️Commodities
    • 🎫Fees & Spread
    • 📰Pair List
    • 🧒How to use Practice Mode
    • ⚡One-Click Trading (1CT)
    • 🌌gTrade Solana
    • 📺Video tutorials
  • Loyalty Programs
    • Prime Trader
    • gTrade Credits
  • Community
    • gTrade Guardians
    • Governance forum
  • Reward Pools
    • 💚GNS Staking
    • 👛gToken Vaults
      • 👷Staker Functions
      • ❔Staker FAQ
      • ⏳Upgrades, updates, and timelocks
    • 🔼Active Pools & Incentives
  • Help
    • ❓FAQ
    • ⏳Chain congestion
  • Bridges
    • 🪐Arbitrum <-> Polygon Bridge
    • 🪐ERC20 Bridge (Polygon)
    • 🪐NFT Bridge (Polygon)
  • Developer
    • Integrators
      • Trading Contracts
      • Backend
      • Price Feed
      • Guides
        • v9 migration
        • Calculating borrowing fees
        • Calculating liquidation price
    • Technical Reference
      • Contracts
        • Changelogs
          • v9.2.1 Update
          • v9.1 Update
          • v9.1.1 Update
          • v9 Update
        • Core
          • Abstract
            • GNSAddressStore
            • GNSDiamondCut
            • GNSDiamondLoupe
            • GNSDiamondStorage
          • Facets
            • GNSBorrowingFees
            • GNSOtc
            • GNSPriceAggregator
            • GNSTradingInteractions
            • GNSTriggerRewards
            • GNSFeeTiers
            • GNSPairsStorage
            • GNSPriceImpact
            • GNSReferrals
            • GNSTradingCallbacks
            • GNSTradingStorage
          • GNSMultiCollatDiamond
          • GNSStaking
          • GToken
          • GTokenOpenPnlFeed
        • Interfaces
          • Libraries
            • IBorrowingFeesUtils
            • IOtcUtils
            • IPriceAggregatorUtils
            • ITriggerRewardsUtils
            • IFeeTiersUtils
            • IPairsStorageUtils
            • IPriceImpactUtils
            • IReferralsUtils
            • ITradingCallbacksUtils
            • ITradingCommonUtils
            • ITradingInteractionsUtils
            • ITradingStorageUtils
            • IUpdateLeverageUtils
            • IUpdatePositionSizeUtils
          • Types
            • IAddressStore
            • IBorrowingFees
            • IDiamondStorage
            • IOtc
            • IPriceAggregator
            • ITradingStorage
            • ITradingInteractions
            • ITriggerRewards
            • ITypes
            • IUpdateLeverage
            • IPairsStorage
            • IFeeTiers
            • IPriceImpact
            • IReferrals
            • ITradingCallbacks
            • IUpdatePositionSize
          • IChainlinkFeed
          • IArbSys
          • IChainlinkOracle
          • IERC20
          • IERC721
          • IERC721Design
          • IGeneralErrors
          • IGNSAddressStore
          • IGNSDiamond
          • IGNSDiamondCut
          • IGNSMultiCollatDiamond
          • IGNSDiamondLoupe
          • IGNSStaking
          • IGToken
          • IGTokenLockedDepositNft
          • IGTokenOpenPnlFeed
          • IGTokenLockedDepositNftDesign
          • ILiquidityPool
          • IRateProvider
          • IOwnable
        • Libraries
          • updateLeverage
            • UpdateLeverageLifecycles
          • updatePositionSize
            • IncreasePositionSizeUtils
            • DecreasePositionSizeUtils
            • UpdatePositionSizeLifecycles
          • AddressStoreUtils
          • ChainlinkClientUtils
          • ChainUtils
          • ConstantsUtils
          • CollateralUtils
          • DiamondUtils
          • LiquidityPoolUtils
          • OtcUtils
          • PackingUtils
          • StorageUtils
          • TriggerRewardsUtils
          • ArrayGetters
          • BorrowingFeesUtils
          • FeeTiersUtils
          • PriceAggregatorUtils
          • PriceImpactUtils
          • ReferralsUtils
          • TradingCallbacksUtils
          • PairsStorageUtils
          • TradingCommonUtils
          • TradingStorageUtils
          • TradingInteractionsUtils
        • Misc
          • EpochBasedTokenClaim
          • GNSCompensationHandler
          • GTokenRateProvider
          • GTokenLockedDepositNftDesign
          • ManagerTimelock
          • OwnerTimelock
          • VotingDelegator
        • Tokens
          • GainsNetworkToken
          • GTokenLockedDepositNft
      • Backend
        • Backend Types
      • SDK
        • Client Types
  • Archived
    • 🖼️gFARM2 NFTs
Powered by GitBook
On this page
  • validWindowsDuration
  • initializePriceImpact
  • initializeNegPnlCumulVolMultiplier
  • initializePairFactors
  • setPriceImpactWindowsCount
  • setPriceImpactWindowsDuration
  • setNegPnlCumulVolMultiplier
  • setProtectionCloseFactorWhitelist
  • setPairDepths
  • setProtectionCloseFactors
  • setProtectionCloseFactorBlocks
  • setCumulativeFactors
  • setExemptOnOpen
  • setExemptAfterProtectionCloseFactor
  • addPriceImpactOpenInterest
  • getPriceImpactOi
  • getTradePriceImpact
  • getPairDepth
  • getOiWindowsSettings
  • getOiWindow
  • getOiWindows
  • getPairDepths
  • getPairFactors
  • getNegPnlCumulVolMultiplier
  • getProtectionCloseFactorWhitelist
  • _getSlot
  • _getStorage
  • _getMultiCollatDiamond
  • _transferPriceImpactOiForPairs
  • Parameters
  • _transferPriceImpactOiForPair
  • Parameters
  • _getWindowId
  • Parameters
  • _getCurrentWindowId
  • Parameters
  • _getEarliestActiveWindowId
  • Parameters
  • _isWindowPotentiallyActive
  • Parameters
  • _getTradePriceImpact
  • Parameters

Was this helpful?

  1. Developer
  2. Technical Reference
  3. Contracts
  4. Libraries

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

modifier validWindowsDuration(uint48 _windowsDuration)

Validates new windowsDuration value

initializePriceImpact

function initializePriceImpact(uint48 _windowsDuration, uint48 _windowsCount) internal

Check IPriceImpactUtils interface for documentation

initializeNegPnlCumulVolMultiplier

function initializeNegPnlCumulVolMultiplier(uint40 _negPnlCumulVolMultiplier) internal

Check IPriceImpactUtils interface for documentation

initializePairFactors

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

Check IPriceImpactUtils interface for documentation

setPriceImpactWindowsCount

function setPriceImpactWindowsCount(uint48 _newWindowsCount) internal

Check IPriceImpactUtils interface for documentation

setPriceImpactWindowsDuration

function setPriceImpactWindowsDuration(uint48 _newWindowsDuration, uint256 _pairsCount) internal

Check IPriceImpactUtils interface for documentation

setNegPnlCumulVolMultiplier

function setNegPnlCumulVolMultiplier(uint40 _negPnlCumulVolMultiplier) internal

Check IPriceImpactUtils interface for documentation

setProtectionCloseFactorWhitelist

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

Check IPriceImpactUtils interface for documentation

setPairDepths

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

Check IPriceImpactUtils interface for documentation

setProtectionCloseFactors

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

Check IPriceImpactUtils interface for documentation

setProtectionCloseFactorBlocks

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

Check IPriceImpactUtils interface for documentation

setCumulativeFactors

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

Check IPriceImpactUtils interface for documentation

setExemptOnOpen

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

Check IPriceImpactUtils interface for documentation

setExemptAfterProtectionCloseFactor

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

Check IPriceImpactUtils interface for documentation

addPriceImpactOpenInterest

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

Check IPriceImpactUtils interface for documentation

getPriceImpactOi

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

Check IPriceImpactUtils interface for documentation

getTradePriceImpact

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

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

Check IPriceImpactUtils interface for documentation

getOiWindowsSettings

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

Check IPriceImpactUtils interface for documentation

getOiWindow

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

Check IPriceImpactUtils interface for documentation

getOiWindows

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

Check IPriceImpactUtils interface for documentation

getPairDepths

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

Check IPriceImpactUtils interface for documentation

getPairFactors

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

Check IPriceImpactUtils interface for documentation

getNegPnlCumulVolMultiplier

function getNegPnlCumulVolMultiplier() internal view returns (uint40)

Check IPriceImpactUtils interface for documentation

getProtectionCloseFactorWhitelist

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

Check IPriceImpactUtils interface for documentation

_getSlot

function _getSlot() internal pure returns (uint256)

Returns storage slot to use when fetching storage relevant to library

_getStorage

function _getStorage() internal pure returns (struct IPriceImpact.PriceImpactStorage s)

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

_getMultiCollatDiamond

function _getMultiCollatDiamond() internal view returns (contract IGNSMultiCollatDiamond)

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

_transferPriceImpactOiForPairs

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

current OI windows settings

_newWindowsDuration

uint48

new windows duration

_transferPriceImpactOiForPair

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

new pair OI window (new windowsDuration mapping)

_getWindowId

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

OI windows settings

_getCurrentWindowId

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

Returns window id at current timestamp given _settings.

Parameters

Name
Type
Description

_settings

OI windows settings

_getEarliestActiveWindowId

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

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

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

PreviousPriceAggregatorUtilsNextReferralsUtils

Last updated 6 months ago

Was this helpful?

IPriceImpact.OiWindowsSettings
IPriceImpact.PairOi
IPriceImpact.OiWindowsSettings
IPriceImpact.OiWindowsSettings