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

onthehunt - Calculation of leverage ratio is not as intended #42

Open
sherlock-admin2 opened this issue Oct 21, 2024 · 0 comments
Open

Comments

@sherlock-admin2
Copy link

sherlock-admin2 commented Oct 21, 2024

onthehunt

Medium

Calculation of leverage ratio is not as intended

Summary

The struct MethodologySettings has a field called recenteringSpeed which states that it is used as a value to go back to the targetLeverageRatio and it is used in _calculateNewLeverageRatio().

The issue stems from that the calculation doesn't make the _currentLeverageRatio go to the targetLeverageRatio,
just as a percentage of recenteringSpeed

Root Cause

The problem stems from the following:
Let's say we have a minLeverageRatio of 2, maxLeverageRatio of 3, targetLeverageRatio of 2.5,
_currentLeverageRatio of 2.25 and recenteringSpeed being equal to a value corresponding to 15%.

(I will use floating numbers for simplicity, we all know Solidity works with whole numbers only)

a will be -> 2.5 * 1.15 = 2.875
b will be -> 0.85 * 2.25 = 1.9125
c will be -> a + b = 4.7875
d will take the minimum value between c and maxLeverageRatio, which will be later in our example -> 3
and the return statement will take the max value between minLeverageRatio and d which will be again the latter of -> 3

We started with a _currentLeverageRatio of 2.25 and right now we are at maxLeverageRatio of 3 after the calculation which is against the specified comment and is a lot more volatile than anticipated, given the 15% of recenteringSpeed.

Internal pre-conditions

No response

External pre-conditions

No response

Attack Path

  1. Keeper bot sees that it can call rebalance() and calls it
  2. Inside it, we execute _calculateNewLeverageRatio() to calculate the new leverage ratio
  3. The calculation is a lot more volatile than expected and results in unexpected behaviour

Impact

The impact is an incorrect leverage calculation, resulting in a potentially lost funds

PoC

No response

Mitigation

Rewrite the _calculateNewLeverageRatio() with a logic changing the _currentLeverageRatio correctly given the recenteringSpeed, so it works as expected

@sherlock-admin2 sherlock-admin2 changed the title Polished Walnut Capybara - Calculation of leverage ratio is not as intended onthehunt - Calculation of leverage ratio is not as intended Oct 28, 2024
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

No branches or pull requests

1 participant