Skip to content

Instantly share code, notes, and snippets.

@Spl3en
Last active September 21, 2018 21:53
Show Gist options
  • Save Spl3en/cafbed2b8b2895e1e17d432e1c3ce96f to your computer and use it in GitHub Desktop.
Save Spl3en/cafbed2b8b2895e1e17d432e1c3ce96f to your computer and use it in GitHub Desktop.
Counts the % of ERC20-ICX locked (swapped) to the mainnet.
# coding: utf8
import requests
import certifi
import json
import sys
from bs4 import BeautifulSoup as bs
def get_data (contractaddress, topmax):
data = []
# max page is 200,and we can watch 10000 address most
for i in range(1, 201):
try:
if(len(data) >= int(topmax)):
break
# send the request to the site and check for download errors with
# raise_for_status()
url = 'https://etherscan.io/token/generic-tokenholders2?a=%s&s=4.0022874E%%2b26&p=%d' % (contractaddress, i)
print(url)
req = requests.get(url)
req.raise_for_status()
except Exception as e:
print(e)
else:
soupdata = bs(req.text, "html.parser")
# the data list represents the table as taken from the html code of
# https://etherscan.io/token/generic-tokentxns2?contractAddress={ERC20 Contract}&a=&mode=
# It's a list containing lists. Each nested list represents a row from the
# table. Every list has size = 50, except the first one is col name, and contains the following info:
# [TxHash, Age, From, To, Quantity]
table = soupdata.select('table')
# print(table)
rows = table[0].select('tr')
# print(rows)
for row in rows:
# the first one contain th element shoud be except
if(row.select('th')):
continue
cols = row.select('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
result = []
for d in data:
result.append({'address' : d[1].replace("0x", ""), 'percent' : float(d[3].replace("%", ""))})
return result
def create_jsonrpc_request_content (method, params):
content = {
'jsonrpc': '2.0',
'method': method,
'id': 1
}
if params is not None:
content['params'] = params
return content
def post(url, payload):
try:
path = certifi.where()
r = requests.post(url, json=payload, verify=path)
return r
except requests.exceptions.Timeout:
raise RuntimeError("Timeout happened. Check your internet connection status.")
# Calculate here : top 5000 holders
total_percent = 0
locked_addresses = []
top_holders = get_data ("0xb5a5f22694352c15b00323844ad545abb2b11028", 1000)
for idx, holder in enumerate(top_holders):
sys.stdout.write("[%02.2f%%] Checking 0x%s (%03d / %03d) ... " % (total_percent, holder["address"], idx, len(top_holders)))
if holder["address"] == "0000000000000000000000000000000000000000":
# Special case for burn address
total_percent += holder["percent"]
sys.stdout.write(u"\033[1;32;40mSwapped !\033[0m\n")
else:
request = create_jsonrpc_request_content (
"eth_call",
[
{
"data" : "0xcb7bba39000000000000000000000000%s" % holder["address"],
"to" : "0xb5a5f22694352c15b00323844ad545abb2b11028"
},
"latest"
]
)
response = post ("https://node3.web3api.com/", request)
data = json.loads(response.text)
if int(data["result"], 16) == 1: # locked!
total_percent += holder["percent"]
locked_addresses.append(holder["address"])
sys.stdout.write(u"\033[1;32;40mSwapped !\033[0m\n")
else:
sys.stdout.write(u"\033[1;31;40mNo\033[0m\n")
print("Total percent = %f%%" % total_percent)
print("Locked addresses (count : %d / %d)" % (len(locked_addresses), len(top_holders)))
# print(locked_addresses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment