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

fix(oracle): use 512 bit multiplication #36

Merged
merged 12 commits into from
Nov 15, 2023

Conversation

Rubilmax
Copy link
Contributor

Jean-Grimal
Jean-Grimal previously approved these changes Nov 3, 2023
MerlinEgalite
MerlinEgalite previously approved these changes Nov 3, 2023
@Rubilmax Rubilmax dismissed stale reviews from MerlinEgalite and Jean-Grimal via e8105b3 November 7, 2023 10:23
@Rubilmax
Copy link
Contributor Author

Rubilmax commented Nov 7, 2023

Just pushed an alternative order for the price which should be "safer", because chainlink price feeds are 18 decimals max per their specs, which leaves 41 decimals to the vault before overflowing

@QGarchery
Copy link
Contributor

Just pushed an alternative order for the price which should be "safer", because chainlink price feeds are 18 decimals max per their specs, which leaves 41 decimals to the vault before overflowing

Sounds good, although if the base prices are over 1 it can be less than 41 decimals. For example, if you are pricing BTC/ETH, you get an answer that is greater than 1e18. I think that overall this has a very low chance of overflowing, but it should be checked at every oracle deployment

@MerlinEgalite
Copy link
Contributor

Just pushed an alternative order for the price which should be "safer", because chainlink price feeds are 18 decimals max per their specs, which leaves 41 decimals to the vault before overflowing

Sounds good, although if the base prices are over 1 it can be less than 41 decimals. For example, if you are pricing BTC/ETH, you get an answer that is greater than 1e18. I think that overall this has a very low chance of overflowing, but it should be checked at every oracle deployment

We could check it in the constructor maybe?

@QGarchery
Copy link
Contributor

We could check it in the constructor maybe?

I don't think it would be enough because prices can fluctuate. So, for each oracle, we have to make sure that there is a very big buffer that cannot get reached in practice

Jean-Grimal
Jean-Grimal previously approved these changes Nov 7, 2023
Copy link
Contributor Author

@Rubilmax Rubilmax left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@QGarchery QGarchery left a comment

Choose a reason for hiding this comment

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

Ok to use this solution in theory, because of this and because the definition of SCALE_FACTOR would revert at deployment if it over/underflows (which is a nice property). I have the following reservations though:

  • I would like to have an opinion on Alternative oracle to avoid overflows #37 because I think that this solution is actually pretty clean, cc @MathisGD
  • it would be nice to document the risk of overflow, there is a check that has to be done before deployment
  • everyone must be aware that we are using a complex library to do the computation. Should someone check it out ? (I have not done it yet)

@MerlinEgalite
Copy link
Contributor

MerlinEgalite commented Nov 9, 2023

I'm still in favor of this solution and I'm fine with using the uniswap library. And if we want better confidence we can even use OZ version: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/932fddf69a699a9a80fd2396fd1a2ab91cdda123/contracts/utils/math/Math.sol#L123

which I would be in favor.

Doing it on top #42

Copy link
Contributor

@MerlinEgalite MerlinEgalite left a comment

Choose a reason for hiding this comment

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

I think we should add the assumptions in terms of decimals for the vaultConversionSample

@Rubilmax
Copy link
Contributor Author

Rubilmax commented Nov 10, 2023

I think we should add the assumptions in terms of decimals for the vaultConversionSample

Something along the lines of "vaultConversionSample should be large enough to provide enough precision when quoting from the vault, but small enough to avoid overflowing."

We can't be more precise in the general case because it also depends on the prices of base assets involved (restricting decimals is not enough to prevent overflowing)

MerlinEgalite
MerlinEgalite previously approved these changes Nov 10, 2023
@MerlinEgalite
Copy link
Contributor

I think we should add the assumptions in terms of decimals for the vaultConversionSample

Something along the lines of "vaultConversionSample should be large enough to provide enough precision when quoting from the vault, but small enough to avoid overflowing."

We can't be more precise in the general case because it also depends on the prices of base assets involved (restricting decimals is not enough to prevent overflowing)

Maybe I can add it to #44 instead

Copy link
Contributor Author

@Rubilmax Rubilmax left a comment

Choose a reason for hiding this comment

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

LGTM

MerlinEgalite
MerlinEgalite previously approved these changes Nov 10, 2023
Jean-Grimal
Jean-Grimal previously approved these changes Nov 14, 2023
@Rubilmax Rubilmax dismissed stale reviews from Jean-Grimal and MerlinEgalite via 4dece3c November 15, 2023 10:13
MerlinEgalite
MerlinEgalite previously approved these changes Nov 15, 2023
Copy link
Contributor

@QGarchery QGarchery left a comment

Choose a reason for hiding this comment

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

One change requested, otherwise LGTM

src/ChainlinkOracle.sol Outdated Show resolved Hide resolved
Jean-Grimal
Jean-Grimal previously approved these changes Nov 15, 2023
Signed-off-by: Romain Milon <[email protected]>
Jean-Grimal
Jean-Grimal previously approved these changes Nov 15, 2023
QGarchery
QGarchery previously approved these changes Nov 15, 2023
MerlinEgalite
MerlinEgalite previously approved these changes Nov 15, 2023
@MerlinEgalite MerlinEgalite dismissed stale reviews from QGarchery, Jean-Grimal, and themself via e94270f November 15, 2023 19:04
@MerlinEgalite MerlinEgalite merged commit d351d3e into main Nov 15, 2023
2 checks passed
@MerlinEgalite MerlinEgalite deleted the fix/512-bit-multiplication branch November 15, 2023 19:26
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.

Risk of multiplication overflow
4 participants