-
Notifications
You must be signed in to change notification settings - Fork 1
/
curve_get_d.vy.txt
74 lines (66 loc) · 2.25 KB
/
curve_get_d.vy.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#vyper: 0.1.0b16 https://etherscan.io/vyper
# This can (and needs to) be changed at compile time
N_COINS: constant(int128) = 3 # <- change
ZERO256: constant(uint256) = 0 # This hack is really bad XXX
ZEROS: constant(uint256[N_COINS]) = [ZERO256, ZERO256, ZERO256] # <- change
USE_LENDING: constant(bool[N_COINS]) = [True, True, False]
TETHERED: constant(bool[N_COINS]) = [False, False, True]
FEE_DENOMINATOR: constant(uint256) = 10 ** 10
PRECISION: constant(uint256) = 10 ** 18 # The precision to convert to
PRECISION_MUL: constant(uint256[N_COINS]) = [convert(1, uint256), convert(1000000000000, uint256), convert(1000000000000, uint256)]
# PRECISION_MUL: constant(uint256[N_COINS]) = [
# PRECISION / convert(PRECISION, uint256), # DAI
# PRECISION / convert(10 ** 6, uint256), # USDC
# PRECISION / convert(10 ** 6, uint256)] # USDT
A: constant(uint256) = 2000 # 2 x amplification coefficient
@public
@constant
def get_D(xp: uint256[N_COINS]) -> uint256:
S: uint256 = 0
for _x in xp:
S += _x
if S == 0:
return 0
Dprev: uint256 = 0
D: uint256 = S
Ann: uint256 = A * N_COINS
for _i in range(255):
D_P: uint256 = D
for _x in xp:
D_P = D_P * D / (_x * N_COINS + 1) # +1 is to prevent /0
Dprev = D
D = (Ann * S + D_P * N_COINS) * D / ((Ann - 1) * D + (N_COINS + 1) * D_P)
# Equality with the precision of 1
if D > Dprev:
if D - Dprev <= 1:
break
else:
if Dprev - D <= 1:
break
return D
@public
@constant
def get_I(xp: uint256[N_COINS]) -> int128:
S: uint256 = 0
for _x in xp:
S += _x
if S == 0:
return 0
Dprev: uint256 = 0
D: uint256 = S
Ann: uint256 = A * N_COINS
for _i in range(255):
D_P: uint256 = D
for _x in xp:
D_P = D_P * D / (_x * N_COINS + 1) # +1 is to prevent /0
Dprev = D
D = (Ann * S + D_P * N_COINS) * D / ((Ann - 1) * D + (N_COINS + 1) * D_P)
# Equality with the precision of 1
if D > Dprev:
if D - Dprev <= 1:
return _i
else:
if Dprev - D <= 1:
return _i
result: int128 = 1000
return result