Skip to content

Commit e67ad72

Browse files
fix: revert on non-convergence
1 parent 9792fa0 commit e67ad72

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

contracts/pool-templates/base/SwapTemplateBase.vy

+12-9
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,14 @@ def get_D(xp: uint256[N_COINS], amp: uint256) -> uint256:
223223
# Equality with the precision of 1
224224
if D > Dprev:
225225
if D - Dprev <= 1:
226-
break
226+
return D
227227
else:
228228
if Dprev - D <= 1:
229-
break
230-
return D
229+
return D
230+
231+
# convergence typically occurs in 4 rounds or less, this should be unreachable!
232+
# if it does happen the pool is borked and LPs can withdraw via `remove_liquidity`
233+
raise
231234

232235

233236
@view
@@ -408,11 +411,11 @@ def get_y(i: int128, j: int128, x: uint256, xp_: uint256[N_COINS]) -> uint256:
408411
# Equality with the precision of 1
409412
if y > y_prev:
410413
if y - y_prev <= 1:
411-
break
414+
return y
412415
else:
413416
if y_prev - y <= 1:
414-
break
415-
return y
417+
return y
418+
raise
416419

417420

418421
@view
@@ -639,11 +642,11 @@ def get_y_D(A_: uint256, i: int128, xp: uint256[N_COINS], D: uint256) -> uint256
639642
# Equality with the precision of 1
640643
if y > y_prev:
641644
if y - y_prev <= 1:
642-
break
645+
return y
643646
else:
644647
if y_prev - y <= 1:
645-
break
646-
return y
648+
return y
649+
raise
647650

648651

649652
@view

contracts/pool-templates/meta/SwapTemplateMeta.vy

+11-9
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,13 @@ def get_D(xp: uint256[N_COINS], amp: uint256) -> uint256:
297297
# Equality with the precision of 1
298298
if D > Dprev:
299299
if D - Dprev <= 1:
300-
break
300+
return D
301301
else:
302302
if Dprev - D <= 1:
303-
break
304-
return D
303+
return D
304+
# convergence typically occurs in 4 rounds or less, this should be unreachable!
305+
# if it does happen the pool is borked and LPs can withdraw via `remove_liquidity`
306+
raise
305307

306308

307309
@view
@@ -475,11 +477,11 @@ def get_y(i: int128, j: int128, x: uint256, xp_: uint256[N_COINS]) -> uint256:
475477
# Equality with the precision of 1
476478
if y > y_prev:
477479
if y - y_prev <= 1:
478-
break
480+
return y
479481
else:
480482
if y_prev - y <= 1:
481-
break
482-
return y
483+
return y
484+
raise
483485

484486

485487
@view
@@ -854,11 +856,11 @@ def get_y_D(A_: uint256, i: int128, xp: uint256[N_COINS], D: uint256) -> uint256
854856
# Equality with the precision of 1
855857
if y > y_prev:
856858
if y - y_prev <= 1:
857-
break
859+
return y
858860
else:
859861
if y_prev - y <= 1:
860-
break
861-
return y
862+
return y
863+
raise
862864

863865

864866
@view

contracts/pool-templates/y/SwapTemplateY.vy

+11-9
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,13 @@ def get_D(xp: uint256[N_COINS], amp: uint256) -> uint256:
264264
# Equality with the precision of 1
265265
if D > Dprev:
266266
if D - Dprev <= 1:
267-
break
267+
return D
268268
else:
269269
if Dprev - D <= 1:
270-
break
271-
return D
270+
return D
271+
# convergence typically occurs in 4 rounds or less, this should be unreachable!
272+
# if it does happen the pool is borked and LPs can withdraw via `remove_liquidity`
273+
raise
272274

273275

274276
@view
@@ -437,11 +439,11 @@ def get_y(i: int128, j: int128, x: uint256, xp_: uint256[N_COINS]) -> uint256:
437439
# Equality with the precision of 1
438440
if y > y_prev:
439441
if y - y_prev <= 1:
440-
break
442+
return y
441443
else:
442444
if y_prev - y <= 1:
443-
break
444-
return y
445+
return y
446+
raise
445447

446448

447449
@view
@@ -726,11 +728,11 @@ def get_y_D(A_: uint256, i: int128, xp: uint256[N_COINS], D: uint256) -> uint256
726728
# Equality with the precision of 1
727729
if y > y_prev:
728730
if y - y_prev <= 1:
729-
break
731+
return y
730732
else:
731733
if y_prev - y <= 1:
732-
break
733-
return y
734+
return y
735+
raise
734736

735737

736738
@view

0 commit comments

Comments
 (0)