# IPriceAggregatorUtils

*Interface for GNSPriceAggregator facet (inherits types and also contains functions, events, and custom errors)*

## initializePriceAggregator

```solidity
function initializePriceAggregator(address _linkToken, contract IChainlinkFeed _linkUsdPriceFeed, uint24 _twapInterval, uint8 _minAnswers, address[] _oracles, bytes32[2] _jobIds, uint8[] _collateralIndices, struct IPriceAggregator.LiquidityPoolInput[] _gnsCollateralLiquidityPools, contract IChainlinkFeed[] _collateralUsdPriceFeeds) external
```

*Initializes price aggregator facet*

### Parameters

| Name                          | Type                                                                                                                                        | Description                                            |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ |
| \_linkToken                   | address                                                                                                                                     | LINK token address                                     |
| \_linkUsdPriceFeed            | contract IChainlinkFeed                                                                                                                     | LINK/USD price feed address                            |
| \_twapInterval                | uint24                                                                                                                                      | TWAP interval (seconds)                                |
| \_minAnswers                  | uint8                                                                                                                                       | answers count at which a trade is executed with median |
| \_oracles                     | address\[]                                                                                                                                  | chainlink oracle addresses                             |
| \_jobIds                      | bytes32\[2]                                                                                                                                 | chainlink job ids (market/lookback)                    |
| \_collateralIndices           | uint8\[]                                                                                                                                    | collateral indices                                     |
| \_gnsCollateralLiquidityPools | [IPriceAggregator.LiquidityPoolInput\[\]](/developer/technical-reference/contracts/interfaces/types/ipriceaggregator.md#liquiditypoolinput) | corresponding GNS/collateral liquidity pool values     |
| \_collateralUsdPriceFeeds     | contract IChainlinkFeed\[]                                                                                                                  | corresponding collateral/USD chainlink price feeds     |

## updateLinkUsdPriceFeed

```solidity
function updateLinkUsdPriceFeed(contract IChainlinkFeed _value) external
```

*Updates LINK/USD chainlink price feed*

### Parameters

| Name    | Type                    | Description |
| ------- | ----------------------- | ----------- |
| \_value | contract IChainlinkFeed | new value   |

## updateCollateralUsdPriceFeed

```solidity
function updateCollateralUsdPriceFeed(uint8 _collateralIndex, contract IChainlinkFeed _value) external
```

*Updates collateral/USD chainlink price feed*

### Parameters

| Name              | Type                    | Description      |
| ----------------- | ----------------------- | ---------------- |
| \_collateralIndex | uint8                   | collateral index |
| \_value           | contract IChainlinkFeed | new value        |

## updateCollateralGnsLiquidityPool

```solidity
function updateCollateralGnsLiquidityPool(uint8 _collateralIndex, struct IPriceAggregator.LiquidityPoolInput _liquidityPoolInput) external
```

*Updates collateral/GNS liquidity pool*

### Parameters

| Name                 | Type                                                                                                                                    | Description      |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | ---------------- |
| \_collateralIndex    | uint8                                                                                                                                   | collateral index |
| \_liquidityPoolInput | [IPriceAggregator.LiquidityPoolInput](/developer/technical-reference/contracts/interfaces/types/ipriceaggregator.md#liquiditypoolinput) | new values       |

## updateTwapInterval

```solidity
function updateTwapInterval(uint24 _twapInterval) external
```

*Updates TWAP interval*

### Parameters

| Name           | Type   | Description         |
| -------------- | ------ | ------------------- |
| \_twapInterval | uint24 | new value (seconds) |

## updateMinAnswers

```solidity
function updateMinAnswers(uint8 _value) external
```

*Updates minimum answers count*

### Parameters

| Name    | Type  | Description |
| ------- | ----- | ----------- |
| \_value | uint8 | new value   |

## addOracle

```solidity
function addOracle(address _a) external
```

*Adds an oracle*

### Parameters

| Name | Type    | Description |
| ---- | ------- | ----------- |
| \_a  | address | new value   |

## replaceOracle

```solidity
function replaceOracle(uint256 _index, address _a) external
```

*Replaces an oracle*

### Parameters

| Name    | Type    | Description  |
| ------- | ------- | ------------ |
| \_index | uint256 | oracle index |
| \_a     | address | new value    |

## removeOracle

```solidity
function removeOracle(uint256 _index) external
```

*Removes an oracle*

### Parameters

| Name    | Type    | Description  |
| ------- | ------- | ------------ |
| \_index | uint256 | oracle index |

## setMarketJobId

```solidity
function setMarketJobId(bytes32 _jobId) external
```

*Updates market job id*

### Parameters

| Name    | Type    | Description |
| ------- | ------- | ----------- |
| \_jobId | bytes32 | new value   |

## setLimitJobId

```solidity
function setLimitJobId(bytes32 _jobId) external
```

*Updates lookback job id*

### Parameters

| Name    | Type    | Description |
| ------- | ------- | ----------- |
| \_jobId | bytes32 | new value   |

## getPrice

```solidity
function getPrice(uint8 _collateralIndex, uint16 _pairIndex, struct ITradingStorage.Id _tradeId, struct ITradingStorage.Id _orderId, enum ITradingStorage.PendingOrderType _orderType, uint256 _positionSizeCollateral, uint256 _fromBlock) external
```

*Requests price from oracles*

### Parameters

| Name                     | Type                                                                                                  | Description                                                      |
| ------------------------ | ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
| \_collateralIndex        | uint8                                                                                                 | collateral index                                                 |
| \_pairIndex              | uint16                                                                                                | pair index                                                       |
| \_tradeId                | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#id) | trade id                                                         |
| \_orderId                | [ITradingStorage.Id](/developer/technical-reference/contracts/interfaces/types/itradingstorage.md#id) | order id                                                         |
| \_orderType              | enum ITradingStorage.PendingOrderType                                                                 | order type                                                       |
| \_positionSizeCollateral | uint256                                                                                               | position size (collateral precision)                             |
| \_fromBlock              | uint256                                                                                               | block number from which to start fetching prices (for lookbacks) |

## fulfill

```solidity
function fulfill(bytes32 _requestId, uint256 _priceData) external
```

*Fulfills price request, called by chainlink oracles*

### Parameters

| Name        | Type    | Description |
| ----------- | ------- | ----------- |
| \_requestId | bytes32 | request id  |
| \_priceData | uint256 | price data  |

## claimBackLink

```solidity
function claimBackLink() external
```

*Claims back LINK tokens, called by gov fund*

## getLinkFee

```solidity
function getLinkFee(uint8 _collateralIndex, address _trader, uint16 _pairIndex, uint256 _positionSizeCollateral) external view returns (uint256)
```

*Returns LINK fee for price request*

### Parameters

| Name                     | Type    | Description                                               |
| ------------------------ | ------- | --------------------------------------------------------- |
| \_collateralIndex        | uint8   | collateral index                                          |
| \_trader                 | address | trader address                                            |
| \_pairIndex              | uint16  | pair index                                                |
| \_positionSizeCollateral | uint256 | position size in collateral tokens (collateral precision) |

## getCollateralPriceUsd

```solidity
function getCollateralPriceUsd(uint8 _collateralIndex) external view returns (uint256)
```

*Returns collateral/USD price*

### Parameters

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

## getUsdNormalizedValue

```solidity
function getUsdNormalizedValue(uint8 _collateralIndex, uint256 _collateralValue) external view returns (uint256)
```

*Returns USD normalized value from collateral value*

### Parameters

| Name              | Type    | Description                             |
| ----------------- | ------- | --------------------------------------- |
| \_collateralIndex | uint8   | index of collateral                     |
| \_collateralValue | uint256 | collateral value (collateral precision) |

## getCollateralFromUsdNormalizedValue

```solidity
function getCollateralFromUsdNormalizedValue(uint8 _collateralIndex, uint256 _normalizedValue) external view returns (uint256)
```

*Returns collateral value (collateral precision) from USD normalized value*

### Parameters

| Name              | Type    | Description                 |
| ----------------- | ------- | --------------------------- |
| \_collateralIndex | uint8   | index of collateral         |
| \_normalizedValue | uint256 | normalized value (1e18 USD) |

## getGnsPriceUsd

```solidity
function getGnsPriceUsd(uint8 _collateralIndex) external view returns (uint256)
```

*Returns GNS/USD price based on GNS/collateral price*

### Parameters

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

## getGnsPriceUsd

```solidity
function getGnsPriceUsd(uint8 _collateralIndex, uint256 _gnsPriceCollateral) external view returns (uint256)
```

*Returns GNS/USD price based on GNS/collateral price*

### Parameters

| Name                 | Type    | Description                 |
| -------------------- | ------- | --------------------------- |
| \_collateralIndex    | uint8   | index of collateral         |
| \_gnsPriceCollateral | uint256 | GNS/collateral price (1e10) |

## getGnsPriceCollateralIndex

```solidity
function getGnsPriceCollateralIndex(uint8 _collateralIndex) external view returns (uint256)
```

*Returns GNS/collateral price*

### Parameters

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

## getGnsPriceCollateralAddress

```solidity
function getGnsPriceCollateralAddress(address _collateral) external view returns (uint256)
```

*Returns GNS/collateral price*

### Parameters

| Name         | Type    | Description               |
| ------------ | ------- | ------------------------- |
| \_collateral | address | address of the collateral |

## getLinkUsdPriceFeed

```solidity
function getLinkUsdPriceFeed() external view returns (contract IChainlinkFeed)
```

*Returns the link/usd price feed address*

## getTwapInterval

```solidity
function getTwapInterval() external view returns (uint24)
```

*Returns the twap interval in seconds*

## getMinAnswers

```solidity
function getMinAnswers() external view returns (uint8)
```

*Returns the minimum answers to execute an order and take the median*

## getMarketJobId

```solidity
function getMarketJobId() external view returns (bytes32)
```

*Returns the market job id*

## getLimitJobId

```solidity
function getLimitJobId() external view returns (bytes32)
```

*Returns the limit job id*

## getOracle

```solidity
function getOracle(uint256 _index) external view returns (address)
```

*Returns a specific oracle*

### Parameters

| Name    | Type    | Description         |
| ------- | ------- | ------------------- |
| \_index | uint256 | index of the oracle |

## getOracles

```solidity
function getOracles() external view returns (address[])
```

*Returns all oracles*

## getCollateralGnsLiquidityPool

```solidity
function getCollateralGnsLiquidityPool(uint8 _collateralIndex) external view returns (struct IPriceAggregator.LiquidityPoolInfo)
```

*Returns collateral/gns liquidity pool info*

### Parameters

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

## getCollateralUsdPriceFeed

```solidity
function getCollateralUsdPriceFeed(uint8 _collateralIndex) external view returns (contract IChainlinkFeed)
```

*Returns collateral/usd chainlink price feed*

### Parameters

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

## getPriceAggregatorOrder

```solidity
function getPriceAggregatorOrder(bytes32 _requestId) external view returns (struct IPriceAggregator.Order)
```

*Returns order data*

### Parameters

| Name        | Type    | Description         |
| ----------- | ------- | ------------------- |
| \_requestId | bytes32 | index of collateral |

## getPriceAggregatorOrderAnswers

```solidity
function getPriceAggregatorOrderAnswers(struct ITradingStorage.Id _orderId) external view returns (struct IPriceAggregator.OrderAnswer[])
```

*Returns order data*

### Parameters

| Name      | Type               | Description |
| --------- | ------------------ | ----------- |
| \_orderId | ITradingStorage.Id | order id    |

## getChainlinkToken

```solidity
function getChainlinkToken() external view returns (address)
```

*Returns chainlink token address*

## getRequestCount

```solidity
function getRequestCount() external view returns (uint256)
```

*Returns requestCount (used by ChainlinkClientUtils)*

## getPendingRequest

```solidity
function getPendingRequest(bytes32 _id) external view returns (address)
```

*Returns pendingRequests mapping entry (used by ChainlinkClientUtils)*

## LinkUsdPriceFeedUpdated

```solidity
event LinkUsdPriceFeedUpdated(address value)
```

*Emitted when LINK/USD price feed is updated*

### Parameters

| Name  | Type    | Description |
| ----- | ------- | ----------- |
| value | address | new value   |

## CollateralUsdPriceFeedUpdated

```solidity
event CollateralUsdPriceFeedUpdated(uint8 collateralIndex, address value)
```

*Emitted when collateral/USD price feed is updated*

### Parameters

| Name            | Type    | Description      |
| --------------- | ------- | ---------------- |
| collateralIndex | uint8   | collateral index |
| value           | address | new value        |

## CollateralGnsLiquidityPoolUpdated

```solidity
event CollateralGnsLiquidityPoolUpdated(uint8 collateralIndex, struct IPriceAggregator.LiquidityPoolInfo newValue)
```

*Emitted when collateral/GNS Uniswap V3 pool is updated*

### Parameters

| Name            | Type                                      | Description      |
| --------------- | ----------------------------------------- | ---------------- |
| collateralIndex | uint8                                     | collateral index |
| newValue        | struct IPriceAggregator.LiquidityPoolInfo | new value        |

## TwapIntervalUpdated

```solidity
event TwapIntervalUpdated(uint32 newValue)
```

*Emitted when TWAP interval is updated*

### Parameters

| Name     | Type   | Description |
| -------- | ------ | ----------- |
| newValue | uint32 | new value   |

## MinAnswersUpdated

```solidity
event MinAnswersUpdated(uint8 value)
```

*Emitted when minimum answers count is updated*

### Parameters

| Name  | Type  | Description |
| ----- | ----- | ----------- |
| value | uint8 | new value   |

## OracleAdded

```solidity
event OracleAdded(uint256 index, address value)
```

*Emitted when an oracle is added*

### Parameters

| Name  | Type    | Description      |
| ----- | ------- | ---------------- |
| index | uint256 | new oracle index |
| value | address | value            |

## OracleReplaced

```solidity
event OracleReplaced(uint256 index, address oldOracle, address newOracle)
```

*Emitted when an oracle is replaced*

### Parameters

| Name      | Type    | Description  |
| --------- | ------- | ------------ |
| index     | uint256 | oracle index |
| oldOracle | address | old value    |
| newOracle | address | new value    |

## OracleRemoved

```solidity
event OracleRemoved(uint256 index, address oldOracle)
```

*Emitted when an oracle is removed*

### Parameters

| Name      | Type    | Description  |
| --------- | ------- | ------------ |
| index     | uint256 | oracle index |
| oldOracle | address | old value    |

## JobIdUpdated

```solidity
event JobIdUpdated(uint256 index, bytes32 jobId)
```

*Emitted when market job id is updated*

### Parameters

| Name  | Type    | Description |
| ----- | ------- | ----------- |
| index | uint256 | index       |
| jobId | bytes32 | new value   |

## LinkRequestCreated

```solidity
event LinkRequestCreated(struct Chainlink.Request request)
```

*Emitted when a chainlink request is created*

### Parameters

| Name    | Type                     | Description          |
| ------- | ------------------------ | -------------------- |
| request | struct Chainlink.Request | link request details |

## PriceRequested

```solidity
event PriceRequested(uint8 collateralIndex, uint256 pairIndex, struct ITradingStorage.Id tradeId, struct ITradingStorage.Id pendingOrderId, enum ITradingStorage.PendingOrderType orderType, uint256 fromBlock, bool isLookback, bytes32 job, uint256 linkFeePerNode, uint256 nodesCount)
```

*Emitted when a price is requested to the oracles*

### Parameters

| Name            | Type                                  | Description                                                      |
| --------------- | ------------------------------------- | ---------------------------------------------------------------- |
| collateralIndex | uint8                                 | collateral index                                                 |
| pairIndex       | uint256                               | trading pair index                                               |
| tradeId         | struct ITradingStorage.Id             | trader id                                                        |
| pendingOrderId  | struct ITradingStorage.Id             | pending order id                                                 |
| orderType       | enum ITradingStorage.PendingOrderType | order type (market open/market close/limit open/stop open/etc.)  |
| fromBlock       | uint256                               | block number from which to start fetching prices (for lookbacks) |
| isLookback      | bool                                  | true if lookback                                                 |
| job             | bytes32                               | chainlink job id (market/lookback)                               |
| linkFeePerNode  | uint256                               | link fee distributed per node (1e18 precision)                   |
| nodesCount      | uint256                               | amount of nodes to fetch prices from                             |

## TradingCallbackExecuted

```solidity
event TradingCallbackExecuted(struct ITradingCallbacks.AggregatorAnswer a, enum ITradingStorage.PendingOrderType orderType)
```

*Emitted when a trading callback is called from the price aggregator*

### Parameters

| Name      | Type                                      | Description            |
| --------- | ----------------------------------------- | ---------------------- |
| a         | struct ITradingCallbacks.AggregatorAnswer | aggregator answer data |
| orderType | enum ITradingStorage.PendingOrderType     | order type             |

## PriceReceived

```solidity
event PriceReceived(struct ITradingStorage.Id orderId, uint16 pairIndex, bytes32 request, uint256 priceData, bool isLookback, bool usedInMedian)
```

*Emitted when a price is received from the oracles*

### Parameters

| Name         | Type                      | Description                                                                   |
| ------------ | ------------------------- | ----------------------------------------------------------------------------- |
| orderId      | struct ITradingStorage.Id | pending order id                                                              |
| pairIndex    | uint16                    | trading pair index                                                            |
| request      | bytes32                   | chainlink request id                                                          |
| priceData    | uint256                   | OrderAnswer compressed into uint256                                           |
| isLookback   | bool                      | true if lookback                                                              |
| usedInMedian | bool                      | false if order already executed because min answers count was already reached |

## LinkClaimedBack

```solidity
event LinkClaimedBack(uint256 amountLink)
```

*Emitted when LINK tokens are claimed back by gov fund*

### Parameters

| Name       | Type    | Description                        |
| ---------- | ------- | ---------------------------------- |
| amountLink | uint256 | amount of LINK tokens claimed back |

## TransferAndCallToOracleFailed

```solidity
error TransferAndCallToOracleFailed()
```

## SourceNotOracleOfRequest

```solidity
error SourceNotOracleOfRequest()
```

## RequestAlreadyPending

```solidity
error RequestAlreadyPending()
```

## OracleAlreadyListed

```solidity
error OracleAlreadyListed()
```

## InvalidCandle

```solidity
error InvalidCandle()
```

## WrongCollateralUsdDecimals

```solidity
error WrongCollateralUsdDecimals()
```

## InvalidPoolType

```solidity
error InvalidPoolType()
```


---

# 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/interfaces/libraries/ipriceaggregatorutils.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.
