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
7 changes: 6 additions & 1 deletion etherscan/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def connect(self):
# Check for empty response
if req.text:
data = req.json()
if data.get('status') == '1':
status = data.get('status')
if status == '1' or self.check_keys_api(data):
return data
else:
raise EmptyResponse(data.get('message', 'no message'))
Expand All @@ -125,3 +126,7 @@ def check_and_get_api(self):
pass
else:
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'))

60 changes: 60 additions & 0 deletions etherscan/proxies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .client import Client
from typing import Union


class Proxies(Client):
Expand All @@ -11,3 +12,62 @@ 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.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]):
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.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]):
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.build_url()
req = self.connect()
return req['result']

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

def get_transaction_by_blocknumber_index(self,
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.INDEX] = index if type(index) is str else hex(index)
self.build_url()
req = self.connect()
return req['result']

def get_transaction_count(self, address: str):
self.url_dict[self.ACTION] = 'eth_getTransactionCount'
self.url_dict[self.ADDRESS] = address
self.url_dict[self.TAG] = 'latest'
self.build_url()
req = self.connect()
return req['result']

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


10 changes: 10 additions & 0 deletions examples/proxies/get_block_by_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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'])
10 changes: 10 additions & 0 deletions examples/proxies/get_block_transaction_count_by_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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))
9 changes: 9 additions & 0 deletions examples/proxies/get_most_recent_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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))
10 changes: 10 additions & 0 deletions examples/proxies/get_transaction_by_blocknumber_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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'])
10 changes: 10 additions & 0 deletions examples/proxies/get_transaction_by_hash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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_hash(
tx_hash='0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1')
print(transaction['hash'])
10 changes: 10 additions & 0 deletions examples/proxies/get_transaction_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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))
9 changes: 9 additions & 0 deletions examples/proxies/get_transaction_receipt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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)
9 changes: 9 additions & 0 deletions examples/proxies/get_uncle_by_blocknumber_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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)
uncles = api.get_uncle_by_blocknumber_index(block_number='0x210A9B', index='0x0')
print(uncles['uncles'])
1 change: 1 addition & 0 deletions pip-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests==2.18.4
typing==3.6.4
10 changes: 5 additions & 5 deletions tests/test_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def test_get_most_recent_block(self):
api = Proxies(api_key=API_KEY)
# currently raises an exception even though it should not, see:
# https://github.com/corpetty/py-etherscan-api/issues/32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We forgot to remove that comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am currently working on cleaning up the code a bit. I'll remove that in my pr

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thanks!
Also I'm not getting why Travis didn't fail on the linter when it should have complained. Running tox locally I see some valid complain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just remove the comment then?

Also, If the proxy apis fail you get this:

{
    jsonrpc: "2.0",
    id: 1,
    error: {
        code: -32602,
        message: "[some error message]"
    }
}

I don't think this is accounted for in this PR. However, I think #8 would fix this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the linter still just pointing at the tests folder only?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the two lines comments were with a self.assertRaises(EmptyResponse) to describe a bug. But the bug was fixed and assert removed in 06f7b04, but the comment was left. So yes basically just removing that two comment is enough.
Also you can delete the print, I don't think we need print in unit tests. Plus I think we should avoid making actual network calls, but actual mock it, but that's a different story.

I'm not sure regarding the rainy scenario. with error code and message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True for the linter it's currently only tests folder, but that one is also failing. e.g. tests/test_proxies.py:4:1: F401 'etherscan.client.EmptyResponse' imported but unused https://github.com/corpetty/py-etherscan-api/blob/06f7b04/tests/test_proxies.py#L4 and other couple of more. So really not sure why Travis didn't catch these obvious ones.
They don't pop up when you run tox on your dev env?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just ran flake8?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I ran tox that rans flake8 for you.

tox
GLOB sdist-make: /home/andre/workspace/py-etherscan-api/setup.py
pep8 inst-nodeps: /home/andre/workspace/py-etherscan-api/.tox/dist/py_etherscan_api-0.7.0.zip
pep8 installed: certifi==2018.4.16,chardet==3.0.4,flake8==3.5.0,idna==2.6,mccabe==0.6.1,py-etherscan-api==0.7.0,pycodestyle==2.3.1,pyflakes==1.6.0,requests==2.18.4,urllib3==1.22
pep8 runtests: PYTHONHASHSEED='913872812'
pep8 runtests: commands[0] | flake8 tests/
tests/test_proxies.py:4:1: F401 'etherscan.client.EmptyResponse' imported but unused
tests/test_proxies.py:20:1: W391 blank line at end of file
tests/test_proxies.py:20:1: W293 blank line contains whitespace
ERROR: InvocationError: '/home/andre/workspace/py-etherscan-api/.tox/pep8/bin/flake8 tests/'
py3 inst-nodeps: /home/andre/workspace/py-etherscan-api/.tox/dist/py_etherscan_api-0.7.0.zip
py3 installed: atomicwrites==1.1.5,attrs==18.1.0,certifi==2018.4.16,chardet==3.0.4,idna==2.6,more-itertools==4.2.0,pluggy==0.6.0,py==1.5.3,py-etherscan-api==0.7.0,pytest==3.6.2,requests==2.18.4,six==1.11.0,typing==3.6.4,urllib3==1.22
py3 runtests: PYTHONHASHSEED='913872812'
py3 runtests: commands[0] | python -m unittest discover --start-directory=tests/
5848317
/usr/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.130', 42938), raddr=('23.111.151.66', 443)>
  testMethod()
./usr/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.130', 42940), raddr=('23.111.151.66', 443)>
  testMethod()
./usr/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed <ssl.SSLSocket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('192.168.1.130', 42942), raddr=('23.111.151.66', 443)>
  testMethod()
.
----------------------------------------------------------------------
Ran 3 tests in 2.913s

OK
_____________________________________________________________________________________________________________________________________ summary _____________________________________________________________________________________________________________________________________
ERROR:   pep8: commands failed
  py3: commands succeeded

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay just ran that. Got the same results. When should we switch flake8 to the whole directory?

with self.assertRaises(EmptyResponse):
most_recent = int(api.get_most_recent_block(), 16)
print(most_recent)
p = re.compile('^[0-9]{7}$')
self.assertTrue(p.match(str(most_recent)))
most_recent = int(api.get_most_recent_block(), 16)
print(most_recent)
p = re.compile('^[0-9]{7}$')
self.assertTrue(p.match(str(most_recent)))