@@ -203,7 +203,7 @@ def _xp_mem(_balances: uint256[N_COINS]) -> uint256[N_COINS]:
203
203
204
204
@pure
205
205
@internal
206
- def get_D (_xp: uint256 [N_COINS], _amp: uint256 ) -> uint256 :
206
+ def _get_D (_xp: uint256 [N_COINS], _amp: uint256 ) -> uint256 :
207
207
S: uint256 = 0
208
208
Dprev: uint256 = 0
209
209
@@ -234,8 +234,8 @@ def get_D(_xp: uint256[N_COINS], _amp: uint256) -> uint256:
234
234
235
235
@view
236
236
@internal
237
- def get_D_mem (_balances: uint256 [N_COINS], _amp: uint256 ) -> uint256 :
238
- return self .get_D (self ._xp_mem (_balances), _amp)
237
+ def _get_D_mem (_balances: uint256 [N_COINS], _amp: uint256 ) -> uint256 :
238
+ return self ._get_D (self ._xp_mem (_balances), _amp)
239
239
240
240
241
241
@view
@@ -246,7 +246,7 @@ def get_virtual_price() -> uint256:
246
246
@dev Useful for calculating profits
247
247
@return LP token virtual price normalized to 1e18
248
248
"""
249
- D: uint256 = self .get_D (self ._xp (), self ._A ())
249
+ D: uint256 = self ._get_D (self ._xp (), self ._A ())
250
250
# D is in the units similar to DAI (e.g. converted to precision 1e18)
251
251
# When balanced, D = n * x_u - total virtual value of the portfolio
252
252
token_supply: uint256 = ERC20 (self .lp_token).totalSupply ()
@@ -266,13 +266,13 @@ def calc_token_amount(_amounts: uint256[N_COINS], _is_deposit: bool) -> uint256:
266
266
"""
267
267
amp: uint256 = self ._A ()
268
268
balances: uint256 [N_COINS] = self .balances
269
- D0: uint256 = self .get_D_mem (balances, amp)
269
+ D0: uint256 = self ._get_D_mem (balances, amp)
270
270
for i in range (N_COINS):
271
271
if _is_deposit:
272
272
balances[i] += _amounts[i]
273
273
else :
274
274
balances[i] -= _amounts[i]
275
- D1: uint256 = self .get_D_mem (balances, amp)
275
+ D1: uint256 = self ._get_D_mem (balances, amp)
276
276
token_amount: uint256 = ERC20 (self .lp_token).totalSupply ()
277
277
diff: uint256 = 0
278
278
if _is_deposit:
@@ -301,7 +301,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
301
301
D0: uint256 = 0
302
302
old_balances: uint256 [N_COINS] = self .balances
303
303
if token_supply > 0 :
304
- D0 = self .get_D_mem (old_balances, amp)
304
+ D0 = self ._get_D_mem (old_balances, amp)
305
305
new_balances: uint256 [N_COINS] = old_balances
306
306
for i in range (N_COINS):
307
307
if token_supply == 0 :
@@ -310,7 +310,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
310
310
new_balances[i] = old_balances[i] + _amounts[i]
311
311
312
312
# Invariant after change
313
- D1: uint256 = self .get_D_mem (new_balances, amp)
313
+ D1: uint256 = self ._get_D_mem (new_balances, amp)
314
314
assert D1 > D0
315
315
316
316
# We need to recalculate the invariant accounting for fees
@@ -332,7 +332,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
332
332
fees[i] = fee * difference / FEE_DENOMINATOR
333
333
self .balances[i] = new_balances[i] - (fees[i] * admin_fee / FEE_DENOMINATOR)
334
334
new_balances[i] -= fees[i]
335
- D2 = self .get_D_mem (new_balances, amp)
335
+ D2 = self ._get_D_mem (new_balances, amp)
336
336
mint_amount = token_supply * (D2 - D0) / D0
337
337
else :
338
338
self .balances = new_balances
@@ -366,7 +366,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
366
366
367
367
@view
368
368
@internal
369
- def get_y (i: int128 , j: int128 , x: uint256 , _xp: uint256 [N_COINS]) -> uint256 :
369
+ def _get_y (i: int128 , j: int128 , x: uint256 , _xp: uint256 [N_COINS]) -> uint256 :
370
370
# x in the input is converted to the same price/precision
371
371
372
372
assert i != j # dev: same coin
@@ -378,7 +378,7 @@ def get_y(i: int128, j: int128, x: uint256, _xp: uint256[N_COINS]) -> uint256:
378
378
assert i < N_COINS
379
379
380
380
A: uint256 = self ._A ()
381
- D: uint256 = self .get_D (_xp, A)
381
+ D: uint256 = self ._get_D (_xp, A)
382
382
Ann: uint256 = A * N_COINS
383
383
c: uint256 = D
384
384
S: uint256 = 0
@@ -417,7 +417,7 @@ def get_dy(i: int128, j: int128, _dx: uint256) -> uint256:
417
417
rates: uint256 [N_COINS] = RATES
418
418
419
419
x: uint256 = xp[i] + (_dx * rates[i] / PRECISION)
420
- y: uint256 = self .get_y (i, j, x, xp)
420
+ y: uint256 = self ._get_y (i, j, x, xp)
421
421
dy: uint256 = (xp[j] - y - 1 )
422
422
fee: uint256 = self .fee * dy / FEE_DENOMINATOR
423
423
return (dy - fee) * PRECISION / rates[j]
@@ -442,7 +442,7 @@ def exchange(i: int128, j: int128, _dx: uint256, _min_dy: uint256) -> uint256:
442
442
443
443
rates: uint256 [N_COINS] = RATES
444
444
x: uint256 = xp[i] + _dx * rates[i] / PRECISION
445
- y: uint256 = self .get_y (i, j, x, xp)
445
+ y: uint256 = self ._get_y (i, j, x, xp)
446
446
447
447
dy: uint256 = xp[j] - y - 1 # -1 just in case there were some rounding errors
448
448
dy_fee: uint256 = dy * self .fee / FEE_DENOMINATOR
@@ -543,10 +543,10 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
543
543
amp: uint256 = self ._A ()
544
544
old_balances: uint256 [N_COINS] = self .balances
545
545
new_balances: uint256 [N_COINS] = old_balances
546
- D0: uint256 = self .get_D_mem (old_balances, amp)
546
+ D0: uint256 = self ._get_D_mem (old_balances, amp)
547
547
for i in range (N_COINS):
548
548
new_balances[i] -= _amounts[i]
549
- D1: uint256 = self .get_D_mem (new_balances, amp)
549
+ D1: uint256 = self ._get_D_mem (new_balances, amp)
550
550
551
551
lp_token: address = self .lp_token
552
552
token_supply: uint256 = ERC20 (lp_token).totalSupply ()
@@ -565,7 +565,7 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
565
565
fees[i] = fee * difference / FEE_DENOMINATOR
566
566
self .balances[i] = new_balances[i] - (fees[i] * admin_fee / FEE_DENOMINATOR)
567
567
new_balances[i] -= fees[i]
568
- D2: uint256 = self .get_D_mem (new_balances, amp)
568
+ D2: uint256 = self ._get_D_mem (new_balances, amp)
569
569
570
570
token_amount: uint256 = (D0 - D2) * token_supply / D0
571
571
assert token_amount != 0 # dev: zero tokens burned
@@ -595,12 +595,12 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
595
595
596
596
@pure
597
597
@internal
598
- def get_y_D (A: uint256 , i: int128 , _xp: uint256 [N_COINS], D: uint256 ) -> uint256 :
598
+ def _get_y_D (A: uint256 , i: int128 , _xp: uint256 [N_COINS], D: uint256 ) -> uint256 :
599
599
"""
600
600
Calculate x[i] if one reduces D from being calculated for xp to D
601
601
602
602
Done by solving quadratic equation iteratively.
603
- x_1**2 + x1 * (sum' - (A*n**n - 1) * D / (A * n**n)) = D ** (n + 1) / (n ** (2 * n) * prod' * A)
603
+ x_1**2 + x_1 * (sum' - (A*n**n - 1) * D / (A * n**n)) = D ** (n + 1) / (n ** (2 * n) * prod' * A)
604
604
x_1**2 + b*x_1 = c
605
605
606
606
x_1 = (x_1**2 + c) / (2*x_1 + b)
@@ -648,11 +648,11 @@ def _calc_withdraw_one_coin(_token_amount: uint256, i: int128) -> (uint256, uint
648
648
# * Solve Eqn against y_i for D - _token_amount
649
649
amp: uint256 = self ._A ()
650
650
xp: uint256 [N_COINS] = self ._xp ()
651
- D0: uint256 = self .get_D (xp, amp)
651
+ D0: uint256 = self ._get_D (xp, amp)
652
652
653
653
total_supply: uint256 = ERC20 (self .lp_token).totalSupply ()
654
654
D1: uint256 = D0 - _token_amount * D0 / total_supply
655
- new_y: uint256 = self .get_y_D (amp, i, xp, D1)
655
+ new_y: uint256 = self ._get_y_D (amp, i, xp, D1)
656
656
xp_reduced: uint256 [N_COINS] = xp
657
657
fee: uint256 = self .fee * N_COINS / (4 * (N_COINS - 1 ))
658
658
for j in range (N_COINS):
@@ -663,7 +663,7 @@ def _calc_withdraw_one_coin(_token_amount: uint256, i: int128) -> (uint256, uint
663
663
dx_expected = xp[j] - xp[j] * D1 / D0
664
664
xp_reduced[j] -= fee * dx_expected / FEE_DENOMINATOR
665
665
666
- dy: uint256 = xp_reduced[i] - self .get_y_D (amp, i, xp_reduced, D1)
666
+ dy: uint256 = xp_reduced[i] - self ._get_y_D (amp, i, xp_reduced, D1)
667
667
precisions: uint256 [N_COINS] = PRECISION_MUL
668
668
dy = (dy - 1 ) / precisions[i] # Withdraw less to account for rounding errors
669
669
dy_0: uint256 = (xp[i] - new_y) / precisions[i] # w/o fees
0 commit comments