-
Notifications
You must be signed in to change notification settings - Fork 5
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
pashap9990 - Funding fee will be zero because of precision loss #72
Comments
You've created a valid escalation! To remove the escalation from consideration: Delete your comment. You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final. |
To clarify, due to this precision loss, there will be a 1% loss of the funding fee or am I missing something? |
open_interest = 1,099,999e6 long_util = open_interest / reserve / 100 = 10.99 lets assume there is a long position with 10000e6 colleral and user want to close his position after a year[15778800 blocks per year] result without precision loss borrowing_paid = collateral * borrowing_sum / DENOM borrowing_paid = 10,000e6 * 15778800 * 10.99 / 1e9 = 1,734,090,120[its mean user has to pay $1734 as borrowing fee] result with precision loss borrowing_paid = collateral * borrowing_sum / DENOM LPs loss = $157[~1%] |
#60 dup of this issue |
I agree that this issue is correct and indeed identifies the precision loss showcasing the 1% loss. Planning to accept the escalation and validate with medium severity. |
Result: |
Escalations have been resolved successfully! Escalation status:
|
pashap9990
Medium
Funding fee will be zero because of precision loss
Summary
funding fee in some cases will be zero because of precision loss
Root Cause
long_utilization = base_interest / (base_reserve / 100)
short_utilization = quote_interest / (quote_reserve / 100)
borrow_long_fee = max_fee * long_utilization
[min = 10]borrow_short_fee = max_fee * short_utilization
[min = 10]funding_fee_long = borrow_long_fee * (long_utilization - short_utilization) / 100
funding_fee_short = borrow_short_fee * (short_utilization - long_utilization) / 100
let's assume alice open a long position with min collateral[5e6] and leverage 2x when btc/usdt $50,000
long_utilization = 0.0002e8 / (1000e8 / 100) = 2e4 / 1e9 = 0.00002[round down => 0]
short_utilization = 0 / 1e12 /100 = 0
borrowing_long_fee = 100 * (0) = 0 [min fee = 1] ==> 10
borrowing_short_fee = 100 * (0) = 0 [min fee = 1] ==> 10
funding_fee_long = 10 * (0) = 0
funding_fee_short = 10 * 0 = 0
1000 block passed
funding_paid = 5e6 * 1000 * 0 / 1_000_000_000 = 0
borrowing_paid = (5e6) * (1000 * 10) / 1_000_000_000 = 50
** long_utilization and short_utilization are zero until base_reserve / 100 >= base_interest and quote_reserve / 100 >= quote_interest
Internal pre-conditions
pool status:
"base_reserve" : 1000e8 BTC
"quote_reserve" : 1,000,000e6 USDT
Code Snippet
https://github.com/sherlock-audit/2024-08-velar-artha/blob/main/gl-sherlock/contracts/params.vy#L63
Impact
Funding fee always is lower than what it really should be
PoC
Place below test in tests/test_positions.py and run with
pytest -k test_precision_loss -s
Mitigation
1-scale up long_utilzation and short_utilzation
2-set min value for long_utilzation and short_utilzation
The text was updated successfully, but these errors were encountered: