-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithmicStablecoinSimulation.m
266 lines (224 loc) · 11 KB
/
AlgorithmicStablecoinSimulation.m
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
classdef AlgorithmicStablecoinSimulation < handle
% Algorithmic stablecoin system simulation
properties
T_a Token
T_b Token
USDC Token
FreeT_a double
TotalT_a double
FreeT_b double
TotalT_b double
InitialT_bPrice double
PoolStable LiquidityPool
PoolVolatile LiquidityPool
VirtualPool VirtualLiquidityPool = OriginalVirtualLiquidityPool(0, 0, 0, 0, 0)
NumberOfIterations int64
ExpRate double = 0.0001
Sigma double = 0.0001
PoolFee double = 0.003
WalletDistribution_stable WalletBalanceGenerator
WalletDistribution_volatile WalletBalanceGenerator
PurchaseGenerator_poolStable PurchaseGenerator
PurchaseGenerator_poolVolatile PurchaseGenerator
ReserveGenerator ReservePurchaseGenerator
VolatilityArray double
end
methods
function simulation = AlgorithmicStablecoinSimulation(varargin)
simulation.T_a = varargin{1};
simulation.T_b = varargin{2};
simulation.InitialT_bPrice = varargin{3};
simulation.TotalT_a = varargin{4};
simulation.TotalT_b = varargin{5};
simulation.FreeT_a = varargin{6};
simulation.FreeT_b = varargin{7};
simulation.VirtualPool = varargin{8};
simulation.NumberOfIterations = varargin{9};
if nargin > 9
simulation.ExpRate = varargin{10};
if nargin > 10
simulation.PoolFee = varargin{11};
if nargin > 11
simulation.Sigma = varargin{12};
end
end
end
simulation.initializePools();
simulation.initializeWalletDistributions(simulation.ExpRate);
simulation.initializePurchaseGenerators();
if nargin > 12
reserves = simulation.TotalT_a * varargin{13};
totalUSDCReserves = reserves * 0.5;
totalStablecoinReserves = reserves * 0.5;
simulation.ReserveGenerator = ReservePurchaseGenerator(simulation.PoolStable, ...
totalUSDCReserves, totalStablecoinReserves, 0.95);
end
end
function initializePools(self)
% create 2 pools (T_a/USDC and T_b/USDC) and one virtual
% pool (T_a/T_b) for the seigniorage process
Q_a = self.TotalT_a - self.FreeT_a;
Q_b = self.TotalT_b - self.FreeT_b;
Q_c = self.InitialT_bPrice * Q_b;
self.USDC = Token("USDC", true, false, 1);
self.PoolStable = LiquidityPool(self.T_a, self.USDC, Q_a, Q_a, self.PoolFee);
self.PoolVolatile = LiquidityPool(self.T_b, self.USDC, Q_b, Q_c, self.PoolFee);
end
function initializeWalletDistributions(self, expRate)
% initialize 2 wallet distributions
self.WalletDistribution_stable = WalletBalanceGenerator(self.TotalT_a, expRate);
self.WalletDistribution_volatile = WalletBalanceGenerator(self.TotalT_b, expRate);
end
function initializePurchaseGenerators(self)
% initialize 2 random purchase generators
initialProbability = 0.5;
self.PurchaseGenerator_poolStable = PurchaseGenerator(self.PoolStable, ...
self.NumberOfIterations, initialProbability, self.Sigma, self.WalletDistribution_stable);
self.PurchaseGenerator_poolVolatile = PurchaseGenerator(self.PoolVolatile, ...
self.NumberOfIterations, initialProbability, self.Sigma, self.WalletDistribution_volatile);
end
function [T_aPrices, T_bPrices, probA, probB, delta, ...
totalT_aSupply, totalT_bSupply, freeT_a, freeT_b] = runSimulation(self)
T_aPrices = zeros(self.NumberOfIterations, 1);
T_bPrices = zeros(self.NumberOfIterations, 1);
delta = zeros(self.NumberOfIterations, 1);
totalT_aSupply = zeros(self.NumberOfIterations, 1);
totalT_bSupply = zeros(self.NumberOfIterations, 1);
freeT_a = zeros(self.NumberOfIterations, 1);
freeT_b = zeros(self.NumberOfIterations, 1);
% main loop
for i = 1:self.NumberOfIterations
self.stablePoolRandomPurchase();
self.volatilePoolRandomPurchase();
self.virtualPoolArbitrage();
if (i == 0) && self.VirtualPool.Delta > 0
disp("STOP");
end
self.VirtualPool.restoreDelta(self.PoolStable.getTokenPrice(self.T_a, self.USDC.PEG));
self.VirtualPool.updateVolatileTokenPrice(...
self.PoolVolatile.getTokenPrice(...
self.T_b, self.USDC.PEG));
if ~isempty(self.ReserveGenerator)
self.ReserveGenerator.reserveIntervention();
end
if ~isempty(self.VolatilityArray)
self.updateVolatility(self.VolatilityArray(i));
end
delta(i) = self.VirtualPool.Delta;
T_aPrices(i) = self.PoolStable.getTokenPrice(self.T_a, self.USDC.PEG);
T_bPrices(i) = self.PoolVolatile.getTokenPrice(self.T_b, self.USDC.PEG);
totalT_aSupply(i) = self.TotalT_a;
totalT_bSupply(i) = self.TotalT_b;
freeT_a(i) = self.FreeT_a;
freeT_b(i) = self.FreeT_b;
end
probA = self.PurchaseGenerator_poolStable.P;
probB = self.PurchaseGenerator_poolVolatile.P;
end
function stablePoolRandomPurchase(self)
% stable pool random purchase
[token, quantity] = self.PurchaseGenerator_poolStable.rndPurchase(self.FreeT_a, self.TotalT_a);
T_aInPool_old = self.PoolStable.Q_a;
self.PoolStable.swap(token, quantity);
self.updateFreeT_a(T_aInPool_old);
% exchange information about the crisis scenario between
% purchase generators
if (self.PurchaseGenerator_poolStable.CrisisScenario > 0)
self.PurchaseGenerator_poolVolatile.CrisisScenario = self.PurchaseGenerator_poolStable.CrisisScenario;
elseif (self.PurchaseGenerator_poolVolatile.CrisisScenario > 0)
self.PurchaseGenerator_poolVolatile.CrisisScenario = 0;
end
end
function volatilePoolRandomPurchase(self)
% volatile pool random purchase
[token, quantity] = self.PurchaseGenerator_poolVolatile.rndPurchase(self.FreeT_b, self.TotalT_b);
T_bInPool_old = self.PoolVolatile.Q_a;
self.PoolVolatile.swap(token, quantity);
self.updateFreeT_b(T_bInPool_old);
end
function out = virtualPoolArbitrage(self)
% virtual pool arbitrage
% The arbitrage operation is deterministic.
% 1. a wallet is ramdomly choosen
% 2. if there is an arbitrage opportunity, it is exploited
% 3. the maximum amount of tokens used in the operation depends
% on the balance of the wallet
out = 0;
[token, quantity] = self.getQuantityRelatedToMaxYield();
if quantity > 0
if token.is_equal(self.T_a)
walletBalance = self.WalletDistribution_stable.rndWalletBalance();
[t, x] = self.PoolStable.swap(self.USDC, min(quantity, walletBalance));
self.TotalT_a = self.TotalT_a - x;
[t, x] = self.VirtualPool.swap(t, x);
self.TotalT_b = self.TotalT_b + x;
[~, out] = self.PoolVolatile.swap(t, x);
else
walletBalance = self.WalletDistribution_volatile.rndWalletBalance();
[t, x] = self.PoolVolatile.swap(self.USDC, min(quantity, walletBalance));
self.TotalT_b = self.TotalT_b - x;
[t, x] = self.VirtualPool.swap(t, x);
self.TotalT_a = self.TotalT_a + x;
[~, out] = self.PoolStable.swap(t, x);
end
end
end
function [token, quantity] = getQuantityRelatedToMaxYield(self)
x = 1;
q = 0;
options = optimset('Display', 'off');
yield1 = self.getArbitrageYield1(x);
if (yield1 > 0)
q = fminsearch(@(x) (-1)*self.getArbitrageYield1(x), ...
1, options);
end
yield2 = self.getArbitrageYield2(x);
if (yield2 > 0)
q = fminsearch(@(x) (-1)*self.getArbitrageYield2(x), ...
1, options);
end
if(yield1 > yield2)
token = self.T_a;
else
token = self.T_b;
end
quantity = q;
end
function yield = getArbitrageYield1(self, quantity)
if quantity > 0
x = self.PoolStable.computeSwapValue(self.USDC, quantity);
y = self.VirtualPool.computeSwapValue(self.T_a, x);
USDC_out = self.PoolVolatile.computeSwapValue(self.T_b, y);
yield = USDC_out - quantity;
else
yield = 0;
end
end
function yield = getArbitrageYield2(self, quantity)
if quantity > 0
x = self.PoolVolatile.computeSwapValue(self.USDC, quantity);
y = self.VirtualPool.computeSwapValue(self.T_b, x);
USDC_out = self.PoolStable.computeSwapValue(self.T_a, y);
yield = USDC_out - quantity;
else
yield = 0;
end
end
function updateFreeT_a(self, Q_a_prev)
self.FreeT_a = self.FreeT_a - (self.PoolStable.Q_a - Q_a_prev);
end
function updateFreeT_b(self, Q_b_prev)
self.FreeT_b = self.FreeT_b - (self.PoolVolatile.Q_a - Q_b_prev);
end
function setVolatilityArray(self, sigma)
if length(sigma) ~= self.NumberOfIterations
error("VolatilityArray length must be equal to NumberOfIteration");
end
self.VolatilityArray = sigma;
end
function updateVolatility(self, sigma)
self.PurchaseGenerator_poolStable.setNewSigma(sigma);
self.PurchaseGenerator_poolVolatile.setNewSigma(sigma);
end
end
end