Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use helper function to detect ramp #26

Merged
merged 2 commits into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions contracts/main/Twocrypto.vy
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ def add_liquidity(

# -------------------- Calculate LP tokens to mint -----------------------

if self.future_A_gamma_time > block.timestamp: # <--- A_gamma is ramping.

# ----- Recalculate the invariant if A or gamma are undergoing a ramp.
if self._is_ramping():
# Recalculate D if A and/or gamma are ramping because the shape of
# the bonding curve is changing.
old_D = staticcall MATH.newton_D(A_gamma[0], A_gamma[1], xp_old, 0)

else:
Expand Down Expand Up @@ -669,8 +669,7 @@ def remove_liquidity_one_coin(
A_gamma,
token_amount,
i,
(self.future_A_gamma_time > block.timestamp), # <------- During ramps
) # we need to update D.
)

assert dy >= min_amount, "slippage"

Expand Down Expand Up @@ -915,11 +914,14 @@ def tweak_price(
old_virtual_price
)

# If A and gamma are not undergoing ramps (t < block.timestamp),
# ensure new virtual_price is not less than old virtual_price,
# else the pool suffers a loss.
if self.future_A_gamma_time < block.timestamp:
assert virtual_price > old_virtual_price, "virtual price decreased"
# If A and gamma are not being ramped, we only allow the virtual price to
# to increase.
# This is does not imply that the virtual price will have increased at the
# end of this function: it can still decrease if the pool rebalances.
if not (virtual_price > old_virtual_price):
# If A and gamma are being ramped, we allow the virtual price to decrease,
# as changing the shape of the bonding curve causes losses in the pool.
assert self._is_ramping(), "virtual price decreased"

self.xcp_profit = xcp_profit

Expand Down Expand Up @@ -1017,7 +1019,7 @@ def _claim_admin_fees():
last_claim_time: uint256 = self.last_admin_fee_claim_timestamp
if (
unsafe_sub(block.timestamp, last_claim_time) < MIN_ADMIN_FEE_CLAIM_INTERVAL or
self.future_A_gamma_time > block.timestamp
self._is_ramping()
):
return

Expand Down Expand Up @@ -1132,6 +1134,14 @@ def xp(
unsafe_div(balances[1] * PRECISIONS[1] * price_scale, PRECISION)
]

@internal
@view
def _is_ramping() -> bool:
"""
@notice Checks if A and gamma are ramping.
@return bool True if A and/or gamma are ramping, False otherwise.
"""
return self.future_A_gamma_time > block.timestamp

@view
@internal
Expand Down Expand Up @@ -1219,15 +1229,13 @@ def _calc_withdraw_one_coin(
A_gamma: uint256[2],
token_amount: uint256,
i: uint256,
update_D: bool,
) -> (uint256, uint256, uint256[N_COINS], uint256):

token_supply: uint256 = self.totalSupply
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

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

Expand All @@ -1239,12 +1247,12 @@ def _calc_withdraw_one_coin(
if i == 0:
price_scale_i = PRECISION * PRECISIONS[0]

if update_D: # <-------------- D is updated if pool is undergoing a ramp.
D0 = staticcall MATH.newton_D(A_gamma[0], A_gamma[1], xp, 0)
else:
D0 = self.D
D: uint256 = 0

D: uint256 = D0
if self._is_ramping():
D = staticcall MATH.newton_D(A_gamma[0], A_gamma[1], xp, 0)
else:
D = self.D

# -------------------------------- Fee Calc ------------------------------

Expand Down Expand Up @@ -1632,7 +1640,6 @@ def calc_withdraw_one_coin(token_amount: uint256, i: uint256) -> uint256:
self._A_gamma(),
token_amount,
i,
(self.future_A_gamma_time > block.timestamp)
)[0]


Expand Down Expand Up @@ -1778,7 +1785,7 @@ def ramp_A_gamma(
@param future_time The timestamp at which the ramping will end.
"""
assert msg.sender == staticcall factory.admin(), "only owner"
assert block.timestamp > self.future_A_gamma_time, "ramp undergoing"
assert not self._is_ramping(), "ramp undergoing"
assert future_time > block.timestamp + MIN_RAMP_TIME - 1, "ramp time<min"

A_gamma: uint256[2] = self._A_gamma()
Expand Down
Loading