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

calc_token_amount is incorrect in stableswapng views implementation #44

Closed
1 of 4 tasks
bout3fiddy opened this issue Mar 13, 2024 · 0 comments · Fixed by #45
Closed
1 of 4 tasks

calc_token_amount is incorrect in stableswapng views implementation #44

bout3fiddy opened this issue Mar 13, 2024 · 0 comments · Fixed by #45
Assignees
Labels
bug Something isn't working

Comments

@bout3fiddy
Copy link
Collaborator

bout3fiddy commented Mar 13, 2024

Description

To replicate:

boa.env.fork("https://eth.llamarpc.com")
pool = boa.load_partial('./contracts/CurveStableSwapNG.vy').at('0x383E6b4437b59fff47B619CBA855CA29342A8559')
views = boa.load_partial('./contracts/CurveStableSwapNGViews.vy').at('0xe0B15824862f3222fdFeD99FeBD0f7e0EC26E1FA')
coin_0 = boa.load_partial('./contracts/ERC20Mock.vy').at(pool.coins(0))
amounts = [500000000, 0]

calculated_output = pool.calc_token_amount(amounts, True)

with boa.env.prank('0x7E4B4DC22111B84594d9b7707A8DCFFd793D477A'), boa.env.anchor():
    coin_0.approve(pool.address, 2**256-1)
    actual_output = pool.add_liquidity(amounts, 0)

calculated_output is 499505730367411984853 whereas actual_output is 499594877358336511794.

Fix

The issue is caused by the calc_token_amount in the view contract:

        for i in range(MAX_COINS):
            if i == N_COINS:
                break

            ideal_balance: uint256 = D1 * old_balances[i] / D0
            difference: uint256 = 0
            new_balance: uint256 = new_balances[i]
            if ideal_balance > new_balance:
                difference = ideal_balance - new_balance
            else:
                difference = new_balance - ideal_balance

            xs = old_balances[i] + new_balance
            _dynamic_fee_i = self._dynamic_fee(xs, ys, base_fee, fee_multiplier)
            new_balances[i] -= _dynamic_fee_i * difference / FEE_DENOMINATOR

which should be:

...
            xs = rates[i] * (old_balances[i] + new_balance) / PRECISION
...

To confirm this fix, please refer to the shared notebook here.

Task list

  • Fix line on stableswapngviews contract
  • deploy across all chains
  • create dao vote for ethereum implementation
  • execute when proposal passes
@bout3fiddy bout3fiddy self-assigned this Mar 13, 2024
@bout3fiddy bout3fiddy added the bug Something isn't working label Mar 13, 2024
@AlbertoCentonze AlbertoCentonze linked a pull request Mar 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants