In [1]:
from urllib2 import Request, urlopen, URLError
import pandas as pd
import numpy as np
import json
In [2]:
# Write your API key here
APIKEY = ""
# Make sure you have this file in the same directory
tokens = pd.read_csv('tokens.csv', index_col=False, header=0)
In [4]:
tokenTxs = {}
def getTxs(token):
try:
# Will look only for normal txs
request = Request("http://api.etherscan.io/api?module=account&action=txlist&address={}&apikey={}".format(token["Address"], APIKEY))
response = urlopen(request)
txs = response.read()
tokenTxs[token["Project"]] = pd.read_json(json.dumps(json.loads(txs)["result"]), orient='records')
except URLError, e:
print 'API error. Got an error code:', e
tokens.apply(getTxs, axis=1)
# Sample data
tokenTxs["Acade City"].head()
Out[4]:
In [7]:
# Remove duplicates
uniqueAddresses = np.array([])
for txs in tokenTxs.itervalues():
uniqueAddresses = np.concatenate([uniqueAddresses, np.unique(txs["from"])])
print "Number of user accounts (with duplicates): {}".format(len(uniqueAddresses))
uniqueAddresses = np.unique(uniqueAddresses)
print "Number of user accounts (after removing duplicates): {}".format(len(uniqueAddresses))
In [8]:
# Uncomment to install web3
#!pip install web3
In [9]:
from web3 import Web3, KeepAliveRPCProvider, IPCProvider
# Note that you should create only one RPCProvider per
# process, as it recycles underlying TCP/IP network connections between
# your process and Ethereum node
#web3 = Web3(KeepAliveRPCProvider(host='localhost', port='8545'))
# or for an IPC based connection
web3 = Web3(IPCProvider())
In [10]:
# ERC20 standar
abi = json.loads('[{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]')
balances = pd.DataFrame("", index=uniqueAddresses, columns=tokens["Project"].values)
def setBalances(token):
tokenContract = web3.eth.contract(
abi = abi,
address = token["Address"]
)
for address in uniqueAddresses:
try:
balances.set_value(address, token["Project"], tokenContract.call().balanceOf(address))
except URLError, e:
print 'Web3 error. Got an error code:', e
tokens.apply(setBalances, axis=1)
# Sample data
balances.head()
Out[10]:
In [11]:
# Raw data
for project, txs in tokenTxs.items():
url = "./datasets/{}.csv".format(project)
txs.to_csv(url, sep=',')
# Balances
balances.to_csv("./datasets/balances.csv", sep=',')