-
Notifications
You must be signed in to change notification settings - Fork 6
/
Swap.py
212 lines (181 loc) · 8.76 KB
/
Swap.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import argparse
import matplotlib.pyplot as plt
# reference implementation from Curve repo
# https://github.com/curvefi/curve-contract/blob/master/tests/pools/common/integration/test_curve.py
from simulation import Curve
# 100 for oUSD
# 200 for mUSD, UST
# 500 for LUSD
# 1500 for frax
# 2000 for Mim
# 5000 for 3CRV
As = [100, 200, 500, 1500, 2000, 5000]
parser = argparse.ArgumentParser(description='-s slippage(%) / -p poolBalance(%)')
parser.add_argument('-s', type=float,
help='slippage(%)')
parser.add_argument('-p', type=float,
help='PoolBalance(%)')
parser.add_argument('--bonus', default="False",
help='isBonus(%)')
parser.add_argument('-a', type=float, required=False,
help='percentageOfTradeSize')
percentageOfEachTrade = 1
# Total poolSize
Dep = 10000000000
# set fee = 0 for stimulation purpose, set fee to 4000000 for prod
fee = 0
# two coins
n = 2
# 800 steps to push the balance with each step trade 0.1%, change for simulation accuracy
steps = [Dep/1000, 800]
# steps = [tradeSize, number of trade]
def simulate(Curve, steps, swapAmount, is_bonus):
# tokens = Dep so it has 1LP = 1 Dep relationship
tradeSize = steps[0]
poolBalances = []
slippages = []
for i in range(steps[1]):
old_balances = 100 * Curve.x[0] / sum(Curve.x)
if is_bonus == "False":
output = Curve.dyWfee(0, 1, swapAmount)
slippage = float(100 * (swapAmount - output) / swapAmount)
elif is_bonus == "True":
output = Curve.dyWfee(1, 0, swapAmount)
slippage = float(100 * (output - swapAmount) / swapAmount)
else:
print("--bonus is either False or True")
break
poolBalances.append(old_balances)
slippages.append(slippage)
Curve.exchange(0,1, tradeSize)
# return the beginning pool %, the sliipage(%)
return poolBalances, slippages
if __name__ == "__main__":
args = parser.parse_args()
if args.a != None:
percentageOfEachTrade = args.a
else:
percentageOfEachTrade = 1
swapAmount = Dep * percentageOfEachTrade / 100
if args.bonus == "False":
if args.s and args.p == None:
# slippage threshold(%) for poolBalance label on the plot
threshold = args.s
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('Pool Balance(%)') # Add an x-label to the axes.
ax.set_ylabel('Slippage(%)') # Add a y-label to the axes.
ax.set_title('Swap Slippage of Size({}% TVL) on Pool Ratio on Curve Stablepool'.format(percentageOfEachTrade))
print("Swap -----", swapAmount)
thresholds = []
for i in As:
print("A : ", i)
curve = Curve(i, Dep,n, fee=fee, tokens=Dep)
x, y = simulate(curve, steps, swapAmount, "False")
ax.plot(x,y, label='A={}'.format(i))
# find the balance based on a threshold slippage (%)
index = [ n for n,i in enumerate(y) if i>threshold ][0]
thresholds.append(x[index])
print( "Pool Balance: ",x[index], " Based on slippage at {} %".format(threshold))
AsText = ["A={}".format(A) for A in As]
thresholdsText = ['{}%'.format(format(t, ".1f")) for t in thresholds]
legend1 = plt.legend(AsText)
legend2 = plt.legend(thresholdsText, loc=1)
ax.add_artist(legend1)
ax.add_artist(legend2)
ax.axhline(y=threshold, ls='--', color='r', label="{}% sliipage".format(threshold))
ax.legend()
plt.show()
elif args.p and args.s == None:
threshold = args.p
# poolBalance(%) threshold label on the plot for slippage
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('Pool Balance(%)') # Add an x-label to the axes.
ax.set_ylabel('Slippage(%)') # Add a y-label to the axes.
ax.set_title('Swap Slippage of Size({}% TVL) on Pool Ratio on Curve Stablepool'.format(percentageOfEachTrade))
print("Swap -----", swapAmount)
thresholds = []
for i in As:
print("A : ", i)
curve = Curve(i, Dep,n, fee=fee, tokens=Dep)
x, y = simulate(curve, steps, swapAmount, "False")
ax.plot(x,y, label='A={}'.format(i))
# find the slippage (%) given a balance(%) threshold
index = [ n for n,i in enumerate(x) if i>threshold ][0]
thresholds.append(y[index])
print( "Slippage: ", y[index], " Based on poolBlaance at {} %".format(threshold))
AsText = ["A={}".format(A) for A in As]
thresholdsText = ['{}%'.format(format(t, ".3f")) for t in thresholds]
legend1 = plt.legend(AsText)
legend2 = plt.legend(thresholdsText, loc=1)
ax.add_artist(legend1)
ax.add_artist(legend2)
ax.axvline(x=threshold, ls='--', color='r', label="{}% poolBalance".format(threshold))
#ax.axhline(y=threshold, ls='--', color='r', label="{}% sliipage".format(threshold))
ax.legend()
plt.show()
else:
print("Usage: python Swap.py [-s] slippage(%) OR [-p] poolBalance(%) | (optional) --bonus [-a] TVL(%)")
elif args.bonus == "True":
if args.s and args.p == None:
# slippage threshold(%) for poolBalance label on the plot
threshold = args.s
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('Pool Balance(%)') # Add an x-label to the axes.
ax.set_ylabel('Bonus(%)') # Add a y-label to the axes.
ax.set_title('Swap Bonus of Size({}% TVL) on Pool Ratio on Curve Stablepool'.format(percentageOfEachTrade))
print("Swap -----", swapAmount)
thresholds = []
for i in As:
print("A : ", i)
curve = Curve(i, Dep,n, fee=fee, tokens=Dep)
x, y = simulate(curve, steps, swapAmount, "True")
ax.plot(x,y, label='A={}'.format(i))
# find the balance based on a threshold slippage (%)
index = [ n for n,i in enumerate(y) if i>threshold ][0]
thresholds.append(x[index])
print( "Pool Balance: ",x[index], " Based on Bonus at {} %".format(threshold))
AsText = ["A={}".format(A) for A in As]
thresholdsText = ['{}%'.format(format(t, ".1f")) for t in thresholds]
legend1 = plt.legend(AsText)
legend2 = plt.legend(thresholdsText, loc=1)
ax.add_artist(legend1)
ax.add_artist(legend2)
ax.axhline(y=threshold, ls='--', color='r', label="{}% bonus".format(threshold))
ax.legend()
plt.show()
elif args.p and args.s == None:
threshold = args.p
# poolBalance(%) threshold label on the plot for slippage
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlabel('Pool Balance(%)') # Add an x-label to the axes.
ax.set_ylabel('Bonus(%)') # Add a y-label to the axes.
ax.set_title('Swap Bonus of Size({}% TVL) on Pool Ratio on Curve Stablepool'.format(percentageOfEachTrade))
print("Swap -----", swapAmount)
thresholds = []
for i in As:
print("A : ", i)
curve = Curve(i, Dep,n, fee=fee, tokens=Dep)
x, y = simulate(curve, steps, swapAmount, "True")
ax.plot(x,y, label='A={}'.format(i))
# find the slippage (%) given a balance(%) threshold
index = [ n for n,i in enumerate(x) if i>threshold ][0]
thresholds.append(y[index])
print( "Bonus: ", y[index], " Based on poolBlaance at {} %".format(threshold))
AsText = ["A={}".format(A) for A in As]
thresholdsText = ['{}%'.format(format(t, ".3f")) for t in thresholds]
legend1 = plt.legend(AsText)
legend2 = plt.legend(thresholdsText, loc=1)
ax.add_artist(legend1)
ax.add_artist(legend2)
ax.axvline(x=threshold, ls='--', color='r', label="{}% poolBalance".format(threshold))
#ax.axhline(y=threshold, ls='--', color='r', label="{}% sliipage".format(threshold))
ax.legend()
plt.show()
else:
print("Usage: python Swap.py [-s] slippage(%) OR [-p] poolBalance(%) | (optional) --bonus [-a] TVL(%)")
else:
print("--bonus is either False or True")