Skip to content

Commit

Permalink
fix: get_p test
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed Dec 23, 2023
1 parent 50b381b commit b8601ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 53 deletions.
43 changes: 19 additions & 24 deletions tests/unitary/math/test_get_p.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from math import log

import boa
import pytest
from boa.test import strategy
from hypothesis import given, settings

from tests.fixtures.pool import INITIAL_PRICES
from tests.utils import approx
from tests.utils.tokens import mint_for_testing

SETTINGS = {"max_examples": 1000, "deadline": None}
SETTINGS = {"max_examples": 20, "deadline": None}


# flake8: noqa: E501
Expand Down Expand Up @@ -59,28 +58,26 @@ def _get_prices_vyper(swap, price_calc):

def _get_prices_numeric_nofee(swap, views, i):

if i == 0: # we are selling j

dx = 10**16 # 0.01 USD
dy = views.internal._get_dy_nofee(0, 1, dx, swap)[0]
price = dx * 10**18 // dy[0]

else: # we are buying j so numba should go up
if i == 0: # token at index 1 is being pupmed.

dx = int(0.01 * 10**36 // INITIAL_PRICES[1])
dolla_out = views.internal._get_dy_nofee(1, 0, dx, swap)[0]
price = dolla_out * 10**18 // dx

else: # token at index 1 is being dupmed.

dx = 10**16 # 0.01 USD
dy = views.internal._get_dy_nofee(0, 1, dx, swap)[0]
price = dx * 10**18 // dy

return price


# ----- Tests -----


@given(
dollar_amount=strategy(
"decimal", min_value=10**-5, max_value=5 * 10**8
),
dollar_amount=strategy("decimal", min_value=1e-5, max_value=5e8),
)
@settings(**SETTINGS)
@pytest.mark.parametrize("i", [0, 1])
Expand All @@ -97,22 +94,20 @@ def test_dxdy_similar(
previous_p = yuge_swap.price_scale()
j = 1 - i

dx = int(dollar_amount * 10**36 // INITIAL_PRICES[i])
mint_for_testing(coins[i], user, dx)
out = yuge_swap.exchange(i, j, dx, 0, sender=user)
amount_in = int(dollar_amount * 10**36 // INITIAL_PRICES[i])
mint_for_testing(coins[i], user, amount_in)
yuge_swap.exchange(i, j, amount_in, 0, sender=user)

dxdy_vyper = _get_prices_vyper(yuge_swap, dydx_safemath)
dxdy_swap = yuge_swap.last_prices() # <-- we check unsafe impl here.
dxdy_numeric_nofee = _get_prices_numeric_nofee(
yuge_swap, views_contract, i
)
dxdy_numeric = _get_prices_numeric_nofee(yuge_swap, views_contract, i)

if i == 0: # j is being pupmed
if i == 0: # token at index 1 is being pupmed, so last_price should go up
assert dxdy_swap > previous_p
assert dxdy_numeric_nofee > previous_p
else: # j is being dupmed
assert dxdy_numeric > previous_p
else: # token at index 1 is being dupmed, so last_price should go down
assert dxdy_swap < previous_p
assert dxdy_numeric_nofee < previous_p
assert dxdy_numeric < previous_p

assert dxdy_vyper == dxdy_swap
assert abs(log(dxdy_vyper / dxdy_numeric_nofee)) < 1e-5
assert approx(dxdy_vyper, dxdy_numeric, 1e-5)
23 changes: 5 additions & 18 deletions tests/unitary/pool/stateful/test_simulate.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
from math import log

import boa
from boa.test import strategy
from hypothesis.stateful import invariant, rule, run_state_machine_as_test

from tests.unitary.pool.stateful.stateful_base import StatefulBase
from tests.utils import approx
from tests.utils import simulation_int_many as sim
from tests.utils.tokens import mint_for_testing

MAX_SAMPLES = 20
STEP_COUNT = 100


def approx(x1, x2, precision):
return abs(log(x1 / x2)) <= precision


def logdiff(x1, x2):
return abs(log(x1 / x2))


class StatefulSimulation(StatefulBase):
exchange_amount_in = strategy(
"uint256", min_value=10**17, max_value=10**5 * 10**18
Expand Down Expand Up @@ -87,14 +78,11 @@ def exchange(self, exchange_amount_in, exchange_i, user):
self.trader.tweak_price(boa.env.vm.state.timestamp)

# exchange checks:
out_logdiff = logdiff(self.swap_out, dy_trader)
price_oracle_logdiff = logdiff(
self.swap.price_oracle(), self.trader.price_oracle[1]
assert approx(self.swap_out, dy_trader, 1e-3)
assert approx(
self.swap.price_oracle(), self.trader.price_oracle[1], 1e-3
)

assert out_logdiff <= 1e-3
assert price_oracle_logdiff <= 1e-3

boa.env.time_travel(12)

@invariant()
Expand All @@ -108,9 +96,8 @@ def simulator(self):

price_scale = self.swap.price_scale()
price_trader = self.trader.curve.p[1]
price_scale_logdiff = logdiff(price_scale, price_trader)
try:
assert price_scale_logdiff <= 1e-3
assert approx(price_scale, price_trader, 1e-3)
except Exception:
if self.check_limits([0, 0, 0]):
assert False
Expand Down
7 changes: 1 addition & 6 deletions tests/unitary/pool/test_deposit_withdraw.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import math

import boa
import pytest
from boa.test import strategy
from hypothesis import given, settings

from tests.fixtures.pool import INITIAL_PRICES
from tests.utils import approx
from tests.utils import simulation_int_many as sim
from tests.utils.tokens import mint_for_testing

SETTINGS = {"max_examples": 100, "deadline": None}


def approx(x1, x2, precision):
return abs(math.log(x1 / x2)) <= precision


def assert_string_contains(string, substrings):
assert any(substring in string for substring in substrings)

Expand Down
7 changes: 2 additions & 5 deletions tests/unitary/pool/test_oracles.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
from math import exp, log, log2, sqrt
from math import exp, log2, sqrt

import boa
import pytest
from boa.test import strategy
from hypothesis import given, settings

from tests.fixtures.pool import INITIAL_PRICES
from tests.utils import approx
from tests.utils.tokens import mint_for_testing

SETTINGS = {"max_examples": 1000, "deadline": None}


def approx(x1, x2, precision):
return abs(log(x1 / x2)) <= precision


def norm(price_oracle, price_scale):
norm = 0
ratio = price_oracle * 10**18 / price_scale
Expand Down
5 changes: 5 additions & 0 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import math


def approx(x1, x2, precision):
return abs(math.log(x1 / x2)) <= precision

0 comments on commit b8601ae

Please sign in to comment.