Skip to content

Commit

Permalink
replace dev reasons with onchain reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoCentonze committed Jan 4, 2025
1 parent 369aade commit 4a1a2d3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 68 deletions.
34 changes: 17 additions & 17 deletions contracts/main/CurveCryptoMathOptimized2.vy
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i:
y: uint256 = D**2 / (x_j * N_COINS**2)
K0_i: uint256 = (10**18 * N_COINS) * x_j / D

assert (K0_i >= unsafe_div(10**36, lim_mul)) and (K0_i <= lim_mul) # dev: unsafe values x[i]
assert (K0_i >= unsafe_div(10**36, lim_mul)) and (K0_i <= lim_mul), "unsafe values x[i]"

convergence_limit: uint256 = max(max(x_j / 10**14, D / 10**14), 100)

Expand Down Expand Up @@ -210,16 +210,16 @@ def _newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i:
def newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256:

# Safety checks
assert ANN > MIN_A - 1 and ANN < MAX_A + 1 # dev: unsafe values A
assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1 # dev: unsafe values gamma
assert D > 10**17 - 1 and D < 10**15 * 10**18 + 1 # dev: unsafe values D
assert ANN > MIN_A - 1 and ANN < MAX_A + 1, "unsafe values A"
assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1, "unsafe values gamma"
assert D > 10**17 - 1 and D < 10**15 * 10**18 + 1, "unsafe values D"
lim_mul: uint256 = 100 * 10**18 # 100.0
if gamma > MAX_GAMMA_SMALL:
lim_mul = unsafe_div(unsafe_mul(lim_mul, MAX_GAMMA_SMALL), gamma) # smaller than 100.0

y: uint256 = self._newton_y(ANN, gamma, x, D, i, lim_mul)
frac: uint256 = y * 10**18 / D
assert (frac >= unsafe_div(10**36 / N_COINS, lim_mul)) and (frac <= unsafe_div(lim_mul, N_COINS)) # dev: unsafe value for y
assert (frac >= unsafe_div(10**36 / N_COINS, lim_mul)) and (frac <= unsafe_div(lim_mul, N_COINS)), "unsafe value for y"

return y

Expand All @@ -235,9 +235,9 @@ def get_y(
) -> uint256[2]:

# Safety checks
assert _ANN > MIN_A - 1 and _ANN < MAX_A + 1 # dev: unsafe values A
assert _gamma > MIN_GAMMA - 1 and _gamma < MAX_GAMMA + 1 # dev: unsafe values gamma
assert _D > 10**17 - 1 and _D < 10**15 * 10**18 + 1 # dev: unsafe values D
assert _ANN > MIN_A - 1 and _ANN < MAX_A + 1, "unsafe values A"
assert _gamma > MIN_GAMMA - 1 and _gamma < MAX_GAMMA + 1, "unsafe values gamma"
assert _D > 10**17 - 1 and _D < 10**15 * 10**18 + 1, "unsafe values D"
lim_mul: uint256 = 100 * 10**18 # 100.0
if _gamma > MAX_GAMMA_SMALL:
lim_mul = unsafe_div(unsafe_mul(lim_mul, MAX_GAMMA_SMALL), _gamma) # smaller than 100.0
Expand All @@ -254,7 +254,7 @@ def get_y(

# K0_i: int256 = (10**18 * N_COINS) * x_j / D
K0_i: int256 = unsafe_div(10**18 * N_COINS * x_j, D)
assert (K0_i >= unsafe_div(10**36, lim_mul_signed)) and (K0_i <= lim_mul_signed) # dev: unsafe values x[i]
assert (K0_i >= unsafe_div(10**36, lim_mul_signed)) and (K0_i <= lim_mul_signed), "unsafe values x[i]"

ann_gamma2: int256 = ANN * gamma2

Expand Down Expand Up @@ -366,7 +366,7 @@ def get_y(
y_out: uint256[2] = [convert(unsafe_div(unsafe_div(unsafe_mul(unsafe_div(D**2, x_j), root), 4), 10**18), uint256), convert(root, uint256)]

frac: uint256 = unsafe_div(y_out[0] * 10**18, _D)
assert (frac >= unsafe_div(10**36 / N_COINS, lim_mul)) and (frac <= unsafe_div(lim_mul, N_COINS)) # dev: unsafe value for y
assert (frac >= unsafe_div(10**36 / N_COINS, lim_mul)) and (frac <= unsafe_div(lim_mul, N_COINS)), "unsafe value for y"

return y_out

Expand All @@ -381,16 +381,16 @@ def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS], K0_prev
"""

# Safety checks
assert ANN > MIN_A - 1 and ANN < MAX_A + 1 # dev: unsafe values A
assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1 # dev: unsafe values gamma
assert ANN > MIN_A - 1 and ANN < MAX_A + 1, "unsafe values A"
assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1, "unsafe values gamma"

# Initial value of invariant D is that for constant-product invariant
x: uint256[N_COINS] = x_unsorted
if x[0] < x[1]:
x = [x_unsorted[1], x_unsorted[0]]

assert x[0] > 10**9 - 1 and x[0] < 10**15 * 10**18 + 1 # dev: unsafe values x[0]
assert unsafe_div(x[1] * 10**18, x[0]) > 10**14 - 1 # dev: unsafe values x[i] (input)
assert x[0] > 10**9 - 1 and x[0] < 10**15 * 10**18 + 1, "unsafe values x[0]"
assert unsafe_div(x[1] * 10**18, x[0]) > 10**14 - 1, "unsafe values x[i] (input)"

S: uint256 = unsafe_add(x[0], x[1]) # can unsafe add here because we checked x[0] bounds

Expand All @@ -408,7 +408,7 @@ def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS], K0_prev

for i in range(255):
D_prev: uint256 = D
assert D > 0
assert D > 0, "D==0"
# Unsafe division by D and D_prev is now safe

# K0: uint256 = 10**18
Expand Down Expand Up @@ -454,7 +454,7 @@ def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS], K0_prev

for _x in x:
frac: uint256 = _x * 10**18 / D
assert (frac > 10**16 / N_COINS - 1) and (frac < 10**20 / N_COINS + 1) # dev: unsafe values x[i]
assert (frac > 10**16 / N_COINS - 1) and (frac < 10**20 / N_COINS + 1), "unsafe values x[i]"
return D

raise "Did not converge"
Expand All @@ -473,7 +473,7 @@ def get_p(
@param _A_gamma Amplification coefficient and gamma.
"""

assert _D > 10**17 - 1 and _D < 10**15 * 10**18 + 1 # dev: unsafe D values
assert _D > 10**17 - 1 and _D < 10**15 * 10**18 + 1, "unsafe D values"

# K0 = P * N**N / D**N.
# K0 is dimensionless and has 10**36 precision:
Expand Down
4 changes: 2 additions & 2 deletions contracts/main/CurveCryptoViews2Optimized.vy
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def _calc_withdraw_one_coin(
) -> (uint256, uint256):

token_supply: uint256 = Curve(swap).totalSupply()
assert token_amount <= token_supply # dev: token amount more than supply
assert i < N_COINS # dev: coin out of range
assert token_amount <= token_supply, "token amount more than supply"
assert i < N_COINS, "coin out of range"

math: Math = Curve(swap).MATH()

Expand Down
26 changes: 13 additions & 13 deletions contracts/main/CurveTwocryptoFactory.vy
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def __init__():
@external
def initialise_ownership(_fee_receiver: address, _admin: address):

assert msg.sender == self.deployer
assert self.admin == empty(address)
assert msg.sender == self.deployer, "unauthorized"
assert self.admin == empty(address), "already initialized"

self.fee_receiver = _fee_receiver
self.admin = _admin
Expand Down Expand Up @@ -158,21 +158,21 @@ def deploy_pool(
assert pool_implementation != empty(address), "Pool implementation not set"
assert _math_implementation != empty(address), "Math implementation not set"

assert mid_fee < MAX_FEE-1 # mid_fee can be zero
assert out_fee >= mid_fee
assert out_fee < MAX_FEE-1
assert fee_gamma < 10**18+1
assert fee_gamma > 0
assert mid_fee < MAX_FEE-1, "mid>max" # mid_fee can be zero
assert out_fee >= mid_fee, "out<mid"
assert out_fee < MAX_FEE-1, "out>max"
assert fee_gamma < 10**18+1, "fee_gamma>max"
assert fee_gamma > 0, "fee_gamma==0"

assert allowed_extra_profit < 10**18+1
assert allowed_extra_profit < 10**18+1, "allowed_extra_profit>max"

assert adjustment_step < 10**18+1
assert adjustment_step > 0
assert adjustment_step < 10**18+1, "adjustment_step>max"
assert adjustment_step > 0, "adjustment_step==0"

assert ma_exp_time < 872542 # 7 * 24 * 60 * 60 / ln(2)
assert ma_exp_time > 86 # 60 / ln(2)
assert ma_exp_time < 872542, "ma_exp_time>max" # 7 * 24 * 60 * 60 / ln(2)
assert ma_exp_time > 86, "ma_exp_time<min" # 60 / ln(2)

assert initial_price > 10**6 and initial_price < 10**30 # dev: initial price out of bound
assert initial_price > 10**6 and initial_price < 10**30, "initial price out of bound"

assert _coins[0] != _coins[1], "Duplicate coins"

Expand Down
72 changes: 36 additions & 36 deletions contracts/main/CurveTwocryptoOptimized.vy
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ def __init__(
# --------------- Validate A and gamma parameters here and not in factory.
gamma_A: uint256[2] = self._unpack_2(packed_gamma_A) # gamma is at idx 0.

assert gamma_A[0] > MIN_GAMMA-1
assert gamma_A[0] < MAX_GAMMA+1
assert gamma_A[0] > MIN_GAMMA-1, "gamma<MIN"
assert gamma_A[0] < MAX_GAMMA+1, "gamma>MAX"

assert gamma_A[1] > MIN_A-1
assert gamma_A[1] < MAX_A+1
assert gamma_A[1] > MIN_A-1, "A<MIN"
assert gamma_A[1] < MAX_A+1, "A>MAX"

self.initial_A_gamma = packed_gamma_A
self.future_A_gamma = packed_gamma_A
Expand Down Expand Up @@ -312,7 +312,7 @@ def _transfer_in(
# If we checked for received_amounts == dx, an extra transfer without a
# call to exchange_received will break the method.
dx: uint256 = coin_balance - self.balances[_coin_idx]
assert dx >= _dx # dev: user didn't give us coins
assert dx >= _dx, "user didn't give us coins"
# Adjust balances
self.balances[_coin_idx] += dx
Expand All @@ -327,7 +327,7 @@ def _transfer_in(
self,
_dx,
default_return_value=True
)
), "transferFrom failed"
dx: uint256 = ERC20(coins[_coin_idx]).balanceOf(self) - coin_balance
self.balances[_coin_idx] += dx
Expand All @@ -353,7 +353,7 @@ def _transfer_out(_coin_idx: uint256, _amount: uint256, receiver: address):
receiver,
_amount,
default_return_value=True
)
), "transfer failed"
# -------------------------- AMM Main Functions ------------------------------
Expand Down Expand Up @@ -474,7 +474,7 @@ def add_liquidity(
d_token_fee: uint256 = 0
old_D: uint256 = 0
assert amounts[0] + amounts[1] > 0 # dev: no coins to add
assert amounts[0] + amounts[1] > 0, "no coins to add"
# --------------------- Get prices, balances -----------------------------
Expand Down Expand Up @@ -529,7 +529,7 @@ def add_liquidity(
else:
d_token = self.get_xcp(D, price_scale) # <----- Making initial virtual price equal to 1.
assert d_token > 0 # dev: nothing minted
assert d_token > 0, "nothing minted"
if old_D > 0:
Expand Down Expand Up @@ -616,7 +616,7 @@ def remove_liquidity(
for i in range(N_COINS):
withdraw_amounts[i] = balances[i] * amount / total_supply
assert withdraw_amounts[i] >= min_amounts[i]
assert withdraw_amounts[i] >= min_amounts[i], "slippage"
D: uint256 = self.D
self.D = D - unsafe_div(D * amount, total_supply) # <----------- Reduce D
Expand Down Expand Up @@ -749,8 +749,8 @@ def _exchange(
min_dy: uint256,
) -> uint256[3]:
assert i != j # dev: coin index out of range
assert dx_received > 0 # dev: do not exchange 0 coins
assert i != j, "same coin"
assert dx_received > 0, "zero dx"
A_gamma: uint256[2] = self._A_gamma()
xp: uint256[N_COINS] = self.balances
Expand Down Expand Up @@ -918,7 +918,7 @@ def tweak_price(
# else the pool suffers a loss.
if self.future_A_gamma_time < block.timestamp:
# this usually reverts when withdrawing a very small amount of LP tokens
assert virtual_price > old_virtual_price # dev: virtual price decreased
assert virtual_price > old_virtual_price, "virtual price decreased"
self.xcp_profit = xcp_profit
Expand Down Expand Up @@ -1222,8 +1222,8 @@ def _calc_withdraw_one_coin(
) -> (uint256, uint256, uint256[N_COINS], uint256):

token_supply: uint256 = self.totalSupply
assert token_amount <= token_supply # dev: token amount more than supply
assert i < N_COINS # dev: coin out of range
assert token_amount <= token_supply, "token amount more than supply"
assert i < N_COINS, "coin out of range"

xx: uint256[N_COINS] = self.balances
D0: uint256 = 0
Expand Down Expand Up @@ -1294,7 +1294,7 @@ def _approve(_owner: address, _spender: address, _value: uint256):

@internal
def _transfer(_from: address, _to: address, _value: uint256):
assert _to not in [self, empty(address)]
assert _to not in [self, empty(address)], "invalid receiver"

self.balanceOf[_from] -= _value
self.balanceOf[_to] += _value
Expand Down Expand Up @@ -1388,8 +1388,8 @@ def permit(
@param _s The second 32 bytes of the ECDSA signature.
@return bool Success.
"""
assert _owner != empty(address) # dev: invalid owner
assert block.timestamp <= _deadline # dev: permit expired
assert _owner != empty(address), "invalid owner"
assert block.timestamp <= _deadline, "permit expired"

nonce: uint256 = self.nonces[_owner]
digest: bytes32 = keccak256(
Expand All @@ -1403,7 +1403,7 @@ def permit(
),
)
)
assert ecrecover(digest, _v, _r, _s) == _owner # dev: invalid signature
assert ecrecover(digest, _v, _r, _s) == _owner, "invalid signature"

self.nonces[_owner] = unsafe_add(nonce, 1) # <-- Unsafe add is safe here.
self._approve(_owner, _spender, _value)
Expand Down Expand Up @@ -1776,26 +1776,26 @@ def ramp_A_gamma(
@param future_gamma The future gamma value.
@param future_time The timestamp at which the ramping will end.
"""
assert msg.sender == factory.admin() # dev: only owner
assert block.timestamp > self.future_A_gamma_time # dev: ramp undergoing
assert future_time > block.timestamp + MIN_RAMP_TIME - 1 # dev: insufficient time
assert msg.sender == factory.admin(), "unauthorized ramp"
assert block.timestamp > self.future_A_gamma_time, "ramp ongoing"
assert future_time > block.timestamp + MIN_RAMP_TIME - 1, "ramp time<min"

A_gamma: uint256[2] = self._A_gamma()
initial_A_gamma: uint256 = A_gamma[0] << 128
initial_A_gamma = initial_A_gamma | A_gamma[1]

assert future_A > MIN_A - 1
assert future_A < MAX_A + 1
assert future_gamma > MIN_GAMMA - 1
assert future_gamma < MAX_GAMMA + 1
assert future_A > MIN_A - 1, "A<min"
assert future_A < MAX_A + 1, "A>max"
assert future_gamma > MIN_GAMMA - 1, "gamma<min"
assert future_gamma < MAX_GAMMA + 1, "gamme>max"

ratio: uint256 = 10**18 * future_A / A_gamma[0]
assert ratio < 10**18 * MAX_A_CHANGE + 1 # dev: A change too high
assert ratio > 10**18 / MAX_A_CHANGE - 1 # dev: A change too low
assert ratio < 10**18 * MAX_A_CHANGE + 1, "A change too high"
assert ratio > 10**18 / MAX_A_CHANGE - 1, "A change too low"

ratio = 10**18 * future_gamma / A_gamma[1]
assert ratio < 10**18 * MAX_A_CHANGE + 1 # dev: gamma change too high
assert ratio > 10**18 / MAX_A_CHANGE - 1 # dev: gamma change too low
assert ratio < 10**18 * MAX_A_CHANGE + 1, "gamma change too high"
assert ratio > 10**18 / MAX_A_CHANGE - 1, "gamma change too low"

self.initial_A_gamma = initial_A_gamma
self.initial_A_gamma_time = block.timestamp
Expand All @@ -1821,7 +1821,7 @@ def stop_ramp_A_gamma():
@notice Stop Ramping A and gamma parameters immediately.
@dev Only accessible by factory admin.
"""
assert msg.sender == factory.admin() # dev: only owner
assert msg.sender == factory.admin(), "only owner"

A_gamma: uint256[2] = self._A_gamma()
current_A_gamma: uint256 = A_gamma[0] << 128
Expand Down Expand Up @@ -1856,7 +1856,7 @@ def apply_new_parameters(
@param _new_adjustment_step The new adjustment step.
@param _new_ma_time The new ma time. ma_time is time_in_seconds/ln(2).
"""
assert msg.sender == factory.admin() # dev: only owner
assert msg.sender == factory.admin(), "only owner"

# ----------------------------- Set fee params ---------------------------

Expand All @@ -1867,16 +1867,16 @@ def apply_new_parameters(
current_fee_params: uint256[3] = self._unpack_3(self.packed_fee_params)

if new_out_fee < MAX_FEE + 1:
assert new_out_fee > MIN_FEE - 1 # dev: fee is out of range
assert new_out_fee > MIN_FEE - 1, "fee is out of range"
else:
new_out_fee = current_fee_params[1]

if new_mid_fee > MAX_FEE:
new_mid_fee = current_fee_params[0]
assert new_mid_fee <= new_out_fee # dev: mid-fee is too high
assert new_mid_fee <= new_out_fee, "mid-fee is too high"

if new_fee_gamma < 10**18:
assert new_fee_gamma > 0 # dev: fee_gamma out of range [1 .. 10**18]
assert new_fee_gamma > 0, "fee_gamma out of range [1 .. 10**18]"
else:
new_fee_gamma = current_fee_params[2]

Expand All @@ -1897,7 +1897,7 @@ def apply_new_parameters(
new_adjustment_step = current_rebalancing_params[1]

if new_ma_time < 872542: # <----- Calculated as: 7 * 24 * 60 * 60 / ln(2)
assert new_ma_time > 86 # dev: MA time should be longer than 60/ln(2)
assert new_ma_time > 86, "MA time should be longer than 60/ln(2)"
else:
new_ma_time = current_rebalancing_params[2]

Expand Down

0 comments on commit 4a1a2d3

Please sign in to comment.