Skip to content

Commit

Permalink
fix profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed May 27, 2024
1 parent 277dcc6 commit 285d31e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS], initial
if D == 0:
D = N_COINS * isqrt(unsafe_mul(x[0], x[1]))
else:
# initial_DD = isqrt(x[0] * x[1] * 4 / K0_prev * 10**18)
# initial_D = isqrt(x[0] * x[1] * 4 / K0_prev * 10**18)
# K0_prev is derived from from get_y
if S < D:
D = S
Expand Down
13 changes: 4 additions & 9 deletions contracts/experimental/initial_guess/CurveTwocryptoOptimized.vy
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,7 @@ def _exchange(
# ------ Tweak price_scale with good initial guess for newton_D ----------

# Get initial guess using: D = isqrt(x[0] * x[1] * 4 / K0_prev * 10**18)
initial_D: uint256 = isqrt(
unsafe_mul(
unsafe_div(unsafe_mul(unsafe_mul(4, xp[0]), xp[1]), y_out[1]),
10**18
)
)
initial_D: uint256 = isqrt(xp[0] * xp[1] * 4 / y_out[1] * 10**18)
price_scale = self.tweak_price(A_gamma, xp, 0, initial_D)

return [dy, fee, price_scale]
Expand Down Expand Up @@ -971,9 +966,9 @@ def tweak_price(

# ------------------------------------------ Update D with new xp.
D: uint256 = MATH.newton_D(
A_gamma[0],
A_gamma[1],
xp,
A_gamma[0],
A_gamma[1],
xp,
D_unadjusted
)

Expand Down
45 changes: 25 additions & 20 deletions tests/profiling/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import boa
import pytest

from hypothesis import assume
from tests.utils.tokens import mint_for_testing

from contracts.experimental.initial_guess import (
CurveCryptoMathOptimized2 as math_deployer_initial_guess,
)
from contracts.experimental.initial_guess import (
CurveTwocryptoOptimized as amm_deployer_initial_guess,
)

# compiling contracts
from contracts.main import CurveCryptoMathOptimized2 as math_deployer
from contracts.main import CurveCryptoViews2Optimized as view_deployer
from contracts.main import CurveTwocryptoFactory as factory_deployer

from contracts.main import CurveTwocryptoOptimized as amm_deployer
from contracts.main import CurveCryptoMathOptimized2 as math_deployer
from contracts.experimental.initial_guess import CurveTwocryptoOptimized as amm_deployer_initial_guess
from contracts.experimental.initial_guess import CurveCryptoMathOptimized2 as math_deployer_initial_guess
from tests.utils.tokens import mint_for_testing

# ---------------- addresses ----------------
address = boa.test.strategy("address")
Expand All @@ -32,10 +35,12 @@


def _deposit_initial_liquidity(pool, tokens):

# deposit:
user = boa.env.generate_address()
quantities = [10**6 * 10**36 // p for p in [10**18, params["price"]]] # $2M worth
quantities = [
10**6 * 10**36 // p for p in [10**18, params["price"]]
] # $2M worth

for coin, quantity in zip(tokens, quantities):
# mint coins for user:
Expand All @@ -49,21 +54,21 @@ def _deposit_initial_liquidity(pool, tokens):
# Very first deposit
with boa.env.prank(user):
pool.add_liquidity(quantities, 0)

return pool


@pytest.fixture(scope="module")
def tokens():
return [
boa.load("contracts/mocks/ERC20Mock.vy", "tkn_a", "tkn_a", 18),
boa.load("contracts/mocks/ERC20Mock.vy", "tkn_b", "tkn_b", 18)
]
boa.load("contracts/mocks/ERC20Mock.vy", "tkn_a", "tkn_a", 18),
boa.load("contracts/mocks/ERC20Mock.vy", "tkn_b", "tkn_b", 18),
]


@pytest.fixture(scope="module")
def factory_no_initial_guess():

_deployer = boa.env.generate_address()
_fee_receiver = boa.env.generate_address()
_owner = boa.env.generate_address()
Expand All @@ -80,16 +85,16 @@ def factory_no_initial_guess():
with boa.env.prank(_owner):
_factory.set_views_implementation(view_contract)
_factory.set_math_implementation(math_contract)

# set pool implementations:
_factory.set_pool_implementation(amm_implementation, 0)

return _factory


@pytest.fixture(scope="module")
def factory_initial_guess():

_deployer = boa.env.generate_address()
_fee_receiver = boa.env.generate_address()
_owner = boa.env.generate_address()
Expand All @@ -107,10 +112,10 @@ def factory_initial_guess():
with boa.env.prank(_owner):
_factory.set_views_implementation(view_contract)
_factory.set_math_implementation(math_contract)

# set pool implementations:
_factory.set_pool_implementation(amm_implementation, 0)

return _factory


Expand Down Expand Up @@ -165,6 +170,6 @@ def pool_initial_guess(factory_initial_guess, tokens):
@pytest.fixture(scope="module")
def pools(pool, pool_initial_guess):
return [
pool_initial_guess,
pool,
pool,
# pool_initial_guess,
]
22 changes: 13 additions & 9 deletions tests/profiling/test_boa_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@

from tests.utils.tokens import mint_for_testing


NUM_RUNS = 10
N_COINS = 2


def _choose_indices():
i = random.randint(0, N_COINS-1)
i = random.randint(0, N_COINS - 1)
j = 0 if i == 1 else 1
return i, j


@pytest.mark.profile
@pytest.mark.gas_profile
def test_profile_amms(pools, tokens):

user = boa.env.generate_address()

for pool in pools:

for coin in tokens:
mint_for_testing(coin, user, 10**50)
coin.approve(pool, 2**256 - 1, sender=user)
Expand All @@ -33,15 +32,20 @@ def test_profile_amms(pools, tokens):

# proportional deposit:
balances = [pool.balances(i) for i in range(N_COINS)]
amount_first_coin = random.uniform(0, 0.05) * 10**(18+random.randint(1, 3))
amounts = [int(amount_first_coin), int(amount_first_coin * 1e18 // pool.price_scale())]
amount_first_coin = random.uniform(0, 0.05) * 10 ** (
18 + random.randint(1, 3)
)
amounts = [
int(amount_first_coin),
int(amount_first_coin * 1e18 // pool.price_scale()),
]
pool.add_liquidity(amounts, 0)
boa.env.time_travel(random.randint(12, 600))

# deposit single token:
balances = [pool.balances(i) for i in range(N_COINS)]
c = random.uniform(0, 0.05)
i = random.randint(0, N_COINS-1)
i = random.randint(0, N_COINS - 1)
amounts = [0] * N_COINS
for j in range(N_COINS):
if i == j:
Expand All @@ -61,6 +65,6 @@ def test_profile_amms(pools, tokens):
boa.env.time_travel(random.randint(12, 600))

# withdraw in one coin:
i = random.randint(0, N_COINS-1)
i = random.randint(0, N_COINS - 1)
amount = int(pool.balanceOf(user) * 0.01)
pool.remove_liquidity_one_coin(amount, i, 0)

0 comments on commit 285d31e

Please sign in to comment.