SaurabhPatel9651

Aragon ICO analysis

Sep 30th, 2017
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # coding: utf-8
  2. # Visit for full code and support 'https://github.com/corpetty/py-etherscan-api' also, "https://github.com/corpetty/ICO_analysis"
  3.  
  4. # In[1]: #Original Code by Corey Petty (twitter @corpetty)
  5.  
  6. # Needed for adding my version of py_etherscan_api package on personal Mac
  7. import sys
  8. sys.path.append('/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/')
  9.  
  10.  
  11. # In[1]:
  12.  
  13. import etherscan.accounts as accounts
  14. import pandas as pd
  15. import json
  16.  
  17.  
  18. # In[2]:
  19.  
  20. from plotly import __version__
  21. from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
  22. from plotly.offline import plot
  23. from plotly.graph_objs import Scatter, Data, Box, Bar
  24. from plotly.graph_objs import Scattergl
  25. init_notebook_mode() # run at the start of every ipython notebook to use plotly.offline
  26.                      # this injects the plotly.js source files into the notebook
  27. from plotly.tools import FigureFactory as FF
  28. import plotly.plotly as py
  29. import plotly.graph_objs as go
  30. from plotly import tools
  31.  
  32. get_ipython().magic('matplotlib inline')
  33. import matplotlib as plt
  34. plt.style.use('ggplot')
  35.  
  36.  
  37. # In[5]:
  38. # This module is written as an effort to provide python bindings to the EtherScan.io API, which can be found at: https://etherscan.io/apis In order to use this, you must attain an Etherscan user account, and generate an API key.
  39.  
  40. #In order to use the API, you must provide an API key at runtime, which can be found at the Etherscan.io API website. If you'd like to #use the provided examples without altering them, then the JSON file api_key.json must be stored in the base directory. Its format is #as follows: { "key" : "YourApiKeyToken" } with YourApiKeyToken is your provided API key token from EtherScan.io
  41.  
  42. with open('..\api_key.json', 'r') as key_file:
  43.     key = json.loads(key_file.read())['key']
  44.  
  45.  
  46. # In[6]:
  47.  
  48. api = accounts.Account(address="0x960b236A07cf122663c4303350609A66A7B288C0", api_key=key)
  49.  
  50.  
  51. # In[7]:
  52.  
  53. transactions = api.get_all_transactions()
  54. df = pd.DataFrame(transactions)
  55.  
  56.  
  57. # In[7]:
  58.  
  59. df.value = df.value.astype(float)/ 1000000000000000000
  60. df.timeStamp = pd.to_datetime(df.timeStamp, unit='s')
  61. df.blockNumber = df.blockNumber.astype(int)
  62.  
  63.  
  64. # In[8]:
  65.  
  66. # Check total of successful ETH transactions
  67. df[df.isError == '0'].value.sum()
  68.  
  69.  
  70. # In[9]:
  71.  
  72. df[(df.value != 0) & (df.isError == '0')].head()
  73.  
  74.  
  75. # In[10]:
  76.  
  77. df[(df.value != 0) & (df.isError == '0')]['from'].value_counts()
  78.  
  79.  
  80. # In[11]:
  81.  
  82. combined_df = pd.DataFrame(df[(df.value != 0) & (df.isError == '0')].groupby('from').sum().sort_values(by='value'))
  83. size_df = pd.DataFrame(df[(df.value != 0) & (df.isError == '0')].groupby('from').size())
  84.  
  85.  
  86. # In[12]:
  87.  
  88. pd.DataFrame(df[(df.value != 0) & (df.isError == '0')].groupby('from').size()).iloc[:,0].max()
  89.  
  90.  
  91. # In[13]:
  92.  
  93. combined_df.join(size_df).sort_values('value', ascending=False)
  94.  
  95.  
  96. # In[14]:
  97.  
  98. combined_df.sum()
  99.  
  100.  
  101. # In[15]:
  102.  
  103. print('total txs:        {}'.format(len(df)))
  104. print('total value txs:  {}'.format(len(df[(df.value != 0) & (df.isError == '0')])))
  105. print('total error txs:  {}'.format(len(df[df.isError == '1'])))
  106. print('total zero txs:   {}'.format(len(df[df.value == 0])))
  107. print('unique address:   {}'.format(len(combined_df)))
  108.  
  109.  
  110. # In[16]:
  111.  
  112. block_df = pd.DataFrame(df[df.isError == 0].groupby('timeStamp').sum().reset_index())
  113. block_df.value_cum = block_df.value.cumsum()
  114.  
  115.  
  116. # In[17]:
  117.  
  118. mask1 = (df.isError == '0') & (df.blockNumber >= 3723000) & (df.blockNumber <= 3723134) & (df.value !=0)
  119. mask2 = (df.isError == '1') & (df.blockNumber >= 3723000) & (df.blockNumber <= 3723134) & (df.value !=0)
  120. trace = go.Bar(
  121.     x=df[mask1].timeStamp,
  122.     y=df[mask1].value,
  123.     name="successful",
  124. #     marker=dict(
  125. #         line=dict(
  126. #             width=10,
  127. #             color='#1f77b4',
  128. #          ),
  129. #     ),
  130.  
  131. )
  132. trace2 = go.Bar(
  133.     x=df[mask2].timeStamp,
  134.     y=df[mask2].value,
  135.     name="failed",
  136. #     marker=dict(
  137. #         line=dict(
  138. #             width=10,
  139. #             color='#ff7f0e',
  140. #          ),
  141. #     ),
  142.  
  143. )
  144. layout = go.Layout(
  145.     xaxis=go.XAxis(
  146.         title='Time of Investment',
  147. #         titlefont = dict(
  148. #             size = 40,
  149. #         ),
  150. #         tickfont = dict(
  151. #             size = 40
  152. #         )
  153.     ),
  154.     yaxis=go.YAxis(
  155.         title='ETH Invested',
  156. #         titlefont = dict(
  157. #             size = 40,
  158. #         ),
  159. #         tickfont = dict(
  160. #             size = 40
  161. #         )
  162.     ),
  163.     legend = {
  164.         'x':0.8,
  165.         'y':1,
  166. #         'font' : dict(
  167. #             size = 40
  168. #         )
  169.     },
  170. #     margin={
  171. #         'l': 200,
  172. #         'r': 100,
  173. #         'b': 200,
  174. #     },
  175. #     height=1500,
  176. #     width=3000
  177. #    barmode='stack'
  178. )
  179. data = [trace, trace2]
  180. fig = go.Figure(data=data, layout=layout)
  181. # iplot(fig)
  182. py.iplot(fig, filename='Aragon_TimeSeries_Invested')
  183.  
  184.  
  185. # In[18]:
  186.  
  187. combined = df[mask1].groupby('from').sum().reset_index()
  188.  
  189.  
  190. # In[19]:
  191.  
  192. def label_exp_group(value):
  193.     labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
  194.     for label in labels:
  195.         if value <= 10**float(label):
  196.             return label
  197.        
  198. combined['exp_group'] = combined.value.apply(label_exp_group)
  199.  
  200.  
  201. # In[22]:
  202.  
  203. grouped = pd.DataFrame(combined.groupby('exp_group').sum())
  204. grouped = grouped[['value']]
  205. grouped_size = pd.DataFrame(combined.groupby('exp_group').size())
  206. grouped['size'] = grouped_size[0].values
  207. grouped['size_perc'] = [trans / len(combined) * 100 for trans in grouped['size'].values]
  208. grouped['value_perc'] = [value / grouped.value.sum() * 100 for value in grouped.value.values]
  209.  
  210.  
  211. # In[23]:
  212.  
  213. grouped
  214.  
  215.  
  216. # In[24]:
  217.  
  218. trace = go.Bar(
  219.     y=grouped.value_perc,
  220.     name='Total ETH'
  221. )
  222. trace2 = go.Bar(
  223.     y=grouped.size_perc,
  224.     name='Addresses',
  225. )
  226. data = Data([trace, trace2])
  227. layout = go.Layout(
  228.     xaxis=go.XAxis(
  229.         title='Investor Group',
  230.         ticktext = [
  231.                 "$0 < x < 10^0$",
  232.                 "$10^0 < x < 10^1$",
  233.                 "$10^1 < x < 10^2$",
  234.                 "$10^2 < x < 10^3$",
  235.                 "$10^3 < x < 10^4$",
  236.                 "$10^4 < x < 10^5$",
  237.                 "$10^5 < x < 10^6$",
  238.                 "Exchange"
  239.             ],
  240.         tickvals = [ 0, 1, 2, 3, 4, 5,6 ],
  241.         tickfont=dict(
  242. #             size=40,
  243.             color='black'
  244.         ),
  245.         titlefont=dict(
  246. #             size=50,
  247.             color='black'
  248.         ),
  249.     ),
  250.     yaxis=go.YAxis(
  251.         title='Percentage',
  252.         tickfont=dict(
  253. #             size=40,
  254.             color='black'
  255.         ),
  256.         titlefont=dict(
  257. #             size=50,
  258.             color='black'
  259.         ),
  260.     ),
  261.     legend=dict(
  262.         x=0,
  263.         y=1,
  264.         font=dict(
  265. #             size=40,
  266.             color='black'
  267.         ),
  268.     ),
  269. #     height=1500,
  270. #     width=3000,
  271. #      margin={
  272. #         'b': 400,
  273. #         'l': 200
  274. #     },
  275. #    barmode='stack'
  276. )
  277. fig = go.Figure(data=data, layout=layout)
  278.  
  279. # iplot(fig)
  280. py.iplot(fig, filename='Aragon_investor_percentage_breakdown')
  281.  
  282.  
  283. # In[65]:
  284.  
  285. import requests
  286.  
  287.  
  288. # In[67]:
  289.  
  290. def get_token_amount(token, address):
  291.     req = requests.get(url='https://api.etherscan.io/api?module=account&action=tokenbalance&tokenname='+token+'&address='+address+'&tag=latest&apikey='+key)
  292.     try:
  293.         print(token, ' amount: ', float(json.loads(req.text)['result'])/1e18)
  294.     except:
  295.         print('token not available')
  296.  
  297.  
  298. # In[68]:
  299.  
  300. address='0x960b236A07cf122663c4303350609A66A7B288C0'
  301. tokens = ['REP', 'DGD', 'MKR', 'GNT', 'MLN', 'SWT', 'SNGLS']
  302. for token in tokens:
  303.     get_token_amount(token, address)
  304.    
  305.  
  306.  
  307. # ## Lost Fees
  308.  
  309. # In[47]:
  310.  
  311. df.gasUsed = df.gasUsed.astype(int)
  312. df[(df.blockNumber < '3723000')].gasUsed.sum()
  313.  
  314.  
  315. # In[51]:
  316.  
  317. len(df[(df.blockNumber < '3723000') & (df.value != 0)])
  318.  
  319.  
  320. # In[50]:
  321.  
  322. df.loc[0]
  323.  
  324.  
  325. # ## Last Block and Timeframe
  326.  
  327. # In[62]:
  328.  
  329. df[(df.isError == '0') & (df.value != 0)]['blockNumber'].values[-1]
  330.  
  331.  
  332. # In[64]:
  333.  
  334. (3723134 - 3723000) * 15 / 60
  335.  
  336.  
  337. # In[97]:
  338.  
  339. df[df['from'] == '0xfbb1b73c4f0bda4f67dca266ce6ef42f520fbb98'].hash.values[0]
  340.  
  341.  
  342. # In[98]:
  343.  
  344. df[df['from'] == '0xfbb1b73c4f0bda4f67dca266ce6ef42f520fbb98'].value.values[0]
  345.  
  346.  
  347. # ## Out of Gas
  348.  
  349. # In[113]:
  350.  
  351. df.gas = df.gas.astype(int)
  352. df.gasUsed = df.gasUsed.astype(int)
  353. df.cumulativeGasUsed = df.cumulativeGasUsed.astype(int)
  354. df[df.isError == '1'].head()
  355.  
  356.  
  357. # In[122]:
  358.  
  359. print(len(df[(df.isError == '1') & (df.gasUsed == df.gas) &  (df.blockNumber > 3723000) & (df.blockNumber <= 3723134) ]))
  360. df[(df.isError == '1') & (df.gasUsed == df.gas) &  (df.blockNumber > 3723000) & (df.blockNumber <= 3723134) ].value.sum()
  361.  
  362.  
  363. # In[ ]:
  364.  
  365. 6593
RAW Paste Data