<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "Get the transaction history of the ERC20 tokens. \n",
    "Author: <NAME>\n",
    "License: MIT\n",
    "Powered by Etherscan.io APIs\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import urllib3\n",
    "#disable the annoying security warnings\n",
    "urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n",
    "import sys\n",
    "import json\n",
    "import collections\n",
    "import pandas as pd\n",
    "#the rate control module\n",
    "import ratel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#the base url for etherscan api\n",
    "baseUrl='https://api.etherscan.io'\n",
    "#the connection pool\n",
    "pool=urllib3.PoolManager(5)\n",
    "#number of threads\n",
    "numthread=5\n",
    "\n",
    "#get the API key\n",
    "key=''\n",
    "keyarr=[]\n",
    "keyfile='../Credentials/etherscankey.txt'\n",
    "with open(keyfile) as f:\n",
    "    keyarr = f.readlines()\n",
    "if len(keyarr)==0 or keyarr[0] == '':\n",
    "    print \"API Key Not Found in Directory! Aborting ...\"\n",
    "    sys.exit(0)\n",
    "key=keyarr[0]\n",
    "\n",
    "#limit the rate that the api could be called (5 times per second)\n",
    "@ratel.rate_limited(5,1)\n",
    "def call_api(url):\n",
    "    '''\n",
    "    Make API calls \n",
    "    '''    \n",
    "    global pool\n",
    "    r=pool.request('GET',url)\n",
    "    if r.status != 200:\n",
    "        raise ApiError('Cannot call API: {}'.format(response.status_code))\n",
    "    return r\n",
    "\n",
    "#convert dict items to strings\n",
    "def convert(data):\n",
    "    if isinstance(data, basestring):\n",
    "        return str(data)\n",
    "    elif isinstance(data, collections.Mapping):\n",
    "        return dict(map(convert, data.iteritems()))\n",
    "    elif isinstance(data, collections.Iterable):\n",
    "        return type(data)(map(convert, data))\n",
    "    else:\n",
    "        return data\n",
    "\n",
    "def getHistory(fromBlock, toBlock, contractAdd, topic):\n",
    "    global key\n",
    "    r=call_api(\"https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=3957992&toBlock=10000000&address=0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef&apikey=<KEY>\")\n",
    "    html=r.data\n",
    "    #remove the escaped string\n",
    "    o=json.loads(html)\n",
    "    o=convert(o)\n",
    "    df=pd.DataFrame.from_dict(o[\"result\"])\n",
    "    #convert block number from hex to int\n",
    "    df.blockNumber=df.blockNumber.apply(lambda x: int(x, 16))\n",
    "    #convert transfer value to int\n",
    "    df.data=df.data.apply(lambda x: int(x, 16)/(10**18))\n",
    "    #convert data value from hex to int\n",
    "    for index, row in df.iterrows():\n",
    "        print row[\"data\"]\n",
    "    \n",
    "getHistory(1,2,3,4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",<commit_msg>convert the from and to addresses<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "Get the transaction history of the ERC20 tokens. \n",
    "Author: <NAME>\n",
    "License: MIT\n",
    "Powered by Etherscan.io APIs\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import urllib3\n",
    "#disable the annoying security warnings\n",
    "urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n",
    "import sys\n",
    "import json\n",
    "import collections\n",
    "import pandas as pd\n",
    "#the rate control module\n",
    "import ratel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#the base url for etherscan api\n",
    "baseUrl='https://api.etherscan.io'\n",
    "#the connection pool\n",
    "pool=urllib3.PoolManager(5)\n",
    "#number of threads\n",
    "numthread=5\n",
    "\n",
    "#get the API key\n",
    "key=''\n",
    "keyarr=[]\n",
    "keyfile='../Credentials/etherscankey.txt'\n",
    "with open(keyfile) as f:\n",
    "    keyarr = f.readlines()\n",
    "if len(keyarr)==0 or keyarr[0] == '':\n",
    "    print \"API Key Not Found in Directory! Aborting ...\"\n",
    "    sys.exit(0)\n",
    "key=keyarr[0]\n",
    "\n",
    "#limit the rate that the api could be called (5 times per second)\n",
    "@ratel.rate_limited(5,1)\n",
    "def call_api(url):\n",
    "    '''\n",
    "    Make API calls \n",
    "    '''    \n",
    "    global pool\n",
    "    r=pool.request('GET',url)\n",
    "    if r.status != 200:\n",
    "        raise ApiError('Cannot call API: {}'.format(response.status_code))\n",
    "    return r\n",
    "\n",
    "#convert dict items to strings\n",
    "def convert(data):\n",
    "    if isinstance(data, basestring):\n",
    "        return str(data)\n",
    "    elif isinstance(data, collections.Mapping):\n",
    "        return dict(map(convert, data.iteritems()))\n",
    "    elif isinstance(data, collections.Iterable):\n",
    "        return type(data)(map(convert, data))\n",
    "    else:\n",
    "        return data\n",
    "\n",
    "def convert_hex_add_wrapper(arr):\n",
    "    arr[1]=hex_add_converter(arr[1])\n",
    "    arr[2]=hex_add_converter(arr[2])\n",
    "    return arr\n",
    "    \n",
    "def hex_add_converter(add):\n",
    "    return add[0:2]+add[26:]\n",
    "    \n",
    "def getHistory(fromBlock, toBlock, contractAdd, topic):\n",
    "    global key\n",
    "    r=call_api(\"https://api.etherscan.io/api?module=logs&action=getLogs&fromBlock=3957992&toBlock=10000000&address=0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef&apikey=<KEY>\")\n",
    "    html=r.data\n",
    "    #remove the escaped string\n",
    "    o=json.loads(html)\n",
    "    o=convert(o)\n",
    "    df=pd.DataFrame.from_dict(o[\"result\"])\n",
    "    #convert block number from hex to int\n",
    "    df.blockNumber=df.blockNumber.apply(lambda x: int(x, 16))\n",
    "    #convert transfer value to int\n",
    "    df.data=df.data.apply(lambda x: int(x, 16)/(10**18))\n",
    "    #convert the address\n",
    "    df.topics=df.topics.apply(lambda x: convert_hex_add_wrapper(x))\n",
    "    #convert data value from hex to int\n",
    "    for index, row in df.iterrows():\n",
    "        print row['topics']\n",
    "        print row['topics']\n",
    "  \n",
    "getHistory(1,2,3,4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
{'fraction_non_alphanumeric': 0.19817158931082982, 'fraction_numerical': 0.031786216596343175, 'mean_word_length': 2.5950455005055613, 'pattern_counts': {'":': 40, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5579837', 'n_tokens_mistral': 2848, 'n_tokens_neox': 2731, 'n_words': 500}