forked from chrisckwong821/Curve_Finance_Slippage_Simulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Swap.py
128 lines (111 loc) · 4.73 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
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('-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]
def simulate(Curve, steps, swapAmount):
# 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)
output = Curve.dy(0,1, swapAmount)
# return the slippage(%), the beginning pool %
slippage = float(100 * (swapAmount - output) / swapAmount)
#print("slippage : " , slippage, " beginning_balances: ", old_balances)
poolBalances.append(old_balances)
slippages.append(slippage)
Curve.exchange(0,1, tradeSize)
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.s and args.p == None:
print(args.s)
# 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)
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:
print(args.p)
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('swapAmount Slippage on Pool Ratio on Curve Stablepool')
print("Withdrwaw -----", swapAmount)
thresholds = []
for i in As:
print("A : ", i)
curve = Curve(i, Dep,n, fee=fee, tokens=Dep)
x, y = simulate(curve, steps, swapAmount)
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(%)")