# PriceAggregatorUtils

*GNSPriceAggregator facet external library*

## initializePriceAggregator

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

*Check IPriceAggregatorUtils interface for documentation*

## updateLinkUsdPriceFeed

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

*Check IPriceAggregatorUtils interface for documentation*

## updateCollateralUsdPriceFeed

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

*Check IPriceAggregatorUtils interface for documentation*

## updateCollateralGnsLiquidityPool

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

*Check IPriceAggregatorUtils interface for documentation*

## updateTwapInterval

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

*Check IPriceAggregatorUtils interface for documentation*

## updateMinAnswers

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

*Check IPriceAggregatorUtils interface for documentation*

## addOracle

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

*Check IPriceAggregatorUtils interface for documentation*

## replaceOracle

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

*Check IPriceAggregatorUtils interface for documentation*

## removeOracle

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

*Check IPriceAggregatorUtils interface for documentation*

## setMarketJobId

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

*Check IPriceAggregatorUtils interface for documentation*

## setLimitJobId

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

*Check IPriceAggregatorUtils interface for documentation*

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

*Check IPriceAggregatorUtils interface for documentation*

## fulfill

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

*Check IPriceAggregatorUtils interface for documentation*

## claimBackLink

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

*Check IPriceAggregatorUtils interface for documentation*

## getLinkFee

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

*Check IPriceAggregatorUtils interface for documentation*

## getCollateralPriceUsd

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

*Check IPriceAggregatorUtils interface for documentation*

## getUsdNormalizedValue

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

*Check IPriceAggregatorUtils interface for documentation*

## getCollateralFromUsdNormalizedValue

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

*Check IPriceAggregatorUtils interface for documentation*

## getGnsPriceUsd

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

*Check IPriceAggregatorUtils interface for documentation*

## getGnsPriceUsd

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

*Check IPriceAggregatorUtils interface for documentation*

## getGnsPriceCollateralAddress

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

*Check IPriceAggregatorUtils interface for documentation*

## getGnsPriceCollateralIndex

```solidity
function getGnsPriceCollateralIndex(uint8 _collateralIndex) public view returns (uint256 _price)
```

*Check IPriceAggregatorUtils interface for documentation*

## getLinkUsdPriceFeed

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

*Check IPriceAggregatorUtils interface for documentation*

## getTwapInterval

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

*Check IPriceAggregatorUtils interface for documentation*

## getMinAnswers

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

*Check IPriceAggregatorUtils interface for documentation*

## getMarketJobId

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

*Check IPriceAggregatorUtils interface for documentation*

## getLimitJobId

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

*Check IPriceAggregatorUtils interface for documentation*

## getOracle

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

*Check IPriceAggregatorUtils interface for documentation*

## getOracles

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

*Check IPriceAggregatorUtils interface for documentation*

## getCollateralGnsLiquidityPool

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

*Check IPriceAggregatorUtils interface for documentation*

## getCollateralUsdPriceFeed

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

*Check IPriceAggregatorUtils interface for documentation*

## getPriceAggregatorOrder

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

*Check IPriceAggregatorUtils interface for documentation*

## getPriceAggregatorOrderAnswers

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

*Check IPriceAggregatorUtils interface for documentation*

## getChainlinkToken

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

*Check IPriceAggregatorUtils interface for documentation*

## getRequestCount

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

*Check IPriceAggregatorUtils interface for documentation*

## getPendingRequest

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

*Check IPriceAggregatorUtils 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 IPriceAggregator.PriceAggregatorStorage 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.*

## validCollateralIndex

```solidity
modifier validCollateralIndex(uint8 _collateralIndex)
```

*Reverts if collateral index is not valid*

## \_median

```solidity
function _median(struct IPriceAggregator.OrderAnswer[] _array) internal pure returns (uint64)
```

*returns median price of array (1 price only)*

### Parameters

| Name    | Type                                                                                                                          | Description     |
| ------- | ----------------------------------------------------------------------------------------------------------------------------- | --------------- |
| \_array | [IPriceAggregator.OrderAnswer\[\]](/developer/technical-reference/contracts/interfaces/types/ipriceaggregator.md#orderanswer) | array of values |

## \_medianLookbacks

```solidity
function _medianLookbacks(struct IPriceAggregator.OrderAnswer[] _array) internal pure returns (uint64 open, uint64 high, uint64 low)
```

*returns median prices of array (open, high, low)*

### Parameters

| Name    | Type                                                                                                                          | Description     |
| ------- | ----------------------------------------------------------------------------------------------------------------------------- | --------------- |
| \_array | [IPriceAggregator.OrderAnswer\[\]](/developer/technical-reference/contracts/interfaces/types/ipriceaggregator.md#orderanswer) | array of values |

## \_swap

```solidity
function _swap(uint256[] _array, uint256 _i, uint256 _j) internal pure
```

*swaps two elements in array*

### Parameters

| Name    | Type       | Description             |
| ------- | ---------- | ----------------------- |
| \_array | uint256\[] | array of values         |
| \_i     | uint256    | index of first element  |
| \_j     | uint256    | index of second element |

## \_sort

```solidity
function _sort(uint256[] _array, uint256 begin, uint256 end) internal pure
```

*sorts array of uint256 values*

### Parameters

| Name    | Type       | Description     |
| ------- | ---------- | --------------- |
| \_array | uint256\[] | array of values |
| begin   | uint256    | start index     |
| end     | uint256    | end index       |

## \_getCollateralPrecisionDelta

```solidity
function _getCollateralPrecisionDelta(uint8 _collateralIndex) internal view returns (uint256)
```

*Returns precision delta of collateral as uint256*

### Parameters

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


---

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