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

pashap9990 - Lp amount will be minted lower than what it really expected in mint function #104

Closed
sherlock-admin3 opened this issue Sep 9, 2024 · 5 comments
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Escalation Resolved This issue's escalations have been approved/rejected Medium A Medium severity issue. Reward A payout will be made for this issue

Comments

@sherlock-admin3
Copy link
Contributor

sherlock-admin3 commented Sep 9, 2024

pashap9990

Medium

Lp amount will be minted lower than what it really expected in mint function

Summary

LPs may receive fewer tokens than expected when minting due to fluctuating pool reserves. Without specifying a minimum LP amount, this can lead to unexpected fund losses for LPs. Adding a min_lp_amount parameter to the Api::mint function can prevent this issue

Internal pre-conditions

Consider change config in tests/conftest.py
I do it for better understanding,Fee doesn't important in this issue

PARAMS = {
  'MIN_FEE'               : 0,
  'MAX_FEE'               : 0,

  'PROTOCOL_FEE'          :1000
    ...
}

Code Snippet

https://github.com/sherlock-audit/2024-08-velar-artha/blob/main/gl-sherlock/contracts/api.vy#L75

Impact

Lp amount will be minted lower than what it really expected

PoC

Textual PoC:
we assume protocol fee is zero in this example
1-Alice opens long position when price is $2
2-Joe opens a short position when price is $2
3-Price goes down to $1
4-Bob calls Pools::calc_mint and he realize if he provide 1000 VEL to pool,he gets 1000 LP
5-Bob send his tx to network
6-liquidator bot liquids Alice's position
7-Bob's tx will be executed and Bob get 952 lp instead of 1000
8-Joe closes his position and gets his profit,hence pool's reserve will be deducted
9-Bob burns his LPs token,and he gets 772 VEL instead of 1000 VEL
Coded PoC:
Place below test in tests/test_positions.py and run pytest -k test_get_less_lp_amt_than_expected - s

def test_get_less_lp_amt_than_expected(setup, VEL, STX, lp_provider, LP, pools, open, long, close, burn, liquidator, liquidate, mint, short):
    setup()
    #Alice opens position
    open(VEL, STX, True, d(1000), 5, price=d(2), sender=long)
    #Joe opens short position
    open(VEL, STX, False, d(1000), 5, price=d(2), sender=short)

    #Bob calls calc_mint 
    assert pools.calc_mint(1, d(1000), 0, d(20_000), ctx(d(1))) == d(1000)

    #Alice will be liquidated
    liquidate(VEL, STX, 1, price = d(1), sender = liquidator)

    #Bob's tx will be executed
    mint(VEL, STX, LP, d(1000), 0, price=d(1), sender=lp_provider)    

    assert LP.totalSupply() == 20952426306#its mean,Bob get 48 lp token lower than expected

    #Joe closes his position
    close(VEL, STX, 2, price=d(1), sender=short)


    vel_bef = VEL.balanceOf(lp_provider)
    #Bob burn his lp tokens
    burn(VEL, STX, LP, 952426306, price=d(1), sender=lp_provider)
    
    vel_after = VEL.balanceOf(lp_provider)

    print("vel_diff:", (vel_after - vel_bef) / 1e6)#Bob lost $228 

Mitigation

Api::mint should have another parameter like min_lp_amount which can be checked when the lp token wants to be mint

Duplicate of #74

@github-actions github-actions bot added the Excluded Excluded by the judge without consulting the protocol or the senior label Sep 11, 2024
@sherlock-admin3 sherlock-admin3 changed the title Magnificent Pewter Stork - Lp amount will be minted lower than what it really expected in mint function pashap9990 - Lp amount will be minted lower than what it really expected in mint function Sep 11, 2024
@sherlock-admin3 sherlock-admin3 added the Non-Reward This issue will not receive a payout label Sep 11, 2024
@rickkk137
Copy link

rickkk137 commented Sep 13, 2024

Escalate
LP token price directly compute based pool reserve and total supply lp token and the issue clearly states received amount can be less than expected amount and in Coded PoC liquidity provider expected $1000 but in result get $772

loss = $228[22%]

Causes a loss of funds but requires certain external conditions or specific states, or a loss is highly constrained. The loss of the affected party must exceed 0.01% and 10 USD

@sherlock-admin3
Copy link
Contributor Author

sherlock-admin3 commented Sep 13, 2024

Escalate

Slippage related issues showing a definite loss of funds with a detailed explanation for the same can be considered valid high

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.

@WangSecurity
Copy link

After additionally considering this issue, indeed, the slippage is checked only on the oracle price and doesn't catch the pool reserves, leading to the situation where regardless of user-set slippage, they can get fewer tokens after mint. This issue exists in both mint and burn functions. The root cause is the same - insufficient slippage protection and the fix would be the same, even though they should be fixed separately. Hence, this issue will be duplicated with #74, planning to accept the escalation.

@WangSecurity WangSecurity added Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A Medium severity issue. and removed Excluded Excluded by the judge without consulting the protocol or the senior labels Oct 2, 2024
@sherlock-admin2 sherlock-admin2 added Reward A payout will be made for this issue and removed Non-Reward This issue will not receive a payout labels Oct 2, 2024
@WangSecurity
Copy link

Result:
Medium
Duplicate of #74

@sherlock-admin2 sherlock-admin2 removed the Escalated This issue contains a pending escalation label Oct 2, 2024
@sherlock-admin3 sherlock-admin3 added the Escalation Resolved This issue's escalations have been approved/rejected label Oct 2, 2024
@sherlock-admin4
Copy link
Contributor

Escalations have been resolved successfully!

Escalation status:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Escalation Resolved This issue's escalations have been approved/rejected Medium A Medium severity issue. Reward A payout will be made for this issue
Projects
None yet
Development

No branches or pull requests

5 participants