Skip to content

Commit b1d63f4

Browse files
committed
fix: full repay for max_value(uint256)
1 parent 4b4ddd2 commit b1d63f4

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

contracts/Controller.vy

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,8 @@ def repay(
907907
):
908908
"""
909909
@notice Repay debt (partially or fully)
910-
@param _d_debt The amount of debt to repay from user's wallet. If higher than the current debt - will do full repayment
910+
@param _d_debt The amount of debt to repay from user's wallet.
911+
If it's max_value(uint256) or just higher than the current debt - will do full repayment.
911912
@param _for The user to repay the debt for
912913
@param max_active_band Don't allow active band to be higher than this (to prevent front-running the repay)
913914
@param callbacker Address of the callback contract
@@ -929,18 +930,16 @@ def repay(
929930
callbacker, CALLBACK_REPAY, _for, xy[0], xy[1], debt, calldata
930931
)
931932

932-
total_borrowed: uint256 = _d_debt + xy[0] + cb.borrowed
933-
assert total_borrowed > 0 # dev: no coins to repay
934-
d_debt: uint256 = 0
933+
d_debt: uint256 = min(min(_d_debt, debt) + xy[0] + cb.borrowed, debt)
934+
assert d_debt > 0 # dev: no coins to repay
935935

936936
# If we have more borrowed tokens than the debt - full repayment and closing the position
937-
if total_borrowed >= debt:
938-
d_debt = debt
937+
if d_debt >= debt:
939938
debt = 0
940939
if callbacker == empty(address):
941940
xy = extcall AMM.withdraw(_for, WAD)
942941

943-
total_borrowed = 0
942+
total_borrowed: uint256 = 0
944943
if xy[0] > 0:
945944
# Only allow full repayment when underwater for the sender to do
946945
assert approval
@@ -983,7 +982,6 @@ def repay(
983982
active_band: int256 = staticcall AMM.active_band_with_skip()
984983
assert active_band <= max_active_band
985984

986-
d_debt = total_borrowed
987985
debt = unsafe_sub(debt, d_debt)
988986
ns: int256[2] = staticcall AMM.read_user_tick_numbers(_for)
989987
size: int256 = unsafe_sub(ns[1], ns[0])

0 commit comments

Comments
 (0)