-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHome.py
901 lines (757 loc) · 44.6 KB
/
Home.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
import streamlit as st
import pandas as pd
import numpy as np
import plotly.graph_objs as go
import plotly.express as px
import networkx as nx
import utils
import fundingutils
from decimal import Decimal, getcontext
import decimal
# Page configuration
st.set_page_config(page_title="Matching Results", page_icon="assets/favicon.png", layout="wide")
# Initialize session state variables
if 'round_id' not in st.session_state:
st.session_state.round_id = None
if 'chain_id' not in st.session_state:
st.session_state.chain_id = None
# Handle URL parameters for round_id and chain_id
query_params_round_id = st.query_params.get_all('round_id')
if len(query_params_round_id) == 1 and not st.session_state.round_id:
st.session_state.round_id = query_params_round_id[0]
query_params_chain_id = st.query_params.get_all('chain_id')
if len(query_params_chain_id) == 1 and not st.session_state.chain_id:
st.session_state.chain_id = query_params_chain_id[0]
def display_recent_rounds():
# Fetch and process round data
rounds = utils.get_round_summary()
current_time = pd.Timestamp.now(tz='UTC')
rounds = rounds[(rounds['donations_end_time'].dt.tz_convert('UTC') < current_time) & (rounds['votes'] > 0)]
rounds = rounds.sort_values('donations_end_time', ascending=False)
# Create round links and prepare display data
rounds['Round Link'] = rounds.apply(lambda row: f"https://qf-calculator.fly.dev/?round_id={row['round_id']}&chain_id={row['chain_id']}", axis=1)
rounds_display = rounds[['round_name', 'Round Link', 'votes', 'uniqueContributors', 'amountUSD']]
# Configure column display
column_config = {
"Round Link": st.column_config.LinkColumn(
"Round Link",
display_text="Go to Round",
help="Click to view round details"
),
"votes": st.column_config.NumberColumn(
"Total Votes",
help="Total number of votes in the round"
),
"uniqueContributors": st.column_config.NumberColumn(
"Unique Contributors",
help="Number of unique contributors in the round"
),
"amountUSD": st.column_config.NumberColumn(
"Total Amount (USD)",
help="Total amount donated in USD",
format="$%.2f"
)
}
# Display the dataframe
st.header("Recent Rounds That Have Ended:")
st.dataframe(
#rounds_display.head(20),
rounds_display,
column_config=column_config,
hide_index=True
)
def validate_input():
"""Validate the presence of round_id and chain_id in the URL."""
if st.session_state.round_id is None or st.session_state.chain_id is None:
st.header("Oops! Something went wrong. You're not supposed to be here 🙈")
st.subheader("Please provide round_id and chain_id in the URL")
st.subheader('Example: https://qf-calculator.fly.dev/?round_id=23&chain_id=42161')
display_recent_rounds()
st.stop()
return st.session_state.round_id.lower(), int(st.session_state.chain_id)
@st.cache_resource(ttl=36000)
def load_scores_and_set_defense(chain_id, sybilDefense, unique_voters):
"""Load scores and set Sybil defense parameters based on chain and defense type."""
if chain_id == 43114: # AVALANCHE
scores = utils.load_avax_scores(unique_voters)
score_at_50_percent = score_at_100_percent = 25
sybilDefense = 'Avalanche Passport'
elif sybilDefense == 'true':
scores = utils.load_stamp_scores(unique_voters)
score_at_50_percent, score_at_100_percent = 15, 25
sybilDefense = 'Passport Stamps'
elif sybilDefense == 'passport-mbds':
scores = utils.load_passport_model_scores(unique_voters)
score_at_50_percent, score_at_100_percent = 25,50
sybilDefense = 'Passport Model Based Detection System'
else:
# If no Sybil defense is set, assign a default score of 1 to all voters
scores = pd.DataFrame({'address': unique_voters, 'rawScore': 1})
score_at_50_percent = score_at_100_percent = 0
sybilDefense = 'None'
return scores, score_at_50_percent, score_at_100_percent, sybilDefense
def check_round_existence(round_id, chain_id):
rounds = utils.get_round_summary()
rounds = rounds[(rounds['round_id'].str.lower() == round_id) & (rounds['chain_id'] == chain_id)] # FILTER BY ROUND_ID AND CHAIN_ID
if len(rounds) == 0:
st.write('## We could not find your round in our data.')
st.write('')
display_recent_rounds()
st.stop()
def load_data(round_id, chain_id):
"""Load and process data for the specified round and chain."""
blockchain_mapping = {1: "Ethereum", 10: "Optimism", 137: "Polygon", 250: "Fantom",
324: "ZKSync", 8453: "Base", 42161: "Arbitrum", 43114: "Avalanche",
534352: "Scroll", 1329: "SEI", 42220: "Celo", 1088: "Metis", 42: "Lukso" }
rounds = utils.get_round_summary()
# """
# TESTING
# """
# rt = rounds[rounds['sybilDefense'] == 'passport-mbds']
# st.write(rt[rt['uniqueContributors'] > 0].head(20))
rounds = rounds[(rounds['round_id'].str.lower() == round_id) & (rounds['chain_id'] == chain_id)] # FILTER BY ROUND_ID AND CHAIN_ID
if round_id == '11' and chain_id == 1329:
rounds = pd.DataFrame()
round_data = {
'round_name': ['Sei Creator Fund Round #3 - Developer Ecosystem'],
'amountUSD': [41352.29],
'votes': [7428],
'uniqueContributors': [2755],
'chain_id': [1329],
'round_id': ['11'],
'donations_end_time': ['October 17, 2024, 4:00 PM'],
'donations_start_time': ['October 3, 2024, 4:00 PM'],
'has_matching_cap': [True],
'matching_cap_amount': [10],
'matching_funds_available': [833333],
'token': ['0x0000000000000000000000000000000000000000'],
'has_min_donation_threshold': [False],
'min_donation_threshold_amount': [0.0],
'sybilDefense': ['none']
}
rounds = pd.concat([rounds, pd.DataFrame(round_data)], ignore_index=True)
token = rounds['token'].values[0] if 'token' in rounds else 'ETH'
sybilDefense = rounds['sybilDefense'].values[0] if 'sybilDefense' in rounds else 'None'
df = utils.get_round_votes(round_id, chain_id)
# CUSTOM RULE: For round 608, change application_id 90 to 97
if round_id == '608' and chain_id == 42161:
affected_rows = len(df[df['application_id'] == '90'])
if affected_rows > 0:
df.loc[df['application_id'] == '90', ['application_id', 'project_id']] = ['97', '0x668333acfdfa16a6a9cac31af60d933bf92f70426a7e9ed6f7d7b8f1c2113b1b']
st.warning(f"Due to a duplicate project entry, {affected_rows} votes for Application ID 90 have been remapped to Application ID 97 in round 608")
# Fetch token configuration and price
config_df = utils.fetch_tokens_config()
config_df = config_df[(config_df['chain_id'] == chain_id) & (config_df['token_address'] == token)]
matching_token_price = utils.fetch_latest_price(config_df['price_source_chain_id'].iloc[0], config_df['price_source_address'].iloc[0])
unique_voters = df['voter'].unique()
scores, score_at_50_percent, score_at_100_percent, sybilDefense = load_scores_and_set_defense(chain_id, sybilDefense, unique_voters)
# Merge scores with the main dataframe
df = pd.merge(df, scores[['address', 'rawScore']], left_on='voter', right_on='address', how='left')
df['rawScore'] = df['rawScore'].fillna(0) # Fill NaN values with 0 for voters without a score
return {
"blockchain_mapping": blockchain_mapping,
"rounds": rounds,
"df": df,
"config_df": config_df,
"matching_token_price": matching_token_price,
"scores": scores,
"score_at_50_percent": score_at_50_percent,
"score_at_100_percent": score_at_100_percent,
"sybilDefense": sybilDefense,
"chain_id": chain_id,
"matching_cap": rounds['matching_cap_amount'].values[0].astype(float),
"matching_available": rounds['matching_funds_available'].values[0].astype(float),
}
def display_round_settings(data):
"""Display the settings and statistics for the current round."""
st.title(f" {data['rounds']['round_name'].values[0]}: Matching Results")
st.header(f"⚙️ Round Settings")
col1, col2 = st.columns(2)
col1.write(f"**Chain:** {data['blockchain_mapping'][data['chain_id']]}")
col1.write(f"**Matching Cap:** {data['matching_cap']:.2f}%")
col1.write(f"**Passport Defense Selected:** {data['sybilDefense']}")
col1.write(f"**Number of Unique Voters:** {data['df']['voter'].nunique()}")
col2.write(f"**Matching Available:** {data['matching_available']:.2f} {data['config_df']['token_code'].iloc[0]}")
col2.write(f"**Matching Token Price:** ${data['matching_token_price']:.2f}")
col2.write(f"**Minimum Donation Threshold Amount:** ${data['rounds'].get('min_donation_threshold_amount', 0).values[0]:.2f}")
col2.write(f"**Number of Unique Projects:** {data['df']['project_name'].nunique()}")
def calculate_percent_scored_voters(data):
"""Calculate the percentage of unique voters who have a score."""
total_unique_voters = data['df']['voter'].nunique()
scored_unique_voters = len(data['scores'])
percent_scored = (scored_unique_voters / total_unique_voters) * 100 if total_unique_voters > 0 else 0
return percent_scored
def display_scores_progress_bar(data):
"""Display a progress bar showing the percentage of voters who have a score."""
percent_scored = calculate_percent_scored_voters(data)
st.subheader('')
st.subheader(f"{percent_scored:.2f}% of addresses scored using {data['sybilDefense']}")
st.progress(percent_scored/100)
st.subheader('')
def display_crowdfunding_stats(df, matching_amount_display, matching_amount):
"""Display crowdfunding statistics and metrics."""
st.header('👥 Crowdfunding')
crowd_raised = df['amountUSD'].sum()
st.subheader(f"${crowd_raised:,.2f} raised by crowd")
st.subheader(f"{(crowd_raised / matching_amount_display) * 100:.2f}% of the matching pool")
grouped_voter_data = df.groupby('voter')['amountUSD'].agg(['sum', 'mean', 'median', 'max']).reset_index()
col1, col2, col3 = st.columns(3)
col1.metric("Median Donor Contribution", f"${grouped_voter_data['median'].median():.2f}")
col2.metric("Average Donor Contribution", f"${grouped_voter_data['mean'].mean():.2f}")
col3.metric("Max Donor Contribution", f"${grouped_voter_data['max'].max():.2f}")
return grouped_voter_data
def create_donation_distribution_chart(grouped_voter_data):
"""Create a chart showing the distribution of donor contributions."""
bin_edges = [0, 1, 2, 3, 4, 5, 10, 20, 30, 50, 100, 500, 1000, np.inf]
bin_labels = ['0-1','1-2','2-3','3-4', '4-5', '5-10', '10-20','20-30','30-50', '50-100', '100-500', '500-1000', '1000+']
grouped_voter_data['amountUSD_bin'] = pd.cut(grouped_voter_data['sum'], bins=bin_edges, labels=bin_labels, right=False)
fig = px.histogram(grouped_voter_data, x="amountUSD_bin", category_orders={'amountUSD_bin': bin_labels},
labels={'amountUSD_bin': 'Donation Amount Range (USD)'}, nbins=len(bin_edges)-1)
fig.update_traces(hovertemplate='<b>Donation Range:</b> $%{x}<br><b>Number of Donors:</b> %{y}')
fig.update_layout(
title_text='Distribution of Donor Contributions by Amount',
xaxis=dict(title='Donation Amount Range (USD)', titlefont=dict(size=18), tickfont=dict(size=14)),
yaxis=dict(title='Number of Donors', titlefont=dict(size=18), tickfont=dict(size=14)),
bargap=0.3
)
return fig
def handle_csv_upload(purpose='filter out'):
"""Handle the upload and processing of CSV file for wallet filtering."""
if purpose == 'filter out':
st.write('Upload a CSV file with a single column named "address" containing the ETH addresses to filter out. Addresses should include the 0x prefix.')
if purpose == 'filter in':
st.write('Upload a CSV file with a single column named "address" containing the ETH addresses to filter in. Addresses should include the 0x prefix. These addresses will be exempt from passport-based sybil detection.')
if purpose == 'general scaling':
st.write('Upload a CSV file with a column named "address" and a column named "scale". Addresses listed in the CSV will bypass passport scaling and instead have their contributions scaled by the amount listed. You do not need to include every address.')
uploaded_file = st.file_uploader("Upload a CSV file", type="csv", key=purpose)
if uploaded_file is not None:
csv = pd.read_csv(uploaded_file)
st.subheader("✅ CSV file processed successfully and results updated below.")
st.write("Preview of uploaded data:")
st.dataframe(csv, height=200)
csv['address'] = csv['address'].str.lower()
csv.set_index('address', inplace=True)
if purpose == 'filter in':
csv['scale'] = 1
if purpose == 'filter out':
csv['scale'] = 0
return csv
return None
def display_network_graph(df):
"""Display a 3D network graph of voters and grants."""
st.subheader('🌐 Connection Graph')
grants_color = '#00433B'
grantee_color_string = 'moss'
voters_color = '#C4F092'
voter_color_string = 'lightgreen'
line_color = '#6E9A82'
# Assuming 'data' is the DataFrame containing all donation information
# Sum amountUSD grouped by voter and recipient_address
grouped_data = df.groupby(['voter', 'recipient_address']).agg({
'amountUSD': 'sum',
'project_name': 'first' # Assuming project_name is constant for each recipient_address
}).reset_index()
count_connections = grouped_data.shape[0]
count_voters = grouped_data['voter'].nunique()
count_grants = grouped_data['recipient_address'].nunique()
max_connections = 2000
col1, col2 = st.columns([3, 1])
with col1:
st.markdown("The below graph visualizes the connections between donors and grantees. " +
f"Donors are represented by {voter_color_string} nodes, while grantees are represented by {grantee_color_string} nodes. " +
f"Each line connecting a donor to a grantee represents a donation.")
st.markdown("In COCM, projects receive higher matching when their donors support a diverse range of other projects and have unique connection patterns. Conversely, projects get lower matching if their donors primarily support a small number of the same projects.")
with col2:
pct_to_sample = st.slider("Percentage of connections to sample",
min_value=1,
max_value=100,
value=min(100, int(max_connections / count_connections * 100)),
step=1,
help="Adjust this to control the number of connections displayed in the graph. More connections means longer loading times")
st.markdown("**Go fullscreen with the arrows in the top-right for a better view.**")
num_connections_to_sample = int(count_connections * pct_to_sample / 100)
grouped_data = grouped_data.sample(n=min(num_connections_to_sample, max_connections), random_state=42)
count_connections = grouped_data.shape[0]
# Initialize a new Graph
B = nx.Graph()
# Create nodes with the bipartite attribute
B.add_nodes_from(grouped_data['voter'].unique(), bipartite=0, color=voters_color)
B.add_nodes_from(grouped_data['recipient_address'].unique(), bipartite=1, color=grants_color)
# Add edges with amountUSD as an attribute
for _, row in grouped_data.iterrows():
B.add_edge(row['voter'], row['recipient_address'], amountUSD=row['amountUSD'])
pos = nx.spring_layout(B, dim=3, k=0.09, iterations=50)
# Extract node information
node_x, node_y, node_z = zip(*pos.values())
node_names = list(pos.keys())
degrees = [B.degree(node_name) for node_name in node_names]
node_sizes = np.log(np.array(degrees) + 1) * 10
# Extract edge information
edge_x, edge_y, edge_z = [], [], []
edge_weights = []
for edge in B.edges(data=True):
x0, y0, z0 = pos[edge[0]]
x1, y1, z1 = pos[edge[1]]
edge_x.extend([x0, x1, None])
edge_y.extend([y0, y1, None])
edge_z.extend([z0, z1, None])
edge_weights.append(edge[2]['amountUSD'])
# Create the edge traces
edge_trace = go.Scatter3d(
x=edge_x, y=edge_y, z=edge_z,
line=dict(width=1, color=line_color),
hoverinfo='none',
mode='lines',
marker=dict(opacity=0.5))
# Create the node traces
node_trace = go.Scatter3d(
x=node_x, y=node_y, z=node_z,
mode='markers',
hoverinfo='text',
marker=dict(
color=[data['color'] for _, data in B.nodes(data=True)],
size=node_sizes,
opacity=1,
sizemode='diameter'
))
node_adjacencies = [len(list(B.neighbors(node))) for node in node_names]
node_trace.marker.color = [data['color'] for _, data in B.nodes(data=True)]
# Prepare text information for hovering
node_text = []
for node in node_names:
if node in grouped_data['recipient_address'].values:
project_name = grouped_data[grouped_data['recipient_address'] == node]['project_name'].iloc[0]
adj = len(list(B.neighbors(node)))
connections_text = f"Connections: {adj}" if pct_to_sample == 100 else f"Sampled Connections: {adj}"
node_text.append(f'Project: {project_name}<br>{connections_text}')
else:
adj = len(list(B.neighbors(node)))
connections_text = f"Connections: {adj}" if pct_to_sample == 100 else f"Sampled Connections: {adj}"
node_text.append(f'Voter: {node[:6]}...{node[-4:]}<br>{connections_text}')
node_trace.text = node_text
# Create the figure
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title=' ',
titlefont=dict(size=20),
showlegend=False,
hovermode='closest',
margin=dict(b=20, l=5, r=5, t=40),
annotations=[dict(
showarrow=False,
text="Use your mouse to rotate, zoom, and pan around the 3D graph for a better view of connections.",
xref="paper",
yref="paper",
x=0.005,
y=-0.002)],
scene=dict(
xaxis_title='X Axis',
yaxis_title='Y Axis',
zaxis_title='Z Axis')))
st.plotly_chart(fig, use_container_width=True)
def calculate_verified_vs_unverified(scores, donations_df, score_threshold):
"""Calculate and visualize the comparison between verified and unverified users."""
verified_mask = scores['rawScore'] >= score_threshold
verified_users_count = verified_mask.sum()
verified_funding_total = donations_df.loc[donations_df['rawScore'] >= score_threshold, 'amountUSD'].sum()
unverified_users_count = (~verified_mask).sum()
unverified_funding_total = donations_df.loc[donations_df['rawScore'] < score_threshold, 'amountUSD'].sum()
summary_data = pd.DataFrame({
'Category': ['Verified', 'Unverified'],
'Users': [verified_users_count, unverified_users_count],
'Crowdfunding': [verified_funding_total, unverified_funding_total]
})
summary_data['Percentage Of Users'] = summary_data['Users'] / summary_data['Users'].sum() * 100
summary_data['Percentage Of Crowdfunding'] = summary_data['Crowdfunding'] / summary_data['Crowdfunding'].sum() * 100
fig = go.Figure()
for metric in ['Crowdfunding', 'Users']:
fig.add_trace(go.Bar(
x=summary_data[f'Percentage Of {metric}'],
y=summary_data['Category'],
orientation='h',
name=f'Percentage Of {metric}',
text=summary_data[f'Percentage Of {metric}'].apply(lambda x: f"{x:.1f}%"),
textposition='auto',
hovertemplate=f'<b>%{{y}}</b><br>Percentage Of {metric}: %{{x:.1f}}%<br>{metric}: %{{customdata:,}}<extra></extra>',
customdata=summary_data[metric]
))
fig.update_layout(
title_text='Percentage of Crowdfunding and Users by Passport Status',
title_font=dict(size=24),
bargap=0.3,
xaxis=dict(title='Percentage', titlefont=dict(size=18), tickfont=dict(size=14)),
yaxis=dict(title='Category', titlefont=dict(size=18), tickfont=dict(size=14)),
barmode='group',
legend=dict(traceorder='reversed'),
hoverlabel=dict(bgcolor="white", font_size=16, font_family="Rockwell")
)
return fig, summary_data
def categorize_user(score, score_at_50_percent, score_at_100_percent):
if score >= score_at_100_percent:
return 'Full'
elif score >= score_at_50_percent:
return 'Partial'
else:
return 'Unmatched'
def display_passport_usage(data):
"""Display passport usage statistics if Sybil defense is enabled."""
if data['sybilDefense'] != 'None':
st.header('🛂 Passport Usage')
display_scores_progress_bar(data)
num_adjusted = 0
if data['scaling_df'] is not None:
num_adjusted = len(data['scaling_df'])
total_voters = data['df']['voter'].nunique()
if data['sybilDefense'] in ['Passport Stamps', 'Avalanche Passport']:
st.subheader(f" {len(data['scores'])} Users ({len(data['scores'])/total_voters*100:.1f}%) Have a Passport Score")
if num_adjusted > 0:
st.write(f'{num_adjusted} users had scores manually adjusted')
passport_usage_fig, passport_usage_df = calculate_verified_vs_unverified(data['scores'], data['df'], data['score_at_50_percent'])
st.plotly_chart(passport_usage_fig, use_container_width=True)
if data['sybilDefense'] == 'Passport Model Based Detection System':
total_voters = data['df']['voter'].nunique()
n_users_passing_100 = len(data['scores'][data['scores']['rawScore'] >= data['score_at_100_percent']])
n_users_passing_50 = len(data['scores'][(data['scores']['rawScore'] >= data['score_at_50_percent']) & (data['scores']['rawScore'] < data['score_at_100_percent'])])
st.subheader(f" {n_users_passing_100} Users ({n_users_passing_100/total_voters*100:.1f}%) recieve full matching (passport model score over {data['score_at_100_percent']})")
st.subheader(f" {n_users_passing_50} Users ({n_users_passing_50/total_voters*100:.1f}%) recieve partial matching (passport model score between {data['score_at_50_percent']} and {data['score_at_100_percent']})")
st.markdown("""
🔹 **Full matching:** User contributions are matched at 100% of the calculated amount.
🔸 **Partial matching:** User contributions are matched between 50-100% of the calculated amount.
📊 **Matching percentage is based on the user's passport model score:**
- Higher scores = Higher matching percentage
- Very low scores = No matching
💡 This system encourages legitimate users to build stronger digital identities for better matching rates while protecting matching funds from sybils and airdrop farmers.
""")
with st.expander("**Expand: Matching Breakdown by Project Information**"):
# Categorize donors based on their scores
data['scores']['category'] = data['scores']['rawScore'].apply(
lambda score: categorize_user(score, data['score_at_50_percent'], data['score_at_100_percent'])
)
# Merge dataframes and fill missing categories
merged_df = pd.merge(data['df'], data['scores'], left_on='voter', right_on='address', how='left')
merged_df['category'] = merged_df['category'].fillna('Unmatched')
# Group and pivot data
grouped = merged_df.groupby(['project_name', 'category']).agg({
'voter': 'nunique',
'amountUSD': 'sum'
}).reset_index()
pivot_df = grouped.pivot(index='project_name', columns='category', values=['voter', 'amountUSD'])
pivot_df.columns = [f'{col[1]}_{col[0]}' for col in pivot_df.columns]
pivot_df = pivot_df.reset_index()
# Ensure all necessary columns exist and replace None with 0
for cat in ['Unmatched', 'Partial', 'Full']:
for val in ['voter', 'amountUSD']:
if f'{cat}_{val}' not in pivot_df.columns:
pivot_df[f'{cat}_{val}'] = 0
else:
pivot_df[f'{cat}_{val}'] = pivot_df[f'{cat}_{val}'].fillna(0)
# Rename columns for clarity
pivot_df = pivot_df.rename(columns={
'Unmatched_voter': 'Unmatched Donors',
'Partial_voter': 'Partial Donors',
'Full_voter': 'Full Donors',
'Unmatched_amountUSD': 'Unmatched Amount',
'Partial_amountUSD': 'Partial Matched Amount',
'Full_amountUSD': 'Full Matched Amount'
})
# Calculate totals and percentages
pivot_df['Total Donors'] = pivot_df['Unmatched Donors'] + pivot_df['Partial Donors'] + pivot_df['Full Donors']
pivot_df['Percent Donors Matched'] = (pivot_df['Partial Donors'] + pivot_df['Full Donors']) / pivot_df['Total Donors'] * 100
# Round amount columns
for col in ['Unmatched Amount', 'Partial Matched Amount', 'Full Matched Amount']:
pivot_df[col] = pivot_df[col].round(2)
# Reorder columns
column_order = ['project_name', 'Total Donors', 'Percent Donors Matched', 'Unmatched Donors', 'Partial Donors', 'Full Donors', 'Unmatched Amount', 'Partial Matched Amount', 'Full Matched Amount']
pivot_df = pivot_df[column_order]
# Sort by percent of donors matched in descending order
pivot_df = pivot_df.sort_values('Percent Donors Matched', ascending=False)
# Display the dataframe
st.dataframe(
pivot_df,
column_config={
"project_name": "Project",
"Total Donors": st.column_config.NumberColumn("Total Donors", format="%d"),
"Percent Donors Matched": st.column_config.ProgressColumn("% Donors Matched", format="%.2f%%", min_value=0, max_value=100),
"Unmatched Donors": st.column_config.NumberColumn("Unmatched Donors", format="%d"),
"Partial Donors": st.column_config.NumberColumn("Partial Donors", format="%d"),
"Full Donors": st.column_config.NumberColumn("Full Donors", format="%d"),
"Unmatched Amount": st.column_config.NumberColumn("Unmatched Amount", format="$%.2f"),
"Partial Matched Amount": st.column_config.NumberColumn("Partial Matched Amount", format="$%.2f"),
"Full Matched Amount": st.column_config.NumberColumn("Full Matched Amount", format="$%.2f")
},
hide_index=True
)
def calculate_matching_results(data):
"""Calculate matching results using different strategies (COCM and QF)."""
# Apply voting eligibility based on passport scores and donation thresholds
df_with_passport = fundingutils.apply_voting_eligibility(
data['df'].copy(),
data['rounds'].get('min_donation_threshold_amount', 0).values[0],
data['score_at_50_percent'],
data['score_at_100_percent'],
data['scaling_df']
)
votes_df_with_passport = fundingutils.pivot_votes(df_with_passport)
matching_cap_amount = data['matching_cap']
matching_amount = data['matching_available']
# Calculate matching amounts using both COCM and QF strategies
matching_dfs = [fundingutils.get_qf_matching(strategy, votes_df_with_passport, matching_cap_amount, matching_amount, cluster_df=votes_df_with_passport, pct_cocm=data['pct_COCM'])
for strategy in [data['strat'], 'QF']]
# Merge results from both strategies
matching_df = pd.merge(matching_dfs[0], matching_dfs[1], on='project_name', suffixes=(f'_{data["suffix"]}', '_QF'))
# Add project details and calculate the difference between COCM and QF matching
df_unique = data['df'][['project_name', 'chain_id', 'round_id', 'application_id']].drop_duplicates()
matching_df = pd.merge(matching_df, df_unique, on='project_name', how='left')
matching_df['Project Page'] = 'https://explorer.gitcoin.co/#/round/' + matching_df['chain_id'].astype(str) + '/' + matching_df['round_id'].astype(str) + '/' + matching_df['application_id'].astype(str)
matching_df['Δ Match'] = matching_df[f'matching_amount_{data["suffix"]}'] - matching_df['matching_amount_QF']
return matching_df.sort_values(f'matching_amount_{data["suffix"]}', ascending=False)
def display_matching_results(matching_df, matching_token_symbol, s):
"""Display the matching results in a formatted table."""
st.subheader('Matching Results')
column_config = {
"project_name": st.column_config.TextColumn("Project"),
f"matching_amount_{s}": st.column_config.NumberColumn(f"{s} Match", format="%.2f"),
"matching_amount_QF": st.column_config.NumberColumn("QF Match", format="%.2f"),
"Δ Match": st.column_config.NumberColumn("Δ Match", format="%.2f"),
"Project Page": st.column_config.LinkColumn("Project Page", display_text="Visit")
}
display_columns = ['project_name', f'matching_amount_{s}', 'matching_amount_QF', 'Δ Match', 'Project Page']
st.dataframe(
matching_df[display_columns],
use_container_width=True,
column_config=column_config,
hide_index=True
)
st.markdown(f'Matching Values shown above are in **{matching_token_symbol}**')
def select_matching_strategy(s):
"""Allow user to select the matching strategy for download."""
return st.selectbox(
'Select the matching strategy to download:',
(f'{s}', 'QF')
)
def prepare_output_dataframe(matching_df, strategy_choice, data):
"""Prepare the output dataframe for download based on the selected strategy."""
# Select relevant columns and rename them
output_df = matching_df[['project_name', f'matching_amount_{strategy_choice}']]
output_df = output_df.rename(columns={f'matching_amount_{strategy_choice}': 'matched'})
# Fetch and merge project details
projects_df = utils.get_projects_in_round(data['rounds']['round_id'].iloc[0], data['chain_id'])
projects_df = projects_df[~projects_df['project_name'].isin(data['projects_to_remove'])]
# CUSTOM RULE: Remove application ID 90 from round 608, duplicate project
if data['rounds']['round_id'].iloc[0] == '608' and data['chain_id'] == 42161:
if '90' in projects_df['id'].values:
projects_df = projects_df[projects_df['id'] != '90']
output_df = pd.merge(output_df, projects_df, left_on='project_name', right_on='project_name', how='outer')
output_df = output_df.rename(columns={
'id': 'applicationId',
'project_id': 'projectId',
'project_name': 'projectName',
'recipient_address': 'payoutAddress',
'total_donations_count': 'contributionsCount',
'total_amount_donated_in_usd': 'totalReceived'
})
# Convert matching amounts to USD and adjust for token decimals
matching_token_decimals = data['config_df']['token_decimals'].iloc[0]
output_df['matchedUSD'] = (output_df['matched'] * data['matching_token_price']).round(2)
output_df['matched'] = (output_df['matched'] * 10**matching_token_decimals).apply(lambda x: int(x))
output_df['totalReceived'] = (output_df['totalReceived'] * 10**matching_token_decimals).apply(lambda x: int(x))
# Reorder columns and add placeholder columns required for the output format
output_df = output_df[[
'applicationId', 'projectId', 'projectName', 'payoutAddress',
'matchedUSD', 'totalReceived', 'contributionsCount', 'matched'
]]
output_df['sumOfSqrt'] = 0
output_df['capOverflow'] = 0
output_df['matchedWithoutCap'] = 0
return output_df.fillna(0)
def adjust_matching_overflow(output_df, matching_funds_available, matching_token_decimals):
"""Adjust matching funds if there's an overflow, using Decimal for high precision and handling numpy types."""
getcontext().prec = 36 # Set precision high enough to handle 18 decimal places safely
# Convert numpy types to Python types
matching_funds_available = float(matching_funds_available)
matching_token_decimals = int(matching_token_decimals)
full_matching_funds_available = Decimal(str(matching_funds_available)) * Decimal(10**matching_token_decimals)
total_matched = sum(Decimal(str(x)) for x in output_df['matched'])
matching_overflow = total_matched - full_matching_funds_available
if matching_overflow <= 0:
return output_df
#st.warning(f'Initial Matching Overflow: {matching_overflow / Decimal(10**matching_token_decimals)}. Adjusting Matching Funds')
# Calculate the reduction factor
reduction_factor = full_matching_funds_available / total_matched
# Apply the reduction factor to all matched amounts
output_df['matched'] = output_df['matched'].apply(lambda x: int(Decimal(str(x)) * reduction_factor))
# Distribute any remaining overflow due to rounding
remaining_overflow = sum(Decimal(str(x)) for x in output_df['matched']) - full_matching_funds_available
if remaining_overflow > 0:
sorted_indices = output_df['matched'].argsort()[::-1]
for i in range(int(remaining_overflow)):
output_df.iloc[sorted_indices[i], output_df.columns.get_loc('matched')] -= 1
final_overflow = sum(Decimal(str(x)) for x in output_df['matched']) - full_matching_funds_available
#st.success(f'Matching funds adjusted in 1 iteration. Final overflow: {final_overflow / Decimal(10**matching_token_decimals)}')
if final_overflow > 0:
st.error('There is a matching overflow. Please contact the team for further assistance.')
return output_df
def display_matching_distribution(output_df):
"""Display and provide download option for the matching distribution."""
# Create a copy for display purposes
display_df = output_df.copy()
# Format large numbers for display
for col in ['matched', 'totalReceived', 'sumOfSqrt', 'capOverflow', 'matchedWithoutCap']:
display_df[col] = display_df[col].apply(lambda x: f"{x}")
st.dataframe(display_df, use_container_width=True)
# Use the original output_df for CSV download
st.download_button(
label="⬇ Download Matching Distribution",
data=output_df.to_csv(index=False),
file_name='matching_distribution.csv',
mime='text/csv'
)
st.write('You can upload this CSV to manager.gitcoin.co to apply the matching results to your round.')
st.write('Note: If manual edits are needed, the key column to update is "matched". Values must be integers without decimals or commas, as our contracts expect token amounts in their smallest unit (e.g., wei for ETH). Incorrect formatting could cause errors in fund allocation.')
def create_summary_dataframe(output_df, matching_df, token_code, s):
"""Create a summary dataframe for the round results."""
summary_df = output_df[['projectName', 'matchedUSD']].copy()
summary_df = summary_df.merge(matching_df[['project_name', f'matching_amount_{s}', 'Project Page']],
left_on='projectName', right_on='project_name', how='left')
summary_df = summary_df.rename(columns={
'projectName': 'Project',
f'matching_amount_{s}': f'Matching Funds ({token_code})',
'matchedUSD': 'Matching Funds (USD)',
'Project Page': 'Project Page'
})
summary_df = summary_df[[
'Project',
f'Matching Funds ({token_code})',
'Matching Funds (USD)',
'Project Page'
]]
for col in ['Matching Funds (USD)', f'Matching Funds ({token_code})']:
summary_df[col] = summary_df[col].round(2)
return summary_df.sort_values(f'Matching Funds ({token_code})', ascending=False)
def display_summary(summary_df):
"""Display and provide download option for the round summary."""
st.write(summary_df)
st.download_button(
label="⬇ Download Summary",
data=summary_df.to_csv(index=False),
file_name='round_summary.csv',
mime='text/csv'
)
def create_matching_distribution_chart(summary_df, token_symbol):
"""Create a bar chart showing the distribution of matching funds."""
summary_df = summary_df.sort_values(f'Matching Funds ({token_symbol})', ascending=True)
fig = px.bar(
summary_df,
x=f'Matching Funds ({token_symbol})',
y='Project',
orientation='h',
title='Matching Funds Distribution',
labels={f'Matching Funds ({token_symbol})': 'Matched Funds', 'Project': 'Project'},
text=summary_df[f'Matching Funds ({token_symbol})'].apply(lambda x: f"{x/1000:.1f}k" if x >= 1000 else f"{x:.0f}")
)
fig.update_layout(
xaxis_title='',
yaxis_title='',
yaxis=dict(tickmode='linear'),
template='plotly_white',
height=1640,
width=800
)
return fig
def main():
"""Main function to run the Streamlit app."""
st.image('assets/657c7ed16b14af693c08b92d_GTC-Logotype-Dark.png', width=200)
round_id, chain_id = validate_input()
check_round_existence(round_id, chain_id)
# Advanced options
filterin_df=None
filterout_df=None
arbitrary_df=None
scaling_df=None
with st.expander("Advanced: Override Passport Scaling"):
if st.toggle('Filter in wallets', value=False, key='filterin-toggle'):
filterout_df = handle_csv_upload(purpose='filter in')
if st.toggle('Filter out wallets',value=False,key='filterout-toggle'):
filterin_df = handle_csv_upload(purpose='filter out')
if st.toggle('Arbitrary scaling (e.g. Tunable QF)',value=False,key='arbitraryscale-toggle'):
arbitrary_df = handle_csv_upload(purpose='general scaling')
uploaded_dfs = [x for x in [filterin_df, filterout_df, arbitrary_df] if x is not None]
if len(uploaded_dfs) >= 1:
scaling_df = pd.concat(uploaded_dfs)
# half_and_half = False
# with st.expander("Advanced: Give results as half COCM / half QF"):
# st.write('''
# Toggle the switch below to calculate a matching result blending COCM and QF, instead of pure COCM.
# In this case, funding amounts will be found for each mechanism, normalized to the size of the matching pool, and then averaged for each project.
# E.g. if a project gets 10% of the matching pool under COCM and 40% of the matching pool under QF, they will get 25% of the matching pool under this calculation.''')
# half_and_half = st.toggle('Select for half-and-half')
pct = 1
with st.expander('Advanced: Blend COCM and QF'):
st.write('''Use the slider below to blend COCM and QF together in your results.
Funding amounts will be found for each mechanism, normalized to the size of the matching pool, and then blended for each project.
Set the slider to "1" to just use COCM.
Pure QF results are always available separately.''')
c1,c2,c3 = st.columns(3)
pct = c1.slider('Percent COCM', min_value = 0.25, max_value=1.0, value=1.0, step=0.25)
# Load and process data
data = load_data(round_id, chain_id)
data['scaling_df'] = scaling_df
if pct == 1:
data['strat'] = 'COCM'
data['suffix'] = 'COCM'
data['pct_COCM'] = 1
else:
data['strat'] = 'pctCOCM'
data['suffix'] = (str(pct)+'0')[2:4]+'pctCOCM'
data['pct_COCM'] = pct
matching_pool_size_override = None
with st.expander('Advanced: Change Matching Pool Size'):
matching_pool_size_override = st.number_input(f"New matching pool size (in {data['config_df']['token_code'].iloc[0]}): ", value=float(data['matching_available']), min_value=1.0)
if matching_pool_size_override is not None:
data['matching_available'] = matching_pool_size_override
# Display various settings of the round
display_round_settings(data)
matching_amount = data['matching_available']
matching_amount_display = data['matching_token_price'] * matching_amount
# Crowdfunding stats section
grouped_voter_data = display_crowdfunding_stats(data['df'], matching_amount_display, matching_amount)
st.plotly_chart(create_donation_distribution_chart(grouped_voter_data), use_container_width=True)
# Passport usage section (ONLY APPEARS IF SYBIL DEFENSE IS ENABLED)
display_passport_usage(data)
# Quadratic Funding Method Comparison section
st.header('💚 Quadratic Funding Method Comparison')
st.write('''[Quadratic funding](https://wtfisqf.com) helps us solve coordination failures by creating a way for community members to fund what matters to them while amplifying their impact. However, its assumption that people make independent decisions can be exploited to unfairly influence the distribution of matching funds.''')
st.write('''[Connection-oriented cluster-matching (COCM)](https://wtfiscocm.streamlit.app/) doesn't make this assumption. Instead, it quantifies just how coordinated groups of actors are likely to be based on the social signals they have in common. Projects backed by more independent agents receive greater matching funds. Conversely, if a project's support network shows higher levels of coordination, the matching funds are reduced, encouraging self-organized solutions within more coordinated groups. ''')
# Allow removal of projects from matching distribution
all_projects = data['df']['project_name'].unique()
data['projects_to_remove'] = st.multiselect('Projects may be removed from the matching distribution by selecting them here:', all_projects)
data['df'] = data['df'][~data['df']['project_name'].isin(data['projects_to_remove'])]
# Calculate and display matching results
matching_df = calculate_matching_results(data)
display_matching_results(matching_df, data['config_df']['token_code'].iloc[0], data['suffix'])
display_network_graph(data['df'])
# Matching distribution download section
st.subheader('Download Matching Distribution')
if calculate_percent_scored_voters(data) < 99:
st.warning('Matching distribution download is not recommended until more addresses are scored. This could take 24-72 hours after the round concludes.')
strategy_choice = select_matching_strategy(data['suffix'])
output_df = prepare_output_dataframe(matching_df, strategy_choice, data)
output_df = adjust_matching_overflow(output_df, data['matching_available'], data['config_df']['token_decimals'].iloc[0])
display_matching_distribution(output_df)
with st.expander('Advanced: Matching Overflow Information', expanded=False):
try:
full_matching_funds_available = Decimal(str(data['matching_available'])) * Decimal(10**int(data['config_df']['token_decimals'].iloc[0]))
total_matched = sum(Decimal(str(x)) for x in output_df['matched'])
matching_overflow = total_matched - full_matching_funds_available
st.write(f" Matching Overflow: {(matching_overflow / Decimal(10**int(data['config_df']['token_decimals'].iloc[0]))):.18f} or {matching_overflow} wei")
except (ValueError, TypeError, decimal.InvalidOperation) as e:
st.error(f"Error calculating matching overflow: {str(e)}")
# Summary section
st.header('📈 Sharable Summary')
token_code = data['config_df']['token_code'].iloc[0]
summary_df = create_summary_dataframe(output_df, matching_df, token_code, data['suffix'])
display_summary(summary_df)
#matching_distribution_chart = create_matching_distribution_chart(summary_df, token_code)
#st.plotly_chart(matching_distribution_chart)
if __name__ == "__main__":
main()