Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,52 @@

[![Build Status](https://secure.travis-ci.org/corpetty/py-etherscan-api.png?branch=master)](http://travis-ci.org/corpetty/py-etherscan-api) [![Join the chat at https://gitter.im/py-etherscan/Lobby](https://badges.gitter.im/py-etherscan/Lobby.svg)](https://gitter.im/py-etherscan/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)


EtherScan.io API python bindings

## Description
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. If you are interacting with a contract on the Ropsten Testnet please use
https://ropsten.etherscan.io/apis.

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. If you are interacting with a contract on the Ropsten Testnet please use
https://ropsten.etherscan.io/apis.
In order to use this, you must attain an Etherscan user account, and generate an API key.

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:
the base directory. Its format is as follows:

{ "key" : "YourApiKeyToken" }

with `YourApiKeyToken` is your provided API key token from EtherScan.io

## Installation

To install the package to your computer, simply run the following command in the base directory:

python3 -m pip install py-etherscan-api


## Available bindings

Currently, only the following Etherscan.io API modules are available:

- accounts
- contracts
- stats
- tokens
- proxies
- blocks
- transactions

The remaining available modules provided by Etherscan.io will be added eventually...

## Available Networks

Currently, this works for the following networks:

- Mainnet
- Ropsten

## Examples

All possible calls have an associated example file in the examples folder to show how to call the binding

These of course will be fleshed out with more details and explanation in time
Expand All @@ -53,15 +58,15 @@ Jupyter notebooks area also included in each directory to show all examples

- Package and submit to PyPI
- Add the following modules:
- event logs
- geth proxy
- websockets
- event logs
- geth proxy
- websockets
- Add robust documentation
- Add unit test suite
- Add request throttling based on Etherscan's suggestions


## Holla at ya' boy

BTC: 16Ny72US78VEjL5GUinSAavDwARb8dXWKG

ETH: 0x5E8047fc033499BD5d8C463ADb29f10f11165ed0
16 changes: 16 additions & 0 deletions etherscan/blocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .client import Client
from typing import Union


class Blocks(Client):
def __init__(self, api_key='YourApiKeyToken'):
Client.__init__(self, address='', api_key=api_key)
self.url_dict[self.MODULE] = 'block'

def get_block_reward(self, block_number: Union[str, int]):
self.url_dict[self.ACTION] = 'getblockreward'
self.url_dict[self.BLOCKNO] = block_number if type(
block_number) is str else str(block_number)
self.build_url()
req = self.connect()
return req['result']
21 changes: 21 additions & 0 deletions etherscan/transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .client import Client


class Transactions(Client):
def __init__(self, api_key='YourApiKeyToken'):
Client.__init__(self, address='', api_key=api_key)
self.url_dict[self.MODULE] = 'transaction'

def get_status(self, tx_hash: str):
self.url_dict[self.ACTION] = 'getstatus'
self.url_dict[self.TXHASH] = tx_hash
self.build_url()
req = self.connect()
return req['result']

def get_tx_receipt_status(self, tx_hash: str):
self.url_dict[self.ACTION] = 'gettxreceiptstatus'
self.url_dict[self.TXHASH] = tx_hash
self.build_url()
req = self.connect()
return req['result']
195 changes: 195 additions & 0 deletions examples/blocks/Blocks Examples Notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"import etherscan.blocks as blocks\n",
"import json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import our api_key"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The JSON keyfile being read in has only one line in the format:\n",
" \n",
" {\"key\" : \"YourApiKey\" }"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"with open('../../api_key.json', mode='r') as key_file:\n",
" key = json.loads(key_file.read())['key']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set up API"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"block = 2165403\n",
"api = blocks.Blocks(api_key=key)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Get the block reward"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"reward = api.get_block_reward(block)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'blockNumber': '2165403',\n",
" 'timeStamp': '1472533979',\n",
" 'blockMiner': '0x13a06d3dfe21e0db5c016c03ea7d2509f7f8d1e3',\n",
" 'blockReward': '5314181600000000000',\n",
" 'uncles': [{'miner': '0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1',\n",
" 'unclePosition': '0',\n",
" 'blockreward': '3750000000000000000'},\n",
" {'miner': '0x0d0c9855c722ff0c78f21e43aa275a5b8ea60dce',\n",
" 'unclePosition': '1',\n",
" 'blockreward': '3750000000000000000'}],\n",
" 'uncleInclusionReward': '312500000000000000'}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reward"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'5314181600000000000'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reward['blockReward']"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'312500000000000000'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reward['uncleInclusionReward']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.7.0"
},
"latex_envs": {
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 0
},
"toc": {
"nav_menu": {
"height": "121px",
"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
}
1 change: 1 addition & 0 deletions examples/blocks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__author__ = 'Corey Petty'
9 changes: 9 additions & 0 deletions examples/blocks/get_block_reward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from etherscan.blocks import Blocks
import json

with open('../../api_key.json', mode='r') as key_file:
key = json.loads(key_file.read())['key']

api = Blocks(api_key=key)
reward = api.get_block_reward(2165403)
print(reward)
Loading