-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_holders.py
177 lines (147 loc) · 4.77 KB
/
top_holders.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
#! Python3
"""
File name: top_holders.py
Author: Jonathan Snow
Date created: 08/25/2022
Python Version: 3.9.x
File Details:
Purpose: A series of functions to determine the top holders in a requested collection
Notes:
- Alchemy API returns non-checksum addresses, shouldn't be an issue, but worth noting
"""
# Imports
import sys
import requests
from web3 import Web3
from ens import ENS
import dotenv
import os
dotenv.load_dotenv()
# Globals
ALCHEMY_KEY = os.getenv("ALCHEMY_API_KEY")
URL = "https://eth-mainnet.g.alchemy.com/nft/v2/" + ALCHEMY_KEY + "/getOwnersForCollection?contractAddress="
ALCHEMY = "https://eth-mainnet.alchemyapi.io/v2/" + ALCHEMY_KEY
WEB3 = Web3(Web3.HTTPProvider(ALCHEMY))
ns = ENS.fromWeb3(WEB3)
##################################################
#
def main(address):
"""
Function to determine the top holders of a provided collection
and output the relevant information to the console.
"""
# Fetch input collection owners
collection = get_data(URL + address + "&withTokenBalances=true")['ownerAddresses']
print("There are " + str(len(collection)) + " unique owners in this collection.")
# Process data on collection owners
owner_data = process_data(collection)
# Sort dict in descending order by values
sorted_owners = dict(sorted(owner_data.items(), key=lambda item: item[1], reverse=True))
# Pull top holder and held quantity for output
top_holder = list(sorted_owners.keys())[0]
top_holder_ens = get_ens_name(top_holder)
top_holder_quantitiy = list(sorted_owners.values())[0]
print("The top holder of Pudgy Penguins is " + top_holder_ens + " who holds " + str(top_holder_quantitiy) + " tokens.")
# Pull top 10 holders and quantities held for output
# Ugly code to iterate over the first 10 values in a sorted dict
i = 1
for holder in sorted_owners:
each_holder = get_ens_name(holder)
each_holder_quantity = sorted_owners[holder]
print(each_holder + " - " + str(each_holder_quantity))
if i < 10:
i+=1
continue
else:
break
# Set up holder distribution dict
hd = {
1: 0, # 0-1
2: 0, # 1-3
3: 0, # 3-5
4: 0, # 5-10
5: 0, # 10-50
6: 0, # 50-100
7: 0 # >100
}
# Determine holder distribution
# Reverse order as dict is already sorted DESC
for owner in sorted_owners:
quantity_held = sorted_owners[owner]
# 101+
if quantity_held >= 100:
hd[7] += 1
# 51-100
elif quantity_held >= 50:
hd[6] += 1
# 11-50
elif quantity_held >= 10:
hd[5] += 1
# 6-10
elif quantity_held >= 5:
hd[4] += 1
# 4-5
elif quantity_held >= 4:
hd[3] += 1
# 2-3
elif quantity_held >= 2:
hd[2] += 1
# 1
elif quantity_held == 1:
hd[1] += 1
else:
continue
# Ensure that no tokens were missed
assert( (hd[1] + hd[2] + hd[3] + hd[4] + hd[5] + hd[6] + hd[7]) == len(sorted_owners) )
print("\n" + "Token Distribution")
print("100+: " + str(hd[7]))
print("50-100: " + str(hd[6]))
print("10-50: " + str(hd[5]))
print("5-10: " + str(hd[4]))
print("3-5: " + str(hd[3]))
print("1-3: " + str(hd[2]))
print("1: " + str(hd[1]))
print("\n" + "There are " + str(hd[5] + hd[6] + hd[7]) + " High Conviction Holders that have more than 10 tokens each.")
##################################################
def process_data(input):
"""
Function to clean up raw data and output a dict of
addresses along with token balance.
input - the input data
"""
# Setup output dict
output = {}
# Iterate over each owner address in list
for wallet in input:
address = wallet["ownerAddress"]
balances = wallet["tokenBalances"]
# Add to output dict
output[address] = len(balances)
return output
##################################################
def get_data(url):
"""
Function to fetch data from URL and return JSON.
"""
with requests.Session() as s:
response = s.get(url)
return response.json()
##################################################
def get_ens_name(address):
"""
Function to lookup the ens name for an address.
"""
ens_lookup = ns.name(address)
if ens_lookup is None:
return address
else:
return ns.name(address)
##################################################
# Runtime Entry Point
if __name__ == "__main__":
args = sys.argv
if len(args) == 2:
address = args[1]
main(address)
else:
print("Collection address not entered. Please try again.")