Skip to content

Commit

Permalink
fix: precisions is not longer stored packed
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed Aug 28, 2023
1 parent e6d616c commit 2eea2a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
46 changes: 17 additions & 29 deletions contracts/main/CurveTricryptoOptimizedWETH.vy
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ WETH20: public(immutable(address))
N_COINS: constant(uint256) = 3
PRECISION: constant(uint256) = 10**18 # <------- The precision to convert to.
A_MULTIPLIER: constant(uint256) = 10000
packed_precisions: immutable(uint256)
PRECISIONS: immutable(uint256[N_COINS])

MATH: public(immutable(Math))
coins: public(immutable(address[N_COINS]))
Expand Down Expand Up @@ -255,8 +255,8 @@ def __init__(
symbol = _symbol
coins = _coins

packed_precisions = __packed_precisions # <------- Precisions of coins
# are calculated as 10**(18 - coin.decimals()).
PRECISIONS = self._unpack(__packed_precisions) # <-- Precisions of coins
# are calculated as 10**(18 - coin.decimals()).

self.initial_A_gamma = packed_A_gamma # <------------------- A and gamma.
self.future_A_gamma = packed_A_gamma
Expand Down Expand Up @@ -464,7 +464,6 @@ def add_liquidity(

# --------------------- Get prices, balances -----------------------------

precisions: uint256[N_COINS] = self._unpack(packed_precisions)
packed_price_scale: uint256 = self.price_scale_packed
price_scale: uint256[N_COINS-1] = self._unpack_prices(packed_price_scale)

Expand All @@ -476,12 +475,12 @@ def add_liquidity(

xx = xp

xp[0] *= precisions[0]
xp_old[0] *= precisions[0]
xp[0] *= PRECISIONS[0]
xp_old[0] *= PRECISIONS[0]
for i in range(1, N_COINS):
xp[i] = unsafe_div(xp[i] * price_scale[i-1] * precisions[i], PRECISION)
xp[i] = unsafe_div(xp[i] * price_scale[i-1] * PRECISIONS[i], PRECISION)
xp_old[i] = unsafe_div(
xp_old[i] * unsafe_mul(price_scale[i-1], precisions[i]),
xp_old[i] * unsafe_mul(price_scale[i-1], PRECISIONS[i]),
PRECISION
)

Expand Down Expand Up @@ -774,7 +773,6 @@ def _exchange(

A_gamma: uint256[2] = self._A_gamma()
xp: uint256[N_COINS] = self.balances
precisions: uint256[N_COINS] = self._unpack(packed_precisions)
dy: uint256 = 0

y: uint256 = xp[j] # <----------------- if j > N_COINS, this will revert.
Expand All @@ -786,14 +784,14 @@ def _exchange(
packed_price_scale
)

xp[0] *= precisions[0]
xp[0] *= PRECISIONS[0]
for k in range(1, N_COINS):
xp[k] = unsafe_div(
xp[k] * price_scale[k - 1] * precisions[k],
xp[k] * price_scale[k - 1] * PRECISIONS[k],
PRECISION
) # <-------- Safu to do unsafe_div here since PRECISION is not zero.

prec_i: uint256 = precisions[i]
prec_i: uint256 = PRECISIONS[i]

# ----------- Update invariant if A, gamma are undergoing ramps ---------

Expand All @@ -813,7 +811,7 @@ def _exchange(
# ----------------------- Calculate dy and fees --------------------------

D: uint256 = self.D
prec_j: uint256 = precisions[j]
prec_j: uint256 = PRECISIONS[j]
y_out: uint256[2] = MATH.get_y(A_gamma[0], A_gamma[1], xp, D, j)
dy = xp[j] - y_out[0]
xp[j] -= dy
Expand Down Expand Up @@ -1133,7 +1131,6 @@ def _claim_admin_fees():

vprice: uint256 = self.virtual_price
packed_price_scale: uint256 = self.price_scale_packed
precisions: uint256[N_COINS] = self._unpack(packed_precisions)
fee_receiver: address = factory.fee_receiver()
balances: uint256[N_COINS] = self.balances

Expand Down Expand Up @@ -1214,14 +1211,13 @@ def _claim_admin_fees():
def xp(
balances: uint256[N_COINS],
price_scale_packed: uint256,
precisions: uint256[N_COINS]
) -> uint256[N_COINS]:

result: uint256[N_COINS] = balances
result[0] *= precisions[0]
result[0] *= PRECISIONS[0]
packed_prices: uint256 = price_scale_packed
for i in range(1, N_COINS):
p: uint256 = (packed_prices & PRICE_MASK) * precisions[i]
p: uint256 = (packed_prices & PRICE_MASK) * PRECISIONS[i]
result[i] = result[i] * p / PRECISION
packed_prices = packed_prices >> PRICE_SIZE

Expand Down Expand Up @@ -1327,13 +1323,12 @@ def _calc_withdraw_one_coin(
assert i < N_COINS # dev: coin out of range

xx: uint256[N_COINS] = self.balances
precisions: uint256[N_COINS] = self._unpack(packed_precisions)
xp: uint256[N_COINS] = precisions
xp: uint256[N_COINS] = PRECISIONS
D0: uint256 = 0

# -------------------------- Calculate D0 and xp -------------------------

price_scale_i: uint256 = PRECISION * precisions[0]
price_scale_i: uint256 = PRECISION * PRECISIONS[0]
packed_prices: uint256 = self.price_scale_packed
xp[0] *= xx[0]
for k in range(1, N_COINS):
Expand Down Expand Up @@ -1801,14 +1796,7 @@ def fee() -> uint256:
removed.
@return uint256 fee bps.
"""
precisions: uint256[N_COINS] = self._unpack(packed_precisions)
return self._fee(
self.xp(
self.balances,
self.price_scale_packed,
precisions
)
)
return self._fee(self.xp(self.balances, self.price_scale_packed))


@view
Expand Down Expand Up @@ -1932,7 +1920,7 @@ def precisions() -> uint256[N_COINS]: # <-------------- For by view contract.
@notice Returns the precisions of each coin in the pool.
@return uint256[3] precisions of coins.
"""
return self._unpack(packed_precisions)
return PRECISIONS


@external
Expand Down
1 change: 0 additions & 1 deletion tests/boa/unitary/math/test_get_p.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def _get_dydx_vyper(swap, i, j, price_calc):
xp = swap.internal.xp(
swap._storage.balances.get(),
swap._storage.price_scale_packed.get(),
swap.precisions(),
)

for k in range(3):
Expand Down
4 changes: 1 addition & 3 deletions tests/boa/unitary/math/test_get_p_expt.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ def _get_prices_vyper(swap, price_calc):
for i in range(3):
balances.append(swap.balances(i))

xp = swap.internal.xp(
balances, swap._storage.price_scale_packed.get(), swap.precisions()
)
xp = swap.internal.xp(balances, swap._storage.price_scale_packed.get())

D = swap.D()

Expand Down
1 change: 0 additions & 1 deletion tests/boa/unitary/pool/test_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def get_D_oracle_input(swap, math_contract):
xp = swap.internal.xp(
swap._storage.balances.get(),
swap._storage.price_scale_packed.get(),
swap.internal._unpack(swap._immutables.packed_precisions),
)
D = math_contract.newton_D(A, gamma, xp)
xcp = math_contract.geometric_mean(xp)
Expand Down

0 comments on commit 2eea2a5

Please sign in to comment.