Skip to content

Commit

Permalink
fix test MAX_A lim and fix order of unpacking in amm impl constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed Dec 15, 2023
1 parent 2b46e41 commit 4cc4b74
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 51 deletions.
24 changes: 12 additions & 12 deletions contracts/main/CurveTwocryptoFactory.vy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pragma version 0.3.10
# pragma optimize gas
# pragma optimize codesize
"""
@title CurveTwocryptoFactory
@author Curve.Fi
Expand Down Expand Up @@ -186,17 +186,17 @@ def deploy_pool(
# pool is an ERC20 implementation
_salt: bytes32 = block.prevhash
pool: address = create_from_blueprint(
pool_implementation,
_name,
_symbol,
_coins,
_math_implementation,
_salt,
precisions,
packed_A_gamma,
packed_fee_params,
packed_rebalancing_params,
initial_price,
pool_implementation, # blueprint: address
_name, # String[64]
_symbol, # String[32]
_coins, # address[N_COINS]
_math_implementation, # address
_salt, # bytes32
precisions, # uint256[N_COINS]
packed_A_gamma, # uint256
packed_fee_params, # uint256
packed_rebalancing_params, # uint256
initial_price, # uint256
code_offset=3
)

Expand Down
14 changes: 7 additions & 7 deletions contracts/main/CurveTwocryptoOptimized.vy
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ event ClaimAdminFee:

N_COINS: constant(uint256) = 2
PRECISION: constant(uint256) = 10**18 # <------- The precision to convert to.
A_MULTIPLIER: constant(uint256) = 10000
PRECISIONS: immutable(uint256[N_COINS])

MATH: public(immutable(Math))
Expand Down Expand Up @@ -178,8 +177,9 @@ admin_lp_virtual_balance: uint256
MIN_RAMP_TIME: constant(uint256) = 86400
MIN_ADMIN_FEE_CLAIM_INTERVAL: constant(uint256) = 86400

A_MULTIPLIER: constant(uint256) = 10000
MIN_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER / 10
MAX_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER * 100000
MAX_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER * 1000
MAX_A_CHANGE: constant(uint256) = 10
MIN_GAMMA: constant(uint256) = 10**10
MAX_GAMMA: constant(uint256) = 5 * 10**16
Expand Down Expand Up @@ -235,13 +235,13 @@ def __init__(
PRECISIONS = precisions # <------------------------ Precisions of coins.

# --------------- Validate A and gamma parameters here and not in factory.
A_gamma: uint256[2] = self._unpack_2(packed_A_gamma)
gamma_A: uint256[2] = self._unpack_2(packed_A_gamma) # gamma is at idx 0.

assert A_gamma[0] > MIN_A-1
assert A_gamma[0] < MAX_A+1
assert gamma_A[0] > MIN_GAMMA-1
assert gamma_A[0] < MAX_GAMMA+1

assert A_gamma[1] > MIN_GAMMA-1
assert A_gamma[1] < MAX_GAMMA+1
assert gamma_A[1] > MIN_A-1
assert gamma_A[1] < MAX_A+1

self.initial_A_gamma = packed_A_gamma
self.future_A_gamma = packed_A_gamma
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pdbpp
hypothesis>=6.68.1

# vyper and dev framework:
git+https://github.com/vyperlang/titanoboa@b5e9fb96d1424ed5cc5a6af03391d885439c83e5
git+https://github.com/vyperlang/titanoboa@6d1faa3ba5b6a5c7823a7313b74b73dd551aebe4
vyper>=0.3.10
26 changes: 13 additions & 13 deletions tests/fixtures/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ def swap(

with boa.env.prank(deployer):
swap = twocrypto_factory.deploy_pool(
"Curve.fi USD<>WETH",
"USD<>WETH",
[coin.address for coin in coins],
0, # <-------- 0th implementation index
params["A"],
params["gamma"],
params["mid_fee"],
params["out_fee"],
params["fee_gamma"],
params["allowed_extra_profit"],
params["adjustment_step"],
params["ma_time"], # <--- no admin_fee needed
params["initial_prices"][1],
"Curve.fi USD<>WETH", # _name: String[64]
"USD<>WETH", # _symbol: String[32]
[coin.address for coin in coins], # _coins: address[N_COINS]
0, # implementation_id: uint256
params["A"], # A: uint256
params["gamma"], # gamma: uint256
params["mid_fee"], # mid_fee: uint256
params["out_fee"], # out_fee: uint256
params["fee_gamma"], # fee_gamma: uint256
params["allowed_extra_profit"], # allowed_extra_profit: uint256
params["adjustment_step"], # adjustment_step: uint256
params["ma_time"], # ma_exp_time: uint256
params["initial_prices"][1], # initial_price: uint256
)

return amm_interface.at(swap)
Expand Down
18 changes: 11 additions & 7 deletions tests/unitary/math/test_newton_D.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
import sys
import time
from decimal import Decimal

import pytest
from boa.vyper.contract import BoaError
from hypothesis import given, settings
from hypothesis import strategies as st
import tests.utils.simulation_int_many as sim

import tests.utils.simulation_int_many as sim

# Uncomment to be able to print when parallelized
# sys.stdout = sys.stderr


def inv_target_decimal_n2(A, gamma, x, D):
N = len(x)

Expand Down Expand Up @@ -41,7 +43,7 @@ def inv_target_decimal_n2(A, gamma, x, D):

A_MUL = 10000
MIN_A = int(N_COINS**N_COINS * A_MUL / 10)
MAX_A = int(N_COINS**N_COINS * A_MUL * 100000)
MAX_A = int(N_COINS**N_COINS * A_MUL * 1000)

# gamma from 1e-8 up to 0.05
MIN_GAMMA = 10**10
Expand All @@ -54,9 +56,10 @@ def inv_target_decimal_n2(A, gamma, x, D):
pytest.actually_tested = 0
pytest.t_start = time.time()


@pytest.mark.parametrize(
"_tmp", range(N_CASES)
) # Create N_CASES independent test instances.
"_tmp", range(N_CASES)
) # Create N_CASES independent test instances.
@given(
A=st.integers(min_value=MIN_A, max_value=MAX_A),
D=st.integers(
Expand Down Expand Up @@ -142,7 +145,9 @@ def _test_newton_D(

pytest.progress += 1
if pytest.progress % 1000 == 0 and pytest.actually_tested != 0:
print(f"{pytest.progress} | {pytest.actually_tested} cases processed in {time.time()-pytest.t_start:.1f} seconds.")
print(
f"{pytest.progress} | {pytest.actually_tested} cases processed in {time.time()-pytest.t_start:.1f} seconds."
)
X = [D * xD // 10**18, D * yD // 10**18]

result_get_y = 0
Expand Down Expand Up @@ -199,7 +204,7 @@ def _test_newton_D(
# f.write(case)
# with open("log/newton_D_fail_trace.txt", "a") as f:
# f.write(str(e))
return
return

A_dec = Decimal(A) / 10000 / 4

Expand All @@ -213,4 +218,3 @@ def calculate_D_polynome(d):

# with open("log/newton_D_pass.txt", "a") as f:
# f.write(case)

6 changes: 4 additions & 2 deletions tests/unitary/math/test_newton_D_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys
from decimal import Decimal

from boa.vyper.contract import BoaError
import pytest
from boa.vyper.contract import BoaError
from hypothesis import given, settings
from hypothesis import strategies as st

Expand Down Expand Up @@ -52,9 +52,11 @@ def inv_target_decimal_n2(A, gamma, x, D):
MAX_XD = 10**19

pytest.cases = 0


@pytest.mark.parametrize(
"_tmp", range(N_CASES)
) # Create N_CASES independent test instances.
) # Create N_CASES independent test instances.
@given(
A=st.integers(min_value=MIN_A, max_value=MAX_A),
D=st.integers(
Expand Down
10 changes: 6 additions & 4 deletions tests/unitary/math/test_packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

@given(val=strategy("uint256[3]", max_value=10**18))
@settings(max_examples=10000, deadline=None)
def test_pack_unpack_three_integers(swap, twocrypto_factory, val):

def test_pack_unpack_three_integers(
swap, math_contract, twocrypto_factory, val
):
for contract in [swap, twocrypto_factory]:
packed = contract.internal._pack_3(val)
unpacked = swap.internal._unpack_3(packed) # swap unpacks
Expand All @@ -19,5 +20,6 @@ def test_pack_unpack_2_integers(swap, val):

packed = swap.internal._pack_2(val[0], val[1])
unpacked = swap.internal._unpack_2(packed) # swap unpacks
for i in range(2):
assert unpacked[i] == val[i]

assert unpacked[0] == val[0]
assert unpacked[1] == val[1]
1 change: 0 additions & 1 deletion tests/unitary/pool/test_a_gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


def test_A_gamma(swap, params):

A = swap.A()
gamma = swap.gamma()

Expand Down
5 changes: 1 addition & 4 deletions tests/utils/simulation_int_many.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ def y(self, x, i, j):
xp = self.xp()
xp[i] = x * self.p[i] // 10**18
yp = solve_x(self.A, self.gamma, xp, self.D(), j)
try:
return yp * 10**18 // self.p[j]
except:
breakpoint()
return yp * 10**18 // self.p[j]


def get_data(fname):
Expand Down

0 comments on commit 4cc4b74

Please sign in to comment.