-
Notifications
You must be signed in to change notification settings - Fork 6
/
logic.py
277 lines (219 loc) · 8.68 KB
/
logic.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
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
266
267
268
269
270
271
272
273
274
275
276
277
import random
from typing import Callable, List, Any
import numpy as np
from scipy.stats import norm, poisson # type: ignore
from cadCAD.tools.preparation import sweep_cartesian_product # type: ignore
from subspace_model.const import (
BLOCKS_PER_MONTH,
BLOCKS_PER_YEAR,
DAY_TO_SECONDS,
ISSUANCE_FOR_FARMERS
)
from subspace_model.metrics import (
earned_minus_burned_supply,
earned_supply,
issued_supply,
total_supply,
)
from subspace_model.types import (
StochasticFunction,
SubspaceModelParams,
SubspaceModelState,
SubsidyComponent,
)
def DEFAULT_SLASH_FUNCTION(params: SubspaceModelParams, state: SubspaceModelState):
return state["staking_pool_balance"] * 0.001 # HACK
def NORMAL_GENERATOR(mu: float, sigma: float) -> StochasticFunction:
np.random.seed()
return lambda p, s: norm.rvs(mu, sigma, random_state=np.random.RandomState())
def NORMAL_INSTANTANEOUS_SHOCK_GENERATOR(
mu: float, sigma: float, N: int
) -> StochasticFunction:
np.random.seed()
def generator(p, s):
value = norm.rvs(mu, sigma, random_state=np.random.RandomState())
if s["days_passed"] % (N * 7) == 0:
if np.random.choice([0, 1]):
value *= 10
else:
value /= 10
return value
return generator
def NORMAL_SUSTAINED_SHOCK_GENERATOR(
mu: float, sigma: float, N: int, M: int
) -> StochasticFunction:
np.random.seed()
def generator(p, s):
value = norm.rvs(mu, sigma, random_state=np.random.RandomState())
if s["days_passed"] % (N * 7) < M:
if (N * 7) % 2:
value *= 10
else:
value /= 10
return value
return generator
def POISSON_GENERATOR(mu: float) -> StochasticFunction:
np.random.seed()
return lambda p, s: poisson.rvs(mu, random_state=np.random.RandomState())
def POSITIVE_INTEGER(generator: StochasticFunction) -> StochasticFunction:
return lambda p, s: max(0, int(generator(p, s)))
def MAGNITUDE(generator: StochasticFunction) -> StochasticFunction:
return lambda p, s: min(1, max(0, generator(p, s)))
def predictable_trajectory(mean: float, **params: Any) -> Callable:
mu: float = mean
sigma: float = 0.3 * mu
generator: Callable = NORMAL_GENERATOR(mu, sigma)
return generator
def high_volatility_trajectory(mean: float, **params: Any) -> Callable:
mu: float = mean
sigma: float = 5 * mu
generator: Callable = NORMAL_GENERATOR(mu, sigma)
return generator
def predictable_trajectory_with_instantaneous_shocks(
mean: float, **params: Any
) -> Callable:
mu: float = mean
sigma: float = 0.3 * mu
generator: Callable = NORMAL_INSTANTANEOUS_SHOCK_GENERATOR(
mu, sigma, N=params.get("N", 13)
)
return generator
def predictable_trajectory_with_sustained_shocks(
mean: float, **params: Any
) -> Callable:
mu: float = mean
sigma: float = 0.3 * mu
generator: Callable = NORMAL_SUSTAINED_SHOCK_GENERATOR(
mu, sigma, N=params.get("N", 13), M=params.get("M", 7)
)
return generator
def SCENARIO_GROUPS(means: List[float], N: int = 13, M: int = 7) -> List[Callable]:
# Subsample battery to conserve cardinality of scenarios parameter space
groups: List[Callable] = random.sample(
[
predictable_trajectory,
high_volatility_trajectory,
predictable_trajectory_with_instantaneous_shocks,
predictable_trajectory_with_sustained_shocks,
],
1, # XXX This value can range from 1 to 4 to scale up the cardinality of scenarios
)
results: List[Callable] = []
for mean in means:
if mean != 0:
for group in groups:
results.append(group(mean))
else:
results.append(lambda p, s: 0)
return results
SUPPLY_ISSUED = issued_supply
SUPPLY_EARNED = earned_supply
SUPPLY_EARNED_MINUS_BURNED = earned_minus_burned_supply
SUPPLY_TOTAL = total_supply
REFERENCE_SUBSIDY_CONSTANT_SINGLE_COMPONENT = [
SubsidyComponent(0, 2 * BLOCKS_PER_YEAR, 10_000,
10_000 / (2 * BLOCKS_PER_YEAR)),
]
REFERENCE_SUBSIDY_HYBRID_SINGLE_COMPONENT = [
SubsidyComponent(0, BLOCKS_PER_MONTH, 10_000, 1_000 / BLOCKS_PER_MONTH),
]
REFERENCE_SUBSIDY_HYBRID_TWO_COMPONENTS = [
SubsidyComponent(0, BLOCKS_PER_MONTH, 5_000, 1_000 / BLOCKS_PER_MONTH),
SubsidyComponent(
6 * BLOCKS_PER_MONTH, 7 * BLOCKS_PER_MONTH, 5_000, 1_000 / BLOCKS_PER_MONTH
),
]
def MAINNET_REFERENCE_SUBSIDY_COMPONENTS():
component_1_start_days = [0, 14, 30]
component_2_start_days = [0, 14, 30]
component_1_initial_subsidy_duration = [0]
component_1_initial_subsidies = [1, 4, 7]
component_1_maximum_cumulative_subsidies = [
0.1 * ISSUANCE_FOR_FARMERS,
0.3 * ISSUANCE_FOR_FARMERS,
0.5 * ISSUANCE_FOR_FARMERS]
component_2_initial_subsidy_duration = [
6 * (365.25 / 12),
12 * (365.25 / 12),
24 * (365.25 / 12),
48 * (365.25 / 12),
]
component_2_initial_subsidies = [1, 4, 7]
component_2_maximum_cumulative_subsidies = [0.1 * ISSUANCE_FOR_FARMERS,
0.3 * ISSUANCE_FOR_FARMERS,
0.5 * ISSUANCE_FOR_FARMERS]
cartesian_product = sweep_cartesian_product(
{
"component_1_start_days": component_1_start_days,
"component_1_initial_subsidy_duration": component_1_initial_subsidy_duration,
"component_1_initial_subsidies": component_1_initial_subsidies,
"component_1_maximum_cumulative_subsidies": component_1_maximum_cumulative_subsidies,
"component_2_start_days": component_2_start_days,
"component_2_initial_subsidy_duration": component_2_initial_subsidy_duration,
"component_2_initial_subsidies": component_2_initial_subsidies,
"component_2_maximum_cumulative_subsidies": component_2_maximum_cumulative_subsidies,
} # type: ignore
)
components = [
(
SubsidyComponent(
start1,
duration1,
maximum_cumulative_subsidy1,
initial_subsidy1,
),
SubsidyComponent(
start2,
duration2,
maximum_cumulative_subsidy2,
initial_subsidy2,)
)
for start1, duration1, initial_subsidy1, maximum_cumulative_subsidy1, start2, duration2, initial_subsidy2, maximum_cumulative_subsidy2 in zip(
cartesian_product['component_1_start_days'],
cartesian_product['component_1_initial_subsidy_duration'],
cartesian_product['component_1_initial_subsidies'],
cartesian_product['component_1_maximum_cumulative_subsidies'],
cartesian_product['component_2_start_days'],
cartesian_product['component_2_initial_subsidy_duration'],
cartesian_product['component_2_initial_subsidies'],
cartesian_product['component_2_maximum_cumulative_subsidies'],
)]
return components
DEFAULT_REFERENCE_SUBSIDY_COMPONENTS = MAINNET_REFERENCE_SUBSIDY_COMPONENTS()[
-1]
def TRANSACTION_COUNT_PER_DAY_FUNCTION_CONSTANT_UTILIZATION_50(
params: SubspaceModelParams, state: SubspaceModelState
) -> float:
average_transaction_size = state["average_transaction_size"]
max_size = (
params["max_block_size"] * DAY_TO_SECONDS *
params["block_time_in_seconds"]
)
# Hold a constant utilization rate of 0.5
transaction_count = 0.5 * max_size / average_transaction_size
return transaction_count
def TRANSACTION_COUNT_PER_DAY_FUNCTION_GROWING_UTILIZATION_TWO_YEARS(
params: SubspaceModelParams, state: SubspaceModelState
) -> float:
days_passed = state["days_passed"]
average_transaction_size = state["average_transaction_size"]
max_size = (
params["max_block_size"] * DAY_TO_SECONDS *
params["block_time_in_seconds"]
)
utilization = min(days_passed / (2 * 365), 1)
# Grow utilization rate from 0 to 1 over 2 years
transaction_count = utilization * max_size / average_transaction_size
return transaction_count
def TRANSACTION_COUNT_PER_DAY_FUNCTION_FROM_UTILIZATION_RATIOS(
params: SubspaceModelParams, state: SubspaceModelState
) -> float:
max_size = (
params["max_block_size"] * DAY_TO_SECONDS *
params["block_time_in_seconds"]
)
transaction_volume = max_size * state["block_utilization"]
transaction_count = transaction_volume / state["average_transaction_size"]
return transaction_count
def WEEKLY_VARYING(params: SubspaceModelParams, state: SubspaceModelState):
return 2 + np.sin(2 * np.pi * state["days_passed"] / 7)