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
34 changes: 24 additions & 10 deletions etherscan/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@


class Account(Client):
PAGE_NUM_PATTERN = re.compile(
'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0')

def __init__(self, address=Client.dao_address, api_key='YourApiKeyToken'):
Client.__init__(self, address=address, api_key=api_key)
self.url_dict[self.MODULE] = 'account'
Expand All @@ -21,9 +24,11 @@ def get_balance_multiple(self):
req = self.connect()
return req['result']

def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False) -> list:
def get_transaction_page(self, page=1, offset=10000, sort='asc',
internal=False) -> list:
"""
Get a page of transactions, each transaction returns list of dict with keys:
Get a page of transactions, each transaction
returns list of dict with keys:
nonce
hash
cumulativeGasUsed
Expand Down Expand Up @@ -62,7 +67,8 @@ def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False)
req = self.connect()
return req['result']

def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list:
def get_all_transactions(self, offset=10000, sort='asc',
internal=False) -> list:
if internal:
self.url_dict[self.ACTION] = 'txlistinternal'
else:
Expand All @@ -77,19 +83,23 @@ def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list
self.build_url()
req = self.connect()
if "No transactions found" in req['message']:
print("Total number of transactions: {}".format(len(trans_list)))
print(
"Total number of transactions: {}".format(len(trans_list)))
self.page = ''
return trans_list
else:
trans_list += req['result']
# Find any character block that is a integer of any length
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE])
page_number = re.findall(Account.PAGE_NUM_PATTERN,
self.url_dict[self.PAGE])
print("page {} added".format(page_number[0]))
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)

def get_blocks_mined_page(self, blocktype='blocks', page=1, offset=10000) -> list:
def get_blocks_mined_page(self, blocktype='blocks', page=1,
offset=10000) -> list:
"""
Get a page of blocks mined by given address, returns list of dict with keys:
Get a page of blocks mined by given address,
returns list of dict with keys:
blockReward (in wei)
blockNumber
timeStamp
Expand Down Expand Up @@ -117,12 +127,15 @@ def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list:
req = self.connect()
print(req['message'])
if "No transactions found" in req['message']:
print("Total number of blocks mined: {}".format(len(blocks_list)))
print(
"Total number of blocks mined: {}".format(
len(blocks_list)))
return blocks_list
else:
blocks_list += req['result']
# Find any character block that is a integer of any length
page_number = re.findall(r'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0', self.url_dict[self.PAGE])
page_number = re.findall(Account.PAGE_NUM_PATTERN,
self.url_dict[self.PAGE])
print("page {} added".format(page_number[0]))
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)

Expand All @@ -135,6 +148,7 @@ def get_internal_by_hash(self, tx_hash=''):

def update_transactions(self, address, trans):
"""
Gets last page of transactions (last 10k trans) and updates current trans book (book)
Gets last page of transactions (last 10k trans)
and updates current trans book (book)
"""
pass
28 changes: 16 additions & 12 deletions etherscan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class BadRequest(ClientException):
"""Invalid request passed"""


# Assume user puts his API key in the api_key.json file under variable name "key"
# Assume user puts his API key in the api_key.json
# file under variable name "key"
class Client(object):
dao_address = '0xbb9bc244d798123fde783fcc1c72d3bb8c189413'

Expand Down Expand Up @@ -65,7 +66,6 @@ class Client(object):
def __init__(self, address, api_key=''):
self.http = requests.session()
self.url_dict = collections.OrderedDict([

(self.MODULE, ''),
(self.ADDRESS, ''),
(self.OFFSET, ''),
Expand All @@ -86,12 +86,12 @@ def __init__(self, address, api_key=''):
(self.TAG, ''),
(self.BOOLEAN, ''),
(self.INDEX, ''),
(self.API_KEY, api_key)]
)
(self.API_KEY, api_key)])

# Var initialization should take place within init
self.url = None

# self.url_dict[API_KEY] = str(api_key)
self.check_and_get_api()
# self.key = self.URL_BASES['key'] + self.API_KEY

if (len(address) > 20) and (type(address) == list):
raise BadRequest("Etherscan only takes 20 addresses at a time")
Expand All @@ -101,7 +101,9 @@ def __init__(self, address, api_key=''):
self.url_dict[self.ADDRESS] = address

def build_url(self):
self.url = self.PREFIX + ''.join([param + val if val else '' for param, val in self.url_dict.items()])
self.url = self.PREFIX + ''.join(
[param + val if val else '' for param, val in
self.url_dict.items()])

def connect(self):
# TODO: deal with "unknown exception" error
Expand All @@ -119,14 +121,16 @@ def connect(self):
return data
else:
raise EmptyResponse(data.get('message', 'no message'))
raise BadRequest("Problem with connection, status code: %s" % req.status_code)
raise BadRequest(
"Problem with connection, status code: %s" % req.status_code)

def check_and_get_api(self):
if self.url_dict[self.API_KEY]: # Check if api_key is empty string
pass
else:
self.url_dict[self.API_KEY] = input('Please type your EtherScan.io API key: ')
self.url_dict[self.API_KEY] = input(
'Please type your EtherScan.io API key: ')

def check_keys_api(self, data):
return all (k in data for k in ('jsonrpc', 'id', 'result'))

@staticmethod
def check_keys_api(data):
return all(k in data for k in ('jsonrpc', 'id', 'result'))
27 changes: 15 additions & 12 deletions etherscan/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,32 @@ def get_most_recent_block(self):
self.build_url()
req = self.connect()
return req['result']

def get_block_by_number(self, block_number: Union[str, int]):
self.url_dict[self.ACTION] = 'eth_getBlockByNumber'
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
self.url_dict[self.TAG] = block_number if type(
block_number) is str else hex(block_number)
self.url_dict[self.BOOLEAN] = 'true'
self.build_url()
req = self.connect()
return req['result']

def get_uncle_by_blocknumber_index(self,
block_number: Union[str, int],
index: Union[str, int]):
block_number: Union[str, int],
index: Union[str, int]):
self.url_dict[self.ACTION] = 'eth_getUncleByBlockNumberAndIndex'
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
self.url_dict[self.TAG] = block_number if type(
block_number) is str else hex(block_number)
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
self.build_url()
req = self.connect()
return req['result']

def get_block_transaction_count_by_number(self, block_number: Union[str, int]):
def get_block_transaction_count_by_number(self,
block_number: Union[str, int]):
self.url_dict[self.ACTION] = 'eth_getBlockTransactionCountByNumber'
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
self.url_dict[self.TAG] = block_number if type(
block_number) is str else hex(block_number)
self.build_url()
req = self.connect()
return req['result']
Expand All @@ -46,10 +50,11 @@ def get_transaction_by_hash(self, tx_hash: str):
return req['result']

def get_transaction_by_blocknumber_index(self,
block_number: Union[str, int],
index: Union[str, int]):
block_number: Union[str, int],
index: Union[str, int]):
self.url_dict[self.ACTION] = 'eth_getTransactionByBlockNumberAndIndex'
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
self.url_dict[self.TAG] = block_number if type(
block_number) is str else hex(block_number)
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
self.build_url()
req = self.connect()
Expand All @@ -69,5 +74,3 @@ def get_transaction_receipt(self, tx_hash: str):
self.build_url()
req = self.connect()
return req['result']


4 changes: 2 additions & 2 deletions examples/accounts/Accounts Examples Notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3.0
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
Expand Down Expand Up @@ -489,4 +489,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}
2 changes: 1 addition & 1 deletion examples/accounts/get_all_blocks_mined.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

api = Account(address=address, api_key=key)
blocks = api.get_all_blocks_mined(offset=10000, blocktype='uncles')
print(blocks)
print(blocks)
6 changes: 3 additions & 3 deletions examples/accounts/get_all_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
with open('../../api_key.json', mode='r') as key_file:
key = json.loads(key_file.read())['key']

# address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
address = '0x49edf201c1e139282643d5e7c6fb0c7219ad1db7'

api = Account(address=address, api_key=key)
transactions = api.get_all_transactions(offset=10000, sort='asc', internal=True)
transactions = api.get_all_transactions(offset=10000, sort='asc',
internal=False)

print(transactions[0])
print(transactions[0])
1 change: 0 additions & 1 deletion examples/accounts/get_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
api = Account(address=address, api_key=key)
balance = api.get_balance()
print(balance)

5 changes: 3 additions & 2 deletions examples/accounts/get_balance_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
with open('../../api_key.json', mode='r') as key_file:
key = json.loads(key_file.read())['key']

address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']

api = Account(address=address, api_key=key)
balances = api.get_balance_multiple()
print(balances)
print(balances)
2 changes: 1 addition & 1 deletion examples/accounts/get_blocks_mined.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

api = Account(address=address, api_key=key)
blocks = api.get_blocks_mined_page(page=1, offset=10000, blocktype='blocks')
print(blocks)
print(blocks)
2 changes: 1 addition & 1 deletion examples/accounts/get_transaction_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

api = Account(address=address, api_key=key)
transactions = api.get_transaction_page(page=1, offset=10000, sort='des')
print(transactions)
print(transactions)
1 change: 0 additions & 1 deletion examples/contracts/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

5 changes: 2 additions & 3 deletions examples/proxies/get_block_by_number.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

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


api = Proxies(api_key=key)
block = api.get_block_by_number(5747732)
print(block['number'])
print(block['number'])
5 changes: 2 additions & 3 deletions examples/proxies/get_block_transaction_count_by_number.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

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


api = Proxies(api_key=key)
tx_count = api.get_block_transaction_count_by_number(block_number='0x10FB78')
print(int(tx_count, 16))
print(int(tx_count, 16))
4 changes: 2 additions & 2 deletions examples/proxies/get_most_recent_block.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

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

api = Proxies(api_key=key)
block = api.get_most_recent_block()
print(int(block, 16))
8 changes: 4 additions & 4 deletions examples/proxies/get_transaction_by_blocknumber_index.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

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


api = Proxies(api_key=key)
transaction = api.get_transaction_by_blocknumber_index(block_number='0x57b2cc', index='0x2')
print(transaction['transactionIndex'])
transaction = api.get_transaction_by_blocknumber_index(block_number='0x57b2cc',
index='0x2')
print(transaction['transactionIndex'])
8 changes: 4 additions & 4 deletions examples/proxies/get_transaction_by_hash.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

with open('../../api_key.json', mode='r') as key_file:
key = json.loads(key_file.read())['key']
TX_HASH = '0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1'
api = Proxies(api_key=key)
transaction = api.get_transaction_by_hash(
tx_hash='0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1')
print(transaction['hash'])
tx_hash=TX_HASH)
print(transaction['hash'])
5 changes: 2 additions & 3 deletions examples/proxies/get_transaction_count.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

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


api = Proxies(api_key=key)
count = api.get_transaction_count('0x6E2446aCfcec11CC4a60f36aFA061a9ba81aF7e0')
print(int(count, 16))
print(int(count, 16))
7 changes: 4 additions & 3 deletions examples/proxies/get_transaction_receipt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from etherscan.proxies import Proxies
from etherscan.proxies import Proxies
import json

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

api = Proxies(api_key=key)
receipt = api.get_transaction_receipt('0xb03d4625fd433ad05f036abdc895a1837a7d838ed39f970db69e7d832e41205d')
print(receipt)
receipt = api.get_transaction_receipt(
'0xb03d4625fd433ad05f036abdc895a1837a7d838ed39f970db69e7d832e41205d')
print(receipt)
Loading