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
_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
_asset
address
asset in question
Returns
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
_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 priceCalculationData.periodForAvgPrice
and priceCalculationData.blockTime
price provider calculates number of blocks for (cardinality) requires for TWAP price. Unfortunately block time can change and this can lead to issues with getting price. Edge case will be when we set _blockTime
to 1, then we have 100% guarantee, that no matter how real block time changes, we always can get price. Downside will be cost of initialization. That's why it is better to set a bit lower and adjust (decrease) in case of issues.
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
_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
_asset
address
address of an asset for which to read price
Returns
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
_pool
address
UniV3 pool address
Returns
_0
bool
TURE if has enough observations
oldestObservationTimestamp
uint32
timestamp of the oldest observation
owner
Returns the address of the current owner.
Returns
_0
address
undefined
pendingOwner
Returns the address of the pending owner.
Returns
_0
address
undefined
pools
Maps asset address to UniV3 pool
Parameters
_0
address
undefined
Returns
_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
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
_0
bool
always true
priceProvidersRepository
PriceProvidersRepository address
Returns
_0
contract IPriceProvidersRepository
undefined
quoteToken
Token address which prices are quoted in. Must be the same as PriceProvidersRepository.quoteToken
Returns
_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
_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
newOwner
address
undefined
transferPendingOwnership
Transfers pending ownership of the contract to a new account (newPendingOwner
). Can only be called by the current owner.
Parameters
newPendingOwner
address
undefined
uniswapV3Factory
UniswapV3 factory contract
Returns
_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
_asset
address
asset for which prices are going to be calcualted
_pool
contract IUniswapV3Pool
UniV3 pool address
Returns
_0
bool
true if verification successful, otherwise throws
Events
NewBlockTime
Emitted when blockTime changes
Parameters
blockTime
uint8
block time in seconds
NewPeriod
Emitted when TWAP period changes
Parameters
period
uint32
new period in seconds, ie. 1800 means 30 min
OwnershipPending
Parameters
previousOwner indexed
address
undefined
newOwner indexed
address
undefined
OwnershipTransferred
Parameters
previousOwner indexed
address
undefined
newOwner indexed
address
undefined
PoolForAsset
Emitted when UniV3 pool is set for asset
Parameters
asset indexed
address
asset address
pool indexed
contract IUniswapV3Pool
UniV3 pool address
Last updated