ChainlinkAdapter
Inherits: IChainlinkAdapter, FeedRegistry, OracleAdapter, PriceRepository
This oracle adapter will attempt to use all available feeds to determine prices between pairs
State Variables
STALE_PRICE_THRESHOLD
If the difference between target and last update is greater than the STALE_PRICE_THRESHOLD, the price is considered stale
uint256 internal constant STALE_PRICE_THRESHOLD = 25 hours;
Functions
constructor
constructor(address _wrappedNativeToken, address _wrappedBTCToken)
FeedRegistry(_wrappedNativeToken, _wrappedBTCToken);
isPairSupported
Returns whether the pair has already been added to the adapter and if it supports the path required for the pair (true, true): Pair is fully supported (false, true): Pair is not supported, but can be added (false, false): Pair cannot be supported
tokenA and tokenB may be passed in either tokenA/tokenB or tokenB/tokenA order
function isPairSupported(address tokenA, address tokenB) external view returns (bool isCached, bool hasPath);
Parameters
Name | Type | Description |
---|---|---|
tokenA | address | One of the pair's tokens |
tokenB | address | The other of the pair's tokens |
Returns
Name | Type | Description |
---|---|---|
isCached | bool | True if the pair has been cached, false otherwise |
hasPath | bool | True if the pair has a valid path, false otherwise |
upsertPair
Stores or updates the given token pair data provider configuration. This function will let the adapter take some actions to configure the pair, in preparation for future quotes. Can be called many times in order to let the adapter re-configure for a new context
function upsertPair(address tokenA, address tokenB) external nonReentrant;
Parameters
Name | Type | Description |
---|---|---|
tokenA | address | One of the pair's tokens |
tokenB | address | The other of the pair's tokens |
getPrice
Returns the most recent price for the given token pair
function getPrice(address tokenIn, address tokenOut) external view returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
tokenIn | address | The exchange token (base token) |
tokenOut | address | The token to quote against (quote token) |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | The most recent price for the token pair (18 decimals) |
getPriceAt
Returns the price closest to target
for the given token pair
function getPriceAt(address tokenIn, address tokenOut, uint256 target) external view returns (UD60x18);
Parameters
Name | Type | Description |
---|---|---|
tokenIn | address | The exchange token (base token) |
tokenOut | address | The token to quote against (quote token) |
target | uint256 | Reference timestamp of the quote |
Returns
Name | Type | Description |
---|---|---|
<none> | UD60x18 | Historical price for the token pair (18 decimals) |
_getPriceAt
Returns a price based on the pricing path between tokenIn
and tokenOut
function _getPriceAt(address tokenIn, address tokenOut, uint256 target) internal view returns (UD60x18);
describePricingPath
Describes the pricing path used to convert the token to ETH
function describePricingPath(address token)
external
view
returns (AdapterType adapterType, address[][] memory path, uint8[] memory decimals);
Parameters
Name | Type | Description |
---|---|---|
token | address | The token from where the pricing path starts |
Returns
Name | Type | Description |
---|---|---|
adapterType | AdapterType | The type of adapter |
path | address[][] | The path required to convert the token to ETH |
decimals | uint8[] | The decimals of each token in the path |
pricingPath
Returns the pricing path that will be used when quoting the given pair
tokenA and tokenB may be passed in either tokenA/tokenB or tokenB/tokenA order
function pricingPath(address tokenA, address tokenB) external view returns (PricingPath);
Parameters
Name | Type | Description |
---|---|---|
tokenA | address | One of the pair's tokens |
tokenB | address | The other of the pair's tokens |
Returns
Name | Type | Description |
---|---|---|
<none> | PricingPath | The pricing path that will be used |
batchRegisterFeedMappings
Registers mappings of ERC20 token, and denomination (ETH, BTC, or USD) to feed
function batchRegisterFeedMappings(FeedMappingArgs[] memory args)
external
override(FeedRegistry, IFeedRegistry)
onlyOwner;
Parameters
Name | Type | Description |
---|---|---|
args | FeedMappingArgs[] | The arguments for the new mappings |
setTokenPriceAt
Set the price of token
denominated in denomination
at the given timestamp
function setTokenPriceAt(address token, address denomination, uint256 timestamp, UD60x18 price)
external
override(PriceRepository, IPriceRepository)
nonReentrant;
Parameters
Name | Type | Description |
---|---|---|
token | address | The exchange token (ERC20 token) |
denomination | address | The Chainlink token denomination to quote against (ETH, BTC, or USD) |
timestamp | uint256 | Reference timestamp (in seconds) |
price | UD60x18 | The amount of token denominated in denomination (18 decimals) |
_pricingPath
Returns the pricing path between tokenA
and tokenB
and the mapped tokens (unsorted)
function _pricingPath(address tokenA, address tokenB)
internal
view
returns (PricingPath path, address mappedTokenA, address mappedTokenB);
_getDirectPrice
Returns the price of tokenIn
denominated in tokenOut
when the pair is either ETH/USD, token/ETH or
token/USD
function _getDirectPrice(PricingPath path, address tokenIn, address tokenOut, uint256 target)
internal
view
returns (UD60x18);
_getPriceSameDenomination
Returns the price of tokenIn
denominated in tokenOut
when both tokens share the same token
denomination (either ETH or USD)
function _getPriceSameDenomination(PricingPath path, address tokenIn, address tokenOut, uint256 target)
internal
view
returns (UD60x18);
_getPriceDifferentDenomination
Returns the price of tokenIn
denominated in tokenOut
when one of the tokens uses ETH as the
denomination, and the other USD
function _getPriceDifferentDenomination(PricingPath path, address tokenIn, address tokenOut, uint256 target)
internal
view
returns (UD60x18);
_getPriceWBTCPrice
Returns the price of tokenIn
denominated in tokenOut
when the pair is token/WBTC
function _getPriceWBTCPrice(address tokenIn, address tokenOut, uint256 target) internal view returns (UD60x18);
_determinePricingPath
Returns the pricing path between tokenA
and tokenB
function _determinePricingPath(address tokenA, address tokenB) internal view virtual returns (PricingPath);
_tryToFindPath
Attempts to find the best pricing path for token
based on the conversionType
, if a feed exists
function _tryToFindPath(
address token,
ConversionType conversionType,
PricingPath preferredPath,
PricingPath fallbackPath
) internal view returns (PricingPath);
_fetchPrice
Returns the latest price of token
denominated in denomination
, if target
is 0, otherwise we
algorithmically search for a price which meets our criteria
function _fetchPrice(address token, address denomination, uint256 target, int8 factor)
internal
view
returns (uint256);
_fetchLatestPrice
Returns the latest price of token
denominated in denomination
function _fetchLatestPrice(address token, address denomination) internal view returns (uint256);
_fetchPriceAt
Returns the price of token
denominated in denomination
at or left of target
. If the price left of
target is stale, we revert and wait until a price override is set.
function _fetchPriceAt(address token, address denomination, uint256 target, int8 factor)
internal
view
returns (uint256);
_performBinarySearchForRoundData
Performs a binary search to find the round data closest to the target timestamp
function _performBinarySearchForRoundData(
BinarySearchDataInternal memory binarySearchData,
address feed,
uint16 phaseId,
uint64 nextAggregatorRoundId,
uint256 target
) internal view returns (BinarySearchDataInternal memory);
_latestRoundData
Try/Catch wrapper for Chainlink aggregator's latestRoundData() function
function _latestRoundData(address feed) internal view returns (uint80, int256, uint256, uint256, uint80);
_getRoundData
Try/Catch wrapper for Chainlink aggregator's getRoundData() function
function _getRoundData(address feed, uint80 roundId) internal view returns (uint80, int256, uint256, uint256, uint80);
_aggregator
Returns the Chainlink aggregator for token
/ denomination
function _aggregator(address token, address denomination) internal view returns (address[] memory aggregator);
_aggregatorDecimals
Returns decimals for aggregator
function _aggregatorDecimals(address aggregator) internal view returns (uint8);
_getPriceAgainstUSD
Returns the scaled price of token
denominated in USD at target
function _getPriceAgainstUSD(address token, uint256 target) internal view returns (UD60x18);
_getPriceAgainstETH
Returns the scaled price of token
denominated in ETH at target
function _getPriceAgainstETH(address token, uint256 target) internal view returns (UD60x18);
_getETHUSD
Returns the scaled price of ETH denominated in USD at target
function _getETHUSD(uint256 target) internal view returns (UD60x18);
_getBTCUSD
Returns the scaled price of BTC denominated in USD at target
function _getBTCUSD(uint256 target) internal view returns (UD60x18);
_getWBTCBTC
Returns the scaled price of WBTC denominated in BTC at target
function _getWBTCBTC(uint256 target) internal view returns (UD60x18);
_revertIfPriceLeftOfTargetStale
Revert if the difference between target
and updateAt
is greater than STALE_PRICE_THRESHOLD
function _revertIfPriceLeftOfTargetStale(uint256 updatedAt, uint256 target) internal pure;
_revertIfInvalidDenomination
Revert if denomination
is not a valid
function _revertIfInvalidDenomination(address denomination) internal pure;