Use preview functions for precise accounting #96
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
convertToAssets
andconvertToShares
are not guaranteed to use current onchain conditions to calculate conversions and may use some approximation to return more of an "average" prize. Yield vaults that implement these as average price oracles may behave poorly with the prize vault when these functions are used for precise accounting functions.To address this, usage of these functions have been replaced with the yield vault
preview
functions instead to provide precise accounting based on current onchain conditions. However, this comes with new challenges since these preview functions may revert in some scenarios, but must be used in the prize vault'smaxDeposit
,maxWithdraw
, etc. functions, which MUST not revert. The following changes have been made to allow the usage of these preview functions:totalPreciseAssets()
(returns the total assets using current rates from the yield vaultpreviewRedeem
function)_tryGetTotalPreciseAssets()
(Attempts to fetch thetotalPreciseAssets
, but catches any reversions and indicates that the call was unsuccessful. This function is used in themaxDeposit
,maxWithdraw
, etc. functions so they can return zero instead of reverting)totalAssets()
is no longer used for any accounting functions besidesconvertToAssets
andconvertToShares
, since all three of these functions are treated as "approximations" by the spec.