{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Needed for adding my version of py_etherscan_api pacakge\n", "import sys\n", "sys.path.append('/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import etherscan.accounts as accounts\n", "import pandas as pd\n", "import json" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "IOPub data rate exceeded.\n", "The notebook server will temporarily stop sending output\n", "to the client in order to avoid crashing it.\n", "To change this limit, set the config variable\n", "`--NotebookApp.iopub_data_rate_limit`.\n" ] } ], "source": [ "from plotly import __version__\n", "from plotly.offline import download_plotlyjs, init_notebook_mode, iplot\n", "from plotly.offline import plot\n", "from plotly.graph_objs import Scatter, Data, Box, Bar\n", "from plotly.graph_objs import Scattergl\n", "init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline\n", " # this injects the plotly.js source files into the notebook\n", "from plotly.tools import FigureFactory as FF\n", "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "from plotly import tools\n", "\n", "%matplotlib inline\n", "import matplotlib as plt\n", "plt.style.use('ggplot')\n", "import cufflinks as cf\n", "cf.set_config_file(offline=True, world_readable=True, theme='ggplot')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open('../api_key.json', 'r') as key_file:\n", " key = json.loads(key_file.read())['key']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "api = accounts.Account(address=\"0x49EdF201c1E139282643d5e7C6fB0C7219Ad1db7\", api_key=key)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "page 1 added\n", "Total number of transactions: 2856\n" ] } ], "source": [ "transactions = api.get_all_transactions()\n", "df = pd.DataFrame(transactions)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df.value = df.value.astype(float)/ 1000000000000000000\n", "df.timeStamp = pd.to_datetime(df.timeStamp, unit='s')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "166710.89379168933" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Check total of successful ETH transactions\n", "df[df.isError == '0'].value.sum() " ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true, "scrolled": true }, "outputs": [], "source": [ "block_df = pd.DataFrame(df[df.isError == 0].groupby('timeStamp').sum().reset_index())\n", "block_df.value_cum = block_df.value.cumsum()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import datetime" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dt = datetime.datetime(2017, 5, 2, 15, 30)\n", "dt2 = datetime.datetime(2017, 5, 2, 18, 40)\n", "mask1 = (df.isError == '0') & (df.timeStamp > dt) & (df.timeStamp < dt2)\n", "mask2 = (df.isError == '1') & (df.timeStamp > dt) & (df.timeStamp < dt2)\n", "trace = go.Bar(\n", " x=df[mask1].timeStamp,\n", " y=df[mask1].value,\n", " name=\"successful\",\n", "\n", ")\n", "trace2 = go.Bar(\n", " x=df[mask2].timeStamp,\n", " y=df[mask2].value,\n", " name=\"failed\",\n", "\n", ")\n", "layout = go.Layout(\n", " xaxis=go.XAxis(\n", " title='Time of Investment',\n", "# titlefont = dict(\n", "# size = 16,\n", "# ),\n", "# tickfont = dict(\n", "# size = 16\n", "# )\n", " ),\n", " yaxis=go.YAxis(\n", " title='ETH Invested',\n", "# titlefont = dict(\n", "# size = 16,\n", "# ),\n", "# tickfont = dict(\n", "# size = 16\n", "# )\n", " ),\n", " legend = {\n", " 'x':0.8,\n", " 'y':1\n", " },\n", "# margin={\n", "# 'l': 200,\n", "# 'r': 100,\n", "# 'b': 200,\n", "# },\n", "# height=1500,\n", "# width=3000\n", "# barmode='stack'\n", ")\n", "data = [trace, trace2]\n", "fig = go.Figure(data=data, layout=layout)\n", "iplot(fig)\n", "#py.iplot(fig, filename='TokenCard_TimeSeries_Invested')" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [], "source": [ "combined = df[df.isError == '0'].groupby('from').sum().reset_index()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def label_exp_group(value):\n", " labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8']\n", " for label in labels:\n", " if value <= 10**float(label):\n", " return label\n", " \n", "combined['exp_group'] = combined.value.apply(label_exp_group)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "grouped = pd.DataFrame(combined.groupby('exp_group').sum())\n", "grouped = grouped[['value']]\n", "grouped_size = pd.DataFrame(combined.groupby('exp_group').size())\n", "grouped['size'] = grouped_size[0].values\n", "grouped['size_perc'] = [trans / len(combined) * 100 for trans in grouped['size'].values]\n", "grouped['value_perc'] = [value / 250000 * 100 for value in grouped.value.values]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
valuesizesize_percvalue_perc
exp_group
0104.46345215710.8425410.041785
12728.10949753036.6022101.091244
222326.91499859340.9530398.930766
339741.9924951419.73756915.896797
454084.413349251.72651921.633765
547725.00000020.13812219.090000
\n", "
" ], "text/plain": [ " value size size_perc value_perc\n", "exp_group \n", "0 104.463452 157 10.842541 0.041785\n", "1 2728.109497 530 36.602210 1.091244\n", "2 22326.914998 593 40.953039 8.930766\n", "3 39741.992495 141 9.737569 15.896797\n", "4 54084.413349 25 1.726519 21.633765\n", "5 47725.000000 2 0.138122 19.090000" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grouped" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trace = go.Bar( \n", " y=grouped.value_perc,\n", " name='Total ETH'\n", ")\n", "trace2 = go.Bar( \n", " y=grouped.size_perc,\n", " name='Addresses',\n", ")\n", "data = Data([trace, trace2])\n", "layout = go.Layout(\n", " xaxis=go.XAxis(\n", " title='Investor Group',\n", " ticktext = [\n", " \"$0 < x < 10^0$\",\n", " \"$10^0 < x < 10^1$\",\n", " \"$10^1 < x < 10^2$\",\n", " \"$10^2 < x < 10^3$\",\n", " \"$10^3 < x < 10^4$\",\n", " \"$10^4 < x < 10^5$\",\n", " \"$10^5 < x < 10^6$\",\n", " \"Exchange\"\n", " ],\n", " tickvals = [ 0, 1, 2, 3, 4, 5,6 ],\n", " tickfont=dict(\n", "# size=40,\n", " color='black'\n", " ),\n", " titlefont=dict(\n", "# size=50,\n", " color='black'\n", " ),\n", " ),\n", " yaxis=go.YAxis(\n", " title='Percentage',\n", " tickfont=dict(\n", "# size=40,\n", " color='black'\n", " ),\n", " titlefont=dict(\n", "# size=50,\n", " color='black'\n", " ),\n", " ),\n", " legend=dict(\n", " x=0,\n", " y=1,\n", " font=dict(\n", "# size=40,\n", " color='black'\n", " ),\n", " ),\n", "# height=1500,\n", "# width=3000,\n", " margin={\n", " 'b': 100,\n", " },\n", "# barmode='stack'\n", ")\n", "fig = go.Figure(data=data, layout=layout)\n", "\n", "# iplot(fig)\n", "py.iplot(fig, filename='TokenCard_investor_percentage_breakdown')" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_token_amount(token, address):\n", " req = requests.get(url='https://api.etherscan.io/api?module=account&action=tokenbalance&tokenname='+token+'&address='+address+'&tag=latest&apikey='+key)\n", " try:\n", " print(token, ' amount: ', float(json.loads(req.text)['result'])/1e18)\n", " except:\n", " print('token not available')" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "REP amount: 39387.01891213\n", "DGD amount: 2.94466972444e-05\n", "MKR amount: 2.2913320193794133\n", "token not available\n", "token not available\n", "token not available\n", "SNGLS amount: 1.2431534e-11\n" ] } ], "source": [ "address='0x49EdF201c1E139282643d5e7C6fB0C7219Ad1db7'\n", "tokens = ['REP', 'DGD', 'MKR', 'GNT', 'MLN', 'SWT', 'SNGLS']\n", "for token in tokens:\n", " get_token_amount(token, address)\n", " " ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
tokenvalueevaluated
0REP39387658259.49
1DGD29446901672.59
2Edgeless1524171.79
3Golem1176941265268.49
4Guppy50.79
5Melon64024606.91
6MKR2200.27
7SNGLS124315351423423.19
8SwarmCity300802420356.49
9ETH16671013028465.35
\n", "
" ], "text/plain": [ " token value evaluated\n", "0 REP 39387 658259.49\n", "1 DGD 29446 901672.59\n", "2 Edgeless 1524 171.79\n", "3 Golem 1176941 265268.49\n", "4 Guppy 5 0.79\n", "5 Melon 640 24606.91\n", "6 MKR 2 200.27\n", "7 SNGLS 12431535 1423423.19\n", "8 SwarmCity 300802 420356.49\n", "9 ETH 166710 13028465.35" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ERC20 token amounts given by etherscan.io as of Tues 4:06 PM EST\n", "token_amounts = [\n", " {'token': \"REP\", 'value': 39387, 'evaluated': 658259.49},\n", " {'token': \"DGD\", 'value': 29446, 'evaluated': 901672.59},\n", " {'token': \"Edgeless\", 'value': 1524, 'evaluated': 171.79},\n", " {'token': \"Golem\", 'value': 1176941, 'evaluated': 265268.49},\n", " {'token': \"Guppy\", 'value': 5, 'evaluated': 0.79},\n", " {'token': \"Melon\", 'value': 640, 'evaluated': 24606.91},\n", " {'token': \"MKR\", 'value': 2, 'evaluated': 200.27},\n", " {'token': \"SNGLS\", 'value': 12431535, 'evaluated': 1423423.19},\n", " {'token': \"SwarmCity\", 'value': 300802, 'evaluated': 420356.49},\n", " {'token': \"ETH\", 'value': 166710, 'evaluated': 13028465.35}\n", "]\n", "token_amounts = pd.DataFrame(token_amounts)\n", "token_amounts = token_amounts[['token', 'value', 'evaluated']]\n", "token_amounts" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16722425.36\n" ] } ], "source": [ "print(token_amounts.evaluated.sum())" ] }, { "cell_type": "code", "execution_count": 125, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "9 77.910142\n", "Name: evaluated, dtype: float64" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "token_amounts[token_amounts.token == 'ETH'].evaluated / token_amounts.evaluated.sum() * 100" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 127, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trace = go.Bar(\n", " x=token_amounts.token,\n", " y=token_amounts.evaluated\n", ")\n", "layout = go.Layout(\n", " xaxis=go.XAxis(\n", " title='ERC20 Token',\n", " tickfont=dict(\n", "# size=40,\n", " color='black'\n", " ),\n", " titlefont=dict(\n", "# size=50,\n", " color='black'\n", " ),\n", " ),\n", " yaxis=go.YAxis(\n", " title='USD Amount Invested',\n", " tickfont=dict(\n", "# size=40,\n", " color='black'\n", " ),\n", " titlefont=dict(\n", "# size=50,\n", " color='black'\n", " ),\n", " ),\n", ")\n", "data = [trace]\n", "fig = go.Figure(data=data, layout=layout)\n", "# iplot(fig)\n", "py.iplot(fig, filename='TokenCard_token_breakdown')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "gist": { "data": { "description": "tokencard_analysis.ipynb", "public": true }, "id": "" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "latex_envs": { "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 0 }, "toc": { "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }