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 rounding of 2CLP pools #1193

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Fix rounding of 2CLP pools #1193

wants to merge 6 commits into from

Conversation

joaobrunoah
Copy link
Contributor

@joaobrunoah joaobrunoah commented Dec 19, 2024

Description

Certora found some rounding issues related to some functions of 2-CLP pool:

  1. Function calculateQuadratic(), from Gyro2CLPMath.sol, was not rounded properly.
  2. When calculating virtual offsets, rounding was not applied.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Dependency changes
  • Code refactor / cleanup
  • Optimization: [ ] gas / [ ] bytecode
  • Documentation or wording changes
  • Other

Checklist:

  • The diff is legible and has no extraneous changes
  • Complex code has been commented, including external interfaces
  • Tests have 100% code coverage
  • The base branch is either main, or there's a description of how to merge

Copy link
Contributor

@jubeira jubeira left a comment

Choose a reason for hiding this comment

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

Nice, thanks @joaobrunoah !

I don't agree with the changes for computeBalance, but otherwise makes sense!

pkg/pool-gyro/contracts/Gyro2CLPPool.sol Outdated Show resolved Hide resolved
pkg/pool-gyro/contracts/Gyro2CLPPool.sol Outdated Show resolved Hide resolved
pkg/pool-gyro/contracts/lib/Gyro2CLPMath.sol Show resolved Hide resolved
pkg/pool-gyro/contracts/lib/Gyro2CLPMath.sol Show resolved Hide resolved
@joaobrunoah joaobrunoah requested a review from jubeira December 19, 2024 20:16
@joaobrunoah joaobrunoah requested a review from jubeira December 20, 2024 15:18
Copy link
Contributor

@jubeira jubeira left a comment

Choose a reason for hiding this comment

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

Looks better now! I have a doubt about the invariant rounding direction. Not 100% sure it actually matters in the context of a swap though. Let's try to find out before merging.

Comment on lines +171 to +176
uint256 currentInvariant = Gyro2CLPMath.calculateInvariant(
balances,
sqrtAlpha,
sqrtBeta,
kind == SwapKind.EXACT_IN ? Rounding.ROUND_DOWN : Rounding.ROUND_UP
);
Copy link
Contributor

@jubeira jubeira Dec 20, 2024

Choose a reason for hiding this comment

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

The virtual parameters are always proportional to the invariant, and we're rounding one of them up and the other one down.

Technically we should send an invariant rounded down to the virtual parameter that we round down, and an invariant rounded up to the parameter that we're rounding up.

I'm not sure it's really worth it though, as you'd need to compute two different invariants. This is only used for swaps, so rounding is not as impactful as with joins / exits.

Comment on lines +178 to +183
// virtualBalanceIn is always rounded up, because:
// * If swap is EXACT_IN: a bigger virtualBalanceIn leads to a lower amount out;
// * If swap is EXACT_OUT: a bigger virtualBalanceIn leads to a bigger amount in;
// virtualBalanceOut is always rounded down, because:
// * If swap is EXACT_IN: a lower virtualBalanceOut leads to a lower amount out;
// * If swap is EXACT_OUT: a lower virtualBalanceOut leads to a bigger amount in;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks correct now.

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