Skip to content

Commit a199d3e

Browse files
committed
style: Follow curve vyper style guide
1 parent d273e53 commit a199d3e

File tree

4 files changed

+84
-84
lines changed

4 files changed

+84
-84
lines changed

contracts/pool-templates/base/SwapTemplateBase.vy

+21-21
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _xp_mem(_balances: uint256[N_COINS]) -> uint256[N_COINS]:
203203

204204
@pure
205205
@internal
206-
def get_D(_xp: uint256[N_COINS], _amp: uint256) -> uint256:
206+
def _get_D(_xp: uint256[N_COINS], _amp: uint256) -> uint256:
207207
S: uint256 = 0
208208
Dprev: uint256 = 0
209209

@@ -234,8 +234,8 @@ def get_D(_xp: uint256[N_COINS], _amp: uint256) -> uint256:
234234

235235
@view
236236
@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)
239239

240240

241241
@view
@@ -246,7 +246,7 @@ def get_virtual_price() -> uint256:
246246
@dev Useful for calculating profits
247247
@return LP token virtual price normalized to 1e18
248248
"""
249-
D: uint256 = self.get_D(self._xp(), self._A())
249+
D: uint256 = self._get_D(self._xp(), self._A())
250250
# D is in the units similar to DAI (e.g. converted to precision 1e18)
251251
# When balanced, D = n * x_u - total virtual value of the portfolio
252252
token_supply: uint256 = ERC20(self.lp_token).totalSupply()
@@ -266,13 +266,13 @@ def calc_token_amount(_amounts: uint256[N_COINS], _is_deposit: bool) -> uint256:
266266
"""
267267
amp: uint256 = self._A()
268268
balances: uint256[N_COINS] = self.balances
269-
D0: uint256 = self.get_D_mem(balances, amp)
269+
D0: uint256 = self._get_D_mem(balances, amp)
270270
for i in range(N_COINS):
271271
if _is_deposit:
272272
balances[i] += _amounts[i]
273273
else:
274274
balances[i] -= _amounts[i]
275-
D1: uint256 = self.get_D_mem(balances, amp)
275+
D1: uint256 = self._get_D_mem(balances, amp)
276276
token_amount: uint256 = ERC20(self.lp_token).totalSupply()
277277
diff: uint256 = 0
278278
if _is_deposit:
@@ -301,7 +301,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
301301
D0: uint256 = 0
302302
old_balances: uint256[N_COINS] = self.balances
303303
if token_supply > 0:
304-
D0 = self.get_D_mem(old_balances, amp)
304+
D0 = self._get_D_mem(old_balances, amp)
305305
new_balances: uint256[N_COINS] = old_balances
306306
for i in range(N_COINS):
307307
if token_supply == 0:
@@ -310,7 +310,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
310310
new_balances[i] = old_balances[i] + _amounts[i]
311311

312312
# Invariant after change
313-
D1: uint256 = self.get_D_mem(new_balances, amp)
313+
D1: uint256 = self._get_D_mem(new_balances, amp)
314314
assert D1 > D0
315315

316316
# 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
332332
fees[i] = fee * difference / FEE_DENOMINATOR
333333
self.balances[i] = new_balances[i] - (fees[i] * admin_fee / FEE_DENOMINATOR)
334334
new_balances[i] -= fees[i]
335-
D2 = self.get_D_mem(new_balances, amp)
335+
D2 = self._get_D_mem(new_balances, amp)
336336
mint_amount = token_supply * (D2 - D0) / D0
337337
else:
338338
self.balances = new_balances
@@ -366,7 +366,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
366366

367367
@view
368368
@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:
370370
# x in the input is converted to the same price/precision
371371

372372
assert i != j # dev: same coin
@@ -378,7 +378,7 @@ def get_y(i: int128, j: int128, x: uint256, _xp: uint256[N_COINS]) -> uint256:
378378
assert i < N_COINS
379379

380380
A: uint256 = self._A()
381-
D: uint256 = self.get_D(_xp, A)
381+
D: uint256 = self._get_D(_xp, A)
382382
Ann: uint256 = A * N_COINS
383383
c: uint256 = D
384384
S: uint256 = 0
@@ -417,7 +417,7 @@ def get_dy(i: int128, j: int128, _dx: uint256) -> uint256:
417417
rates: uint256[N_COINS] = RATES
418418

419419
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)
421421
dy: uint256 = (xp[j] - y - 1)
422422
fee: uint256 = self.fee * dy / FEE_DENOMINATOR
423423
return (dy - fee) * PRECISION / rates[j]
@@ -442,7 +442,7 @@ def exchange(i: int128, j: int128, _dx: uint256, _min_dy: uint256) -> uint256:
442442

443443
rates: uint256[N_COINS] = RATES
444444
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)
446446

447447
dy: uint256 = xp[j] - y - 1 # -1 just in case there were some rounding errors
448448
dy_fee: uint256 = dy * self.fee / FEE_DENOMINATOR
@@ -543,10 +543,10 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
543543
amp: uint256 = self._A()
544544
old_balances: uint256[N_COINS] = self.balances
545545
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)
547547
for i in range(N_COINS):
548548
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)
550550

551551
lp_token: address = self.lp_token
552552
token_supply: uint256 = ERC20(lp_token).totalSupply()
@@ -565,7 +565,7 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
565565
fees[i] = fee * difference / FEE_DENOMINATOR
566566
self.balances[i] = new_balances[i] - (fees[i] * admin_fee / FEE_DENOMINATOR)
567567
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)
569569

570570
token_amount: uint256 = (D0 - D2) * token_supply / D0
571571
assert token_amount != 0 # dev: zero tokens burned
@@ -595,12 +595,12 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
595595

596596
@pure
597597
@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:
599599
"""
600600
Calculate x[i] if one reduces D from being calculated for xp to D
601601
602602
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)
604604
x_1**2 + b*x_1 = c
605605
606606
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
648648
# * Solve Eqn against y_i for D - _token_amount
649649
amp: uint256 = self._A()
650650
xp: uint256[N_COINS] = self._xp()
651-
D0: uint256 = self.get_D(xp, amp)
651+
D0: uint256 = self._get_D(xp, amp)
652652

653653
total_supply: uint256 = ERC20(self.lp_token).totalSupply()
654654
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)
656656
xp_reduced: uint256[N_COINS] = xp
657657
fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1))
658658
for j in range(N_COINS):
@@ -663,7 +663,7 @@ def _calc_withdraw_one_coin(_token_amount: uint256, i: int128) -> (uint256, uint
663663
dx_expected = xp[j] - xp[j] * D1 / D0
664664
xp_reduced[j] -= fee * dx_expected / FEE_DENOMINATOR
665665

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)
667667
precisions: uint256[N_COINS] = PRECISION_MUL
668668
dy = (dy - 1) / precisions[i] # Withdraw less to account for rounding errors
669669
dy_0: uint256 = (xp[i] - new_y) / precisions[i] # w/o fees

contracts/pool-templates/eth/SwapTemplateEth.vy

+18-18
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def A_precise() -> uint256:
182182

183183
@pure
184184
@internal
185-
def get_D(xp: uint256[N_COINS], amp: uint256) -> uint256:
185+
def _get_D(xp: uint256[N_COINS], amp: uint256) -> uint256:
186186
S: uint256 = 0
187187
Dprev: uint256 = 0
188188
for _x in xp:
@@ -218,7 +218,7 @@ def get_virtual_price() -> uint256:
218218
@dev Useful for calculating profits
219219
@return LP token virtual price normalized to 1e18
220220
"""
221-
D: uint256 = self.get_D(self.balances, self._A())
221+
D: uint256 = self._get_D(self.balances, self._A())
222222
# D is in the units similar to DAI (e.g. converted to precision 1e18)
223223
# When balanced, D = n * x_u - total virtual value of the portfolio
224224
token_supply: uint256 = ERC20(self.lp_token).totalSupply()
@@ -238,13 +238,13 @@ def calc_token_amount(_amounts: uint256[N_COINS], _is_deposit: bool) -> uint256:
238238
"""
239239
amp: uint256 = self._A()
240240
_balances: uint256[N_COINS] = self.balances
241-
D0: uint256 = self.get_D(_balances, amp)
241+
D0: uint256 = self._get_D(_balances, amp)
242242
for i in range(N_COINS):
243243
if _is_deposit:
244244
_balances[i] += _amounts[i]
245245
else:
246246
_balances[i] -= _amounts[i]
247-
D1: uint256 = self.get_D(_balances, amp)
247+
D1: uint256 = self._get_D(_balances, amp)
248248
token_amount: uint256 = ERC20(self.lp_token).totalSupply()
249249
diff: uint256 = 0
250250
if _is_deposit:
@@ -269,7 +269,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
269269
# Initial invariant
270270
amp: uint256 = self._A()
271271
old_balances: uint256[N_COINS] = self.balances
272-
D0: uint256 = self.get_D(old_balances, amp)
272+
D0: uint256 = self._get_D(old_balances, amp)
273273

274274
lp_token: address = self.lp_token
275275
token_supply: uint256 = ERC20(lp_token).totalSupply()
@@ -280,7 +280,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
280280
new_balances[i] = old_balances[i] + _amounts[i]
281281

282282
# Invariant after change
283-
D1: uint256 = self.get_D(new_balances, amp)
283+
D1: uint256 = self._get_D(new_balances, amp)
284284
assert D1 > D0
285285

286286
# We need to recalculate the invariant accounting for fees
@@ -302,7 +302,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
302302
fees[i] = fee * difference / FEE_DENOMINATOR
303303
self.balances[i] = new_balances[i] - (fees[i] * admin_fee / FEE_DENOMINATOR)
304304
new_balances[i] -= fees[i]
305-
D2 = self.get_D(new_balances, amp)
305+
D2 = self._get_D(new_balances, amp)
306306
mint_amount = token_supply * (D2 - D0) / D0
307307
else:
308308
self.balances = new_balances
@@ -341,7 +341,7 @@ def add_liquidity(_amounts: uint256[N_COINS], _min_mint_amount: uint256) -> uint
341341

342342
@view
343343
@internal
344-
def get_y(i: int128, j: int128, x: uint256, _xp: uint256[N_COINS]) -> uint256:
344+
def _get_y(i: int128, j: int128, x: uint256, _xp: uint256[N_COINS]) -> uint256:
345345
# x in the input is converted to the same price/precision
346346

347347
assert i != j # dev: same coin
@@ -353,7 +353,7 @@ def get_y(i: int128, j: int128, x: uint256, _xp: uint256[N_COINS]) -> uint256:
353353
assert i < N_COINS
354354

355355
A: uint256 = self._A()
356-
D: uint256 = self.get_D(_xp, A)
356+
D: uint256 = self._get_D(_xp, A)
357357
c: uint256 = D
358358
S: uint256 = 0
359359
_x: uint256 = 0
@@ -391,7 +391,7 @@ def get_y(i: int128, j: int128, x: uint256, _xp: uint256[N_COINS]) -> uint256:
391391
def get_dy(i: int128, j: int128, _dx: uint256) -> uint256:
392392
xp: uint256[N_COINS] = self.balances
393393
x: uint256 = xp[i] + _dx
394-
y: uint256 = self.get_y(i, j, x, xp)
394+
y: uint256 = self._get_y(i, j, x, xp)
395395
dy: uint256 = xp[j] - y - 1
396396
fee: uint256 = self.fee * dy / FEE_DENOMINATOR
397397
return dy - fee
@@ -415,7 +415,7 @@ def exchange(i: int128, j: int128, _dx: uint256, _min_dy: uint256) -> uint256:
415415
old_balances: uint256[N_COINS] = self.balances
416416

417417
x: uint256 = old_balances[i] + _dx
418-
y: uint256 = self.get_y(i, j, x, old_balances)
418+
y: uint256 = self._get_y(i, j, x, old_balances)
419419

420420
dy: uint256 = old_balances[j] - y - 1 # -1 just in case there were some rounding errors
421421
dy_fee: uint256 = dy * self.fee / FEE_DENOMINATOR
@@ -532,12 +532,12 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
532532
assert token_supply != 0 # dev: zero total supply
533533

534534
old_balances: uint256[N_COINS] = self.balances
535-
D0: uint256 = self.get_D(old_balances, amp)
535+
D0: uint256 = self._get_D(old_balances, amp)
536536

537537
new_balances: uint256[N_COINS] = empty(uint256[N_COINS])
538538
for i in range(N_COINS):
539539
new_balances[i] = old_balances[i] - _amounts[i]
540-
D1: uint256 = self.get_D(new_balances, amp)
540+
D1: uint256 = self._get_D(new_balances, amp)
541541

542542
fees: uint256[N_COINS] = empty(uint256[N_COINS])
543543
difference: uint256 = 0
@@ -553,7 +553,7 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
553553
fees[i] = fee * difference / FEE_DENOMINATOR
554554
self.balances[i] = new_balance - (fees[i] * admin_fee / FEE_DENOMINATOR)
555555
new_balances[i] = new_balance - fees[i]
556-
D2: uint256 = self.get_D(new_balances, amp)
556+
D2: uint256 = self._get_D(new_balances, amp)
557557

558558
token_amount: uint256 = (D0 - D2) * token_supply / D0
559559
assert token_amount != 0 # dev: zero tokens burned
@@ -588,7 +588,7 @@ def remove_liquidity_imbalance(_amounts: uint256[N_COINS], _max_burn_amount: uin
588588

589589
@view
590590
@internal
591-
def get_y_D(A: uint256, i: int128, _xp: uint256[N_COINS], D: uint256) -> uint256:
591+
def _get_y_D(A: uint256, i: int128, _xp: uint256[N_COINS], D: uint256) -> uint256:
592592
"""
593593
Calculate x[i] if one reduces D from being calculated for xp to D
594594
@@ -641,10 +641,10 @@ def _calc_withdraw_one_coin(_token_amount: uint256, i: int128) -> (uint256, uint
641641
# * Solve Eqn against y_i for D - _token_amount
642642
amp: uint256 = self._A()
643643
xp: uint256[N_COINS] = self.balances
644-
D0: uint256 = self.get_D(xp, amp)
644+
D0: uint256 = self._get_D(xp, amp)
645645
total_supply: uint256 = ERC20(self.lp_token).totalSupply()
646646
D1: uint256 = D0 - _token_amount * D0 / total_supply
647-
new_y: uint256 = self.get_y_D(amp, i, xp, D1)
647+
new_y: uint256 = self._get_y_D(amp, i, xp, D1)
648648

649649
fee: uint256 = self.fee * N_COINS / (4 * (N_COINS - 1))
650650
xp_reduced: uint256[N_COINS] = xp
@@ -656,7 +656,7 @@ def _calc_withdraw_one_coin(_token_amount: uint256, i: int128) -> (uint256, uint
656656
dx_expected = xp[j] - xp[j] * D1 / D0
657657
xp_reduced[j] -= fee * dx_expected / FEE_DENOMINATOR
658658

659-
dy: uint256 = xp_reduced[i] - self.get_y_D(amp, i, xp_reduced, D1)
659+
dy: uint256 = xp_reduced[i] - self._get_y_D(amp, i, xp_reduced, D1)
660660

661661
dy -= 1 # Withdraw less to account for rounding errors
662662
dy_0: uint256 = xp[i] - new_y # w/o fees

0 commit comments

Comments
 (0)