UniswapV3PriceProvider
UniswapV3PriceProvider
Price provider contract that reads prices from UniswapV3
Methods
acceptOwnership
Transfers ownership of the contract to a pending owner Can only be called by the pending owner.
adjustOracleCardinality
Adjust UniV3 pool cardinality to Silo's requirements
This could be used manually to setup pool, so it will be ready to use in the future. There is only one case, when it could be needed: if we have more than one pool for _asset
and you want to use UniswapV3 but the pool does not have enough observations (liquidity == 1). You can run this method manually and it will set valid number of observations, so the pool can be used once price data is ready.
Parameters
Name | Type | Description |
---|---|---|
_asset | address | asset used to lookup the pool address to increase the cardinality for |
assetSupported
Informs if PriceProvider is setup for asset. It does not means PriceProvider can provide price right away. Some providers implementations need time to "build" buffor for TWAP price, so price may not be available yet but this method will return true.
Parameters
Name | Type | Description |
---|---|---|
_asset | address | asset in question |
Returns
Name | Type | Description |
---|---|---|
supported | bool | TRUE if asset has been setup, otherwise false |
changeBlockTime
Change block time which is used to adjust oracle cardinality fot providing TWAP prices
Parameters
Name | Type | Description |
---|---|---|
_blockTime | uint8 | it is better to set it bit lower than higher that avg block time eg. if ETH block time is 13~13.5s, you can set it to 11-12s based on |
changePeriodForAvgPrice
Change period for which to calcualted TWAP prices
WARNING: There is a possibility that when we change this period, UniV3 pool that is already initialized and set as oracle for asset, can throw. This can happen when it will not be able calculate TWAP for new period and it can potentially lock the Silo until we have necessary observations. If UniV3 is NOT only available oracle for asset, we can change the oracle and Silo will be unlocked.
Parameters
Name | Type | Description |
---|---|---|
_period | uint32 | new period in seconds, ie. 1800 means 30 min |
getPrice
Returns "Time-Weighted Average Price" for an asset. Calculates TWAP price for quote/asset. It unifies all tokens decimal to 18, examples: - if asses == quote it returns 1e18 - if asset is USDC and quote is ETH and ETH costs ~$3300 then it returns ~0.0003e18 WETH per 1 USDC
UniV3 saves price only on: mint, burn and swap. Mint and burn will write observation only when "current tick is inside the passed range" of ticks. I think that means, that if we minting/burning outside ticks range (so outside current price) it will not modify observation. So we left with swap. Swap will write observation under this condition: // update tick and write an oracle entry if the tick change if (state.tick != slot0Start.tick) { that means, it is possible that price will be up to date (in a range of same tick) but observation timestamp will be old. Every pool by default comes with just one slot for observation (cardinality == 1). We can increase number of slots so TWAP price will be "better". When we increase, we have to wait until new tx will write new observation. Based on all above, we can tell how old is observation, but this does not mean the price is wrong. UniV3 recommends to use observe
and OracleLibrary.consult
uses it. observe
reverts if secondsAgos
> oldest observation, means, if there is any price observation in selected time frame, it will revert. Otherwise it will return either exact TWAP price or by interpolation. Conclusion: we can choose how many observation pool will be storing, but we need to remember, not all of them might be used to provide our price. Final question is: how many observations we need? How UniV3 calculates TWAP we ask for TWAP on time range ago:now using OracleLibrary.consult
, it is all about find the right tick - we call IUniswapV3Pool(pool).observe(secondAgos)
that returns two accumulator values (for ago and now) - each observation is resolved by observeSingle
- for now we just using latest observation, and if it does not match timestamp, we interpolate (!) and this is how we got the tickCumulative, so in extreme situation, if last observation was made day ago, UniV3 will interpolate to reflect tickCumulative at current time - for ago we search for observation using getSurroundingObservations
that give us before and after observation, base on which we calculate "avg" and we have target tickCumulative - getSurroundingObservations: it's job is to find 2 observations based on which we calculate tickCumulative here is where all calculations can revert, if ago < oldest observation, otherwise it will be calculated either by interpolation or we will have exact match - now with both _tickCumulative_s we calculating TWAP recommended observations are = 30 min / blockTime
Parameters
Name | Type | Description |
---|---|---|
_asset | address | address of an asset for which to read price |
Returns
Name | Type | Description |
---|---|---|
price | uint256 | of asses with 18 decimals, throws when pool is not ready yet to provide price |
hasEnoughObservations
Check if UniV3 pool has enough cardinality to meet Silo's requirements
Parameters
Name | Type | Description |
---|---|---|
_pool | address | UniV3 pool address |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | TURE if has enough observations |
oldestObservationTimestamp | uint32 | timestamp of the oldest observation |
owner
Returns the address of the current owner.
Returns
Name | Type | Description |
---|---|---|
_0 | address | undefined |
pendingOwner
Returns the address of the pending owner.
Returns
Name | Type | Description |
---|---|---|
_0 | address | undefined |
pools
Maps asset address to UniV3 pool
Parameters
Name | Type | Description |
---|---|---|
_0 | address | undefined |
Returns
Name | Type | Description |
---|---|---|
_0 | contract IUniswapV3Pool | undefined |
priceCalculationData
priceCalculationData: - periodForAvgPrice: Number of seconds for which time-weighted average should be calculated, ie. 1800 is 30 min - blockTime: Estimated blockchain block time
Returns
Name | Type | Description |
---|---|---|
periodForAvgPrice | uint32 | undefined |
blockTime | uint8 | undefined |
priceProviderPing
Helper method that allows easily detects, if contract is PriceProvider
this can save us from simple human errors, in case we use invalid address but this should NOT be treated as security check
Returns
Name | Type | Description |
---|---|---|
_0 | bool | always true |
priceProvidersRepository
PriceProvidersRepository address
Returns
Name | Type | Description |
---|---|---|
_0 | contract IPriceProvidersRepository | undefined |
quoteToken
Token address which prices are quoted in. Must be the same as PriceProvidersRepository.quoteToken
Returns
Name | Type | Description |
---|---|---|
_0 | address | undefined |
renounceOwnership
Leaves the contract without owner. It will not be possible to call onlyOwner
functions anymore. Can only be called by the current owner. NOTE: Renouncing ownership will leave the contract without an owner, thereby removing any functionality that is only available to the owner.
setupAsset
Setup pool for asset. Use it also for update.
Parameters
Name | Type | Description |
---|---|---|
_asset | address | asset address |
_pool | contract IUniswapV3Pool | UniV3 pool address |
transferOwnership
Transfers ownership of the contract to a new account (newOwner
). Can only be called by the current owner.
Parameters
Name | Type | Description |
---|---|---|
newOwner | address | undefined |
transferPendingOwnership
Transfers pending ownership of the contract to a new account (newPendingOwner
). Can only be called by the current owner.
Parameters
Name | Type | Description |
---|---|---|
newPendingOwner | address | undefined |
uniswapV3Factory
UniswapV3 factory contract
Returns
Name | Type | Description |
---|---|---|
_0 | contract IUniswapV3Factory | undefined |
verifyPool
It verifies if provider pool for asset (and quote token) is valid. Throws when there is no pool or pool is empty (zero liquidity).
Parameters
Name | Type | Description |
---|---|---|
_asset | address | asset for which prices are going to be calcualted |
_pool | contract IUniswapV3Pool | UniV3 pool address |
Returns
Name | Type | Description |
---|---|---|
_0 | bool | true if verification successful, otherwise throws |
Events
NewBlockTime
Emitted when blockTime changes
Parameters
Name | Type | Description |
---|---|---|
blockTime | uint8 | block time in seconds |
NewPeriod
Emitted when TWAP period changes
Parameters
Name | Type | Description |
---|---|---|
period | uint32 | new period in seconds, ie. 1800 means 30 min |
OwnershipPending
Parameters
Name | Type | Description |
---|---|---|
previousOwner | address | undefined |
newOwner | address | undefined |
OwnershipTransferred
Parameters
Name | Type | Description |
---|---|---|
previousOwner | address | undefined |
newOwner | address | undefined |
PoolForAsset
Emitted when UniV3 pool is set for asset
Parameters
Name | Type | Description |
---|---|---|
asset | address | asset address |
pool | contract IUniswapV3Pool | UniV3 pool address |
Last updated