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

chore: make calc_withdraw_one_coin simulation consistent with _calc_withdraw_one_coin #86

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/pools/common/unitary/test_remove_liquidity_one_coin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import brownie
import pytest
from simulation import Curve

pytestmark = [
pytest.mark.skip_pool("busd", "compound", "pax", "susd", "usdt", "y"),
Expand Down Expand Up @@ -81,3 +82,13 @@ def test_below_zero(alice, swap):
def test_above_n_coins(alice, swap, wrapped_coins, n_coins):
with brownie.reverts():
swap.remove_liquidity_one_coin(1, n_coins, 0, {'from': alice})

@pytest.mark.itercoins("idx")
def test_against_simulation(alice, swap, n_coins, pool_token, idx):
amount = pool_token.balanceOf(alice)

balances = [swap.balances(i) for i in range(n_coins)]
curve_model = Curve(swap.A(), balances, n_coins, tokens=pool_token.totalSupply())
expected, _ = curve_model.calc_withdraw_one_coin(amount, idx)

assert swap.calc_withdraw_one_coin(amount, idx) == expected

Choose a reason for hiding this comment

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

thank you

25 changes: 18 additions & 7 deletions tests/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,24 @@ def remove_liquidity_imbalance(self, amounts):

def calc_withdraw_one_coin(self, token_amount, i):
xp = self.xp()
if self.fee:
fee = self.fee - self.fee * xp[i] // sum(xp) + 5 * 10 ** 5
else:
fee = 0
xp_reduced = list(xp)

Choose a reason for hiding this comment

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

thank you


D0 = self.D()
D1 = D0 - token_amount * D0 // self.tokens
dy = xp[i] - self.y_D(i, D1)

return dy - dy * fee // 10 ** 10
new_y = self.y_D(i, D1)

fee = self.fee * self.n // (4 * (self.n - 1))
for j in range(self.n):
dx_expected = 0
if j == i:
dx_expected = xp[j] * D1 // D0 - new_y
else:
dx_expected = xp[j] - xp[j] * D1 // D0
xp_reduced[j] -= fee * dx_expected // 10 ** 10

self.x = [x // (p // 10 ** 18) for x, p in zip(xp_reduced, self.p)]
dy = xp_reduced[i] - self.y_D(i, D1) - 1 # Withdraw less to account for rounding errors
self.x = [x // (p // 10 ** 18) for x, p in zip(xp, self.p)]
dy_0 = xp[i] - new_y

Choose a reason for hiding this comment

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

thank you


return dy, dy_0 - dy