Skip to content

Commit

Permalink
Add get_y similar to stableswap
Browse files Browse the repository at this point in the history
  • Loading branch information
chanhosuh committed Aug 2, 2023
1 parent 4ec41ed commit dc4bbe1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
35 changes: 35 additions & 0 deletions curvesim/pool/cryptoswap/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,41 @@ def get_dy(self, i: int, j: int, dx: int) -> int:

return dy

def get_y(self, i, j, x, xp):
r"""
Calculate x[j] if one makes x[i] = x.
Parameters
----------
i: int
index of coin; usually the "in"-token
j: int
index of coin; usually the "out"-token
x: int
balance of i-th coin in units of D
xp: list of int
coin balances in units of D
Returns
-------
int
The balance of the j-th coin, in units of D, for the other
coin balances given.
Note
----
This is a "view" function; it doesn't change the state of the pool.
"""
A: int = self.A
gamma: int = self.gamma
D: int = _newton_D(A, gamma, xp)

xp = xp.copy()
xp[i] = x

y, _ = _get_y(A, gamma, xp, D, j)
return y

def _fee(self, xp: List[int]) -> int:
"""
f = fee_gamma / (fee_gamma + (1 - K))
Expand Down
30 changes: 30 additions & 0 deletions test/unit/test_tricrypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,36 @@ def test__get_y(vyper_tricrypto, A, gamma, x0, x1, x2, pair, dx_perc):
assert y_out[1] == expected_y_out[1]


def test_get_y(vyper_tricrypto):
"""
Test `get_y`.
Note `_get_y`, which is the pure version, is already tested
thoroughly in its own test against the vyper.
This test is a sanity check to make sure we pass values in correctly
to the underlying `_get_y` implementation.
"""
pool = initialize_pool(vyper_tricrypto)

xp = pool._xp()
A = pool.A
gamma = pool.gamma
D = _newton_D(A, gamma, xp)

i = 0
j = 1

# `get_y` will set i-th balance to `x`
x = xp[i] * 102 // 100
y = pool.get_y(i, j, x, xp)

xp[i] = x
expected_y, _ = _get_y(A, gamma, xp, D, j)

assert y == expected_y


@given(st.integers(min_value=-42139678854452767551, max_value=135305999368893231589))
@settings(
suppress_health_check=[HealthCheck.function_scoped_fixture],
Expand Down

0 comments on commit dc4bbe1

Please sign in to comment.