Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative oracle to avoid overflows #37

Closed
wants to merge 1 commit into from

Conversation

QGarchery
Copy link
Contributor

@QGarchery QGarchery commented Nov 2, 2023

This PR implements the third proposition in #15 (comment). The idea is to use a sample amount, and to only convert between amounts, in order to avoid multiplication overflow.

Remarks:

  1. the sample amount is chosen to be the full unit of the denominator token. If we have A/B & B/C as base feeds, and D/E & E/C as quote feeds, then we call C the denominator token. The sample amount is determined in the first line of the price() function, but could be set at deployment instead
  2. there are many optimizations left to do: notably the computation of the exponential of decimals and caching feed.decimals() in the Chainlink library
  3. the returned prices have been checked to have less than 0.1% error compared to the original oracle

@QGarchery QGarchery self-assigned this Nov 2, 2023
@QGarchery QGarchery marked this pull request as draft November 2, 2023 15:38
@QGarchery QGarchery requested review from MathisGD, a team, Rubilmax, MerlinEgalite and Jean-Grimal and removed request for a team November 2, 2023 15:39
Quantity memory base = Quantity(denominatorSample, DENOMINATOR_TOKEN_DECIMALS);
base = BASE_FEED_2.convert(base, BASE_TOKEN_2_DECIMALS);
base = BASE_FEED_1.convert(base, BASE_TOKEN_1_DECIMALS);
base.amount = address(VAULT) == address(0) ? base.amount : VAULT.convertToShares(base.amount);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't we loosing precision here if base.amount is too low?

AggregatorV3Interface quoteFeed2,
uint256 quoteToken2Decimals,
uint256 denominatorTokenDecimals
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll need to update it with the require we added in the other oracle I think


/// @inheritdoc IOracle
function price() external view returns (uint256) {
uint256 denominatorSample = 10 ** DENOMINATOR_TOKEN_DECIMALS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be an immutable


import {ErrorsLib} from "./ErrorsLib.sol";

struct Quantity {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should specify what decimals are we talking about, the feed? The token?

@QGarchery
Copy link
Contributor Author

Closing in favor of #36. The idea is still interesting and could be used for a future version of the oracle

@QGarchery QGarchery closed this Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants