-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdex_reward_sim.py
More file actions
292 lines (263 loc) · 9.29 KB
/
Copy pathdex_reward_sim.py
File metadata and controls
292 lines (263 loc) · 9.29 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
"""
dex_reward_sim.py
Deterministic simulation of hybrid-DEX reward model (per your spec).
Outputs:
- CSVs: scenario_<i>_nodes.csv, scenario_<i>_lps.csv
- PNGs: scenario_<i>_hist_node.png, scenario_<i>_lorenz_node.png
- Printed summary tables to stdout
Dependencies: numpy, pandas, matplotlib, scipy
Install: pip install numpy pandas matplotlib scipy
Run: python dex_reward_sim.py
"""
import os
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# RNG seed for reproducibility
SEED = 42
np.random.seed(SEED)
# Simulation constants
MATCHES = 100_000
r0 = 0.001
beta = 0.2
V0 = 1000.0
EPS = 1e-6
OUT_DIR = "sim_outputs"
os.makedirs(OUT_DIR, exist_ok=True)
def fee_rate(V):
return r0 * (1.0 + beta * math.log10(1.0 + V / V0))
def fee_pool(V):
fr = fee_rate(V)
return V * fr
def gini(array):
x = np.array(array, dtype=float)
if x.size == 0:
return 0.0
# Negative/zero safe
x = np.clip(x, 0.0, None)
if x.sum() == 0:
return 0.0
x = np.sort(x)
n = x.size
index = np.arange(1, n+1)
return (2.0 * np.sum(index * x) / (n * x.sum()) - (n + 1) / n)
def lorenz_curve(x):
x = np.array(x, dtype=float)
x = np.clip(x, 0.0, None)
total = x.sum()
if total == 0:
return np.linspace(0, 1, len(x)+1), np.linspace(0, 1, len(x)+1)
xs = np.sort(x)
cum = np.cumsum(xs) / total
cum = np.concatenate(([0.0], cum))
p = np.linspace(0.0, 1.0, len(cum))
return p, cum
def summary_stats(rewards):
rewards = np.array(rewards, dtype=float)
total = rewards.sum()
mean = rewards.mean()
std = rewards.std()
g = gini(rewards)
never = np.sum(rewards == 0) / rewards.size
N = rewards.size
topk = max(1, int(math.ceil(0.10 * N)))
top_share = np.sum(np.sort(rewards)[-topk:]) / total if total > 0 else 0.0
return {
"total": total,
"mean": mean,
"std": std,
"gini": g,
"fraction_never_won": never,
"top10%_share": top_share
}
def run_scenario(scenario_id, nodes_uptime, lp_balances, V_sampler):
N = len(nodes_uptime)
M = len(lp_balances)
nodes_uptime = np.array(nodes_uptime, dtype=float)
lp_balances = np.array(lp_balances, dtype=float)
if lp_balances.sum() <= 0:
raise ValueError("LP balances must sum > 0")
# state
last_reward_at = np.zeros(N, dtype=int) # per spec initial 0
node_rewards = np.zeros(N, dtype=float)
node_wins_count = np.zeros(N, dtype=int)
node_win_times = [[] for _ in range(N)]
lp_rewards = np.zeros(M, dtype=float)
# For speed precompute per-match when V fixed
fixed_V = None
if callable(V_sampler):
pass
else:
fixed_V = float(V_sampler)
F = fee_pool(fixed_V)
node_pool = 0.5 * F
lp_pool = node_pool
for t in range(1, MATCHES + 1):
# determine V for this match
if fixed_V is None:
V = V_sampler()
F = fee_pool(V)
node_pool = 0.5 * F
lp_pool = node_pool
# Select winner per rules
max_uptime = nodes_uptime.max()
candidates = np.where(np.abs(nodes_uptime - max_uptime) <= EPS)[0]
if candidates.size == 1:
winner = candidates[0]
else:
time_wait = t - last_reward_at[candidates]
# choose candidate with max time_wait; if multiple, pick first (deterministic)
idx = np.argmax(time_wait)
winner = candidates[idx]
# award
last_reward_at[winner] = t
node_rewards[winner] += node_pool
node_wins_count[winner] += 1
node_win_times[winner].append(t)
# LP distribution pro-rata
lp_share = lp_pool * (lp_balances / lp_balances.sum())
lp_rewards += lp_share
# metrics
node_stats = summary_stats(node_rewards)
lp_stats = summary_stats(lp_rewards)
# additional: average time between rewards for nodes that won
intervals = []
for wins in node_win_times:
if len(wins) >= 2:
diffs = np.diff(wins)
intervals.append(np.mean(diffs))
avg_time_between_rewards = float(np.mean(intervals)) if intervals else float('nan')
# create outputs
nodes_df = pd.DataFrame({
"node_id": np.arange(N),
"uptime": nodes_uptime,
"total_reward": node_rewards,
"wins": node_wins_count,
"last_reward_at": last_reward_at
})
nodes_df.to_csv(os.path.join(OUT_DIR, f"scenario_{scenario_id}_nodes.csv"), index=False)
lps_df = pd.DataFrame({
"lp_id": np.arange(M),
"balance": lp_balances,
"total_reward": lp_rewards
})
lps_df.to_csv(os.path.join(OUT_DIR, f"scenario_{scenario_id}_lps.csv"), index=False)
# plots: node histogram
plt.figure(figsize=(6,4))
plt.hist(node_rewards, bins=50, log=True, color='C0', edgecolor='black')
plt.title(f"Scenario {scenario_id} Node Reward Histogram (log scale)")
plt.xlabel("Total reward")
plt.ylabel("Count (log)")
plt.tight_layout()
plt.savefig(os.path.join(OUT_DIR, f"scenario_{scenario_id}_hist_node.png"))
plt.close()
# Lorenz plot
p, cum = lorenz_curve(node_rewards)
plt.figure(figsize=(6,4))
plt.plot(p, cum, label='Lorenz curve')
plt.plot([0,1], [0,1], '--', color='gray', label='Equality')
plt.title(f"Scenario {scenario_id} Node Lorenz Curve (Gini={node_stats['gini']:.3f})")
plt.xlabel("Cumulative share of nodes")
plt.ylabel("Cumulative share of rewards")
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(OUT_DIR, f"scenario_{scenario_id}_lorenz_node.png"))
plt.close()
# print summary
print(f"\n--- Scenario {scenario_id} Summary ---")
print(f"N nodes = {N}, M LPs = {M}, MATCHES = {MATCHES}")
print(f"Total fees generated (approx): {node_stats['total'] + lp_stats['total']:.2f}")
print("Node metrics:")
for k,v in node_stats.items():
print(f" {k}: {v}")
print("LP metrics:")
for k,v in lp_stats.items():
print(f" {k}: {v}")
print(f"Avg time between rewards (winners): {avg_time_between_rewards:.2f} matches")
# top 10% share
print(f"Top-10% node share: {node_stats['top10%_share']:.3f}")
return {
"nodes_df": nodes_df,
"lps_df": lps_df,
"node_stats": node_stats,
"lp_stats": lp_stats
}
def scenario_fixed_V(N, uptime_values, lp_count, lp_balance_sampler, V_fixed=10000.0, scenario_id=1):
nodes_uptime = np.array(uptime_values)
lp_balances = lp_balance_sampler(lp_count)
return run_scenario(scenario_id, nodes_uptime, lp_balances, V_fixed)
def scenario_random_V(N, uptime_values, lp_count, lp_balance_sampler, scenario_id=5):
nodes_uptime = np.array(uptime_values)
lp_balances = lp_balance_sampler(lp_count)
# lognormal median 1000 => mu = ln(1000)
mu = math.log(1000.0)
sigma = 1.2
rng = np.random.default_rng(SEED + scenario_id)
def sample_V():
return float(rng.lognormal(mean=mu, sigma=sigma))
return run_scenario(scenario_id, nodes_uptime, lp_balances, sample_V)
def lp_equal_balances(count, val=100.0):
return np.full(count, val, dtype=float)
def lp_exponential(count, mean=100.0):
rng = np.random.default_rng(SEED + count)
return rng.exponential(scale=mean, size=count)
# Run scenarios
if __name__ == "__main__":
# Scenario 1
print("Running Scenario 1 ...")
sc1 = scenario_fixed_V(
N=5,
uptime_values=[0.99]*5,
lp_count=10,
lp_balance_sampler=lambda c: lp_equal_balances(c, 100.0),
V_fixed=10000.0,
scenario_id=1
)
# Scenario 2
print("Running Scenario 2 ...")
uptimes_s2 = np.array([0.99]*10 + [0.1]*40)
sc2 = scenario_fixed_V(
N=50,
uptime_values=uptimes_s2,
lp_count=50,
lp_balance_sampler=lambda c: lp_exponential(c, mean=100.0),
V_fixed=10000.0,
scenario_id=2
)
# Scenario 3
print("Running Scenario 3 ...")
uptimes_s3 = np.array([0.99]*50 + [0.01]*450)
sc3 = scenario_fixed_V(
N=500,
uptime_values=uptimes_s3,
lp_count=200,
lp_balance_sampler=lambda c: lp_exponential(c, mean=100.0),
V_fixed=10000.0,
scenario_id=3
)
# Scenario 4 (Sybil)
print("Running Scenario 4 ...")
uptimes_s4 = np.array([0.9]*100 + [0.01]*400)
sc4 = scenario_fixed_V(
N=500,
uptime_values=uptimes_s4,
lp_count=200,
lp_balance_sampler=lambda c: lp_exponential(c, mean=100.0),
V_fixed=10000.0,
scenario_id=4
)
# Scenario 5 (varied trade volume lognormal)
print("Running Scenario 5 ...")
uptimes_s5 = np.array([0.8]*25)
sc5 = scenario_random_V(
N=25,
uptime_values=uptimes_s5,
lp_count=25,
lp_balance_sampler=lambda c: lp_equal_balances(c, 100.0),
scenario_id=5
)
print("\nSimulation completed. Outputs written to directory:", OUT_DIR)
print("CSV per-scenario files: scenario_<i>_nodes.csv, scenario_<i>_lps.csv")
print("Plots: scenario_<i>_hist_node.png, scenario_<i>_lorenz_node.png")