AggregatorOracle
AggregatorOracle is a smart contract that provides aggregated price feeds from multiple oracle sources.
Functions
_setPrimarySources
This function sets the primary oracle sources for a given token.
Parameters:
token
address
address of the token for which the primary oracle sources are being set
maxPriceDeviation
uint256
maximum price deviation allowed for the token's price feed
sources
array
an array of IBaseOracle
contracts representing the primary oracle sources for the token
Events:
SetPrimarySources
: emitted after the primary oracle sources have been set successfully. The event contains the following parameters:token
: address of the token for which the primary oracle sources have been set.maxPriceDeviation
: maximum price deviation allowed for the token's price feed.oracles
: an array ofIBaseOracle
contracts representing the primary oracle sources for the token.
Requirements:
The
token
parameter must not be the zero address.The
maxPriceDeviation
parameter must not be greater thanConstants.MAX_PRICE_DEVIATION
(a constant defined inBlueBerryConst.sol
).The length of the
sources
array must be less than or equal to 3.The address of each
IBaseOracle
contract in thesources
array must not be the zero address.
setPrimarySources
This function sets the primary oracle sources for a given token, and can only be called by the owner of the contract
Parameters:
token
address
address of the token for which the primary oracle sources are being set
maxPriceDeviation
uint256
maximum price deviation allowed for the token's price feed
sources
array
an array of IBaseOracle
contracts representing the primary oracle sources for the token
Events:
SetPrimarySources
: emitted after the primary oracle sources have been set successfully. The event contains the following parameters:token
: address of the token for which the primary oracle sources have been set.maxPriceDeviation
: maximum price deviation allowed for the token's price feed.oracles
: an array ofIBaseOracle
contracts representing the primary oracle sources for the token.
Requirements:
This function has the same requirements as the
_setPrimarySources
function.
setMultiPrimarySources
This function sets multiple primary oracle sources for multiple tokens. This function first validates the inputs by ensuring that the length of all arrays is the same. It then calls the internal function _setPrimarySources() for each token to set its primary sources.
Parameters:
tokens
array
an array of addresses of the token for which the primary oracle sources are being set
maxPriceDeviationList
array
an array of maximum price deviations in base 10000 allowed for the token's price feed
allSources
array
a 2D array of primary oracle sources for each token
_isValidPrices
This is an internal function used to validate the deviation of two given prices.
Parameters:
price0
uint256
the first price in base 1e18
price1
uint256
the second price in base 1e18
maxPriceDeviation
uint256
the maximum price deviation in base 10000
getPrice
This function returns the USD price of a given token. The price is multiplied by 10^18. It also supports at most 3 oracle sources per token.
Parameters:
token
(address): The address of the token to get the price of
Return:
uint256
: The USD price of the token, multiplied by 10^18
Logic:
Check if there is at least one primary oracle source for the token. If there isn't, revert with the NO_PRIMARY_SOURCE error.
Create a dynamic array of uint256 called prices with a length of candidateSourceCount, which is the number of primary oracle sources for the token.
Loop through each primary oracle source for the token and try to get the price. If successful, add the price to the prices array and increment the validSourceCount.
If there are no valid prices, revert with the NO_VALID_SOURCE error.
Sort the prices in ascending order.
Get the max price deviation for the token.
Depending on the number of valid sources, calculate the price using one of the following methods:
If there is only one valid source, return the price.
If there are two valid sources, check if the prices are within the max price deviation. If they are, return the average price. If not, revert with the EXCEED_DEVIATION error.
If there are three valid sources, check if each pair of prices is within the max price deviation. If all pairs are within the deviation threshold, return the median price. If one pair is within the deviation threshold, return the average of that pair. If none of the pairs are within the deviation threshold, revert with the EXCEED_DEVIATION error.
Last updated