From 88d2f06a5f5a7b8ae2581660d123821de5d866e1 Mon Sep 17 00:00:00 2001 From: agatsoh Date: Mon, 11 Jun 2018 16:34:19 +0530 Subject: [PATCH 1/2] Adding proxy API's for etherscan --- etherscan/client.py | 2 +- etherscan/proxies.py | 60 +++++++++++++++++++ examples/proxies/get_block_by_number.py | 10 ++++ .../get_block_transaction_count_by_number.py | 10 ++++ examples/proxies/get_most_recent_block.py | 9 +++ .../get_transaction_by_blocknumber_index.py | 10 ++++ examples/proxies/get_transaction_by_hash.py | 10 ++++ examples/proxies/get_transaction_count.py | 10 ++++ examples/proxies/get_transaction_receipt.py | 9 +++ .../proxies/get_uncle_by_blocknumber_index.py | 9 +++ pip-requirements.txt | 1 + 11 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 examples/proxies/get_block_by_number.py create mode 100644 examples/proxies/get_block_transaction_count_by_number.py create mode 100644 examples/proxies/get_most_recent_block.py create mode 100644 examples/proxies/get_transaction_by_blocknumber_index.py create mode 100644 examples/proxies/get_transaction_by_hash.py create mode 100644 examples/proxies/get_transaction_count.py create mode 100644 examples/proxies/get_transaction_receipt.py create mode 100644 examples/proxies/get_uncle_by_blocknumber_index.py diff --git a/etherscan/client.py b/etherscan/client.py index 96847a5..4fe5a66 100644 --- a/etherscan/client.py +++ b/etherscan/client.py @@ -114,7 +114,7 @@ def connect(self): # Check for empty response if req.text: data = req.json() - if data.get('status') == '1': + if data.get('status') == '1' or 'result' in data: return data else: raise EmptyResponse(data.get('message', 'no message')) diff --git a/etherscan/proxies.py b/etherscan/proxies.py index 4c24039..ee29733 100644 --- a/etherscan/proxies.py +++ b/etherscan/proxies.py @@ -1,4 +1,5 @@ from .client import Client +from typing import Union class Proxies(Client): @@ -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'] + + diff --git a/examples/proxies/get_block_by_number.py b/examples/proxies/get_block_by_number.py new file mode 100644 index 0000000..e55e525 --- /dev/null +++ b/examples/proxies/get_block_by_number.py @@ -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']) \ No newline at end of file diff --git a/examples/proxies/get_block_transaction_count_by_number.py b/examples/proxies/get_block_transaction_count_by_number.py new file mode 100644 index 0000000..37f3fb1 --- /dev/null +++ b/examples/proxies/get_block_transaction_count_by_number.py @@ -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)) \ No newline at end of file diff --git a/examples/proxies/get_most_recent_block.py b/examples/proxies/get_most_recent_block.py new file mode 100644 index 0000000..5ba5a03 --- /dev/null +++ b/examples/proxies/get_most_recent_block.py @@ -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)) diff --git a/examples/proxies/get_transaction_by_blocknumber_index.py b/examples/proxies/get_transaction_by_blocknumber_index.py new file mode 100644 index 0000000..bff4438 --- /dev/null +++ b/examples/proxies/get_transaction_by_blocknumber_index.py @@ -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']) \ No newline at end of file diff --git a/examples/proxies/get_transaction_by_hash.py b/examples/proxies/get_transaction_by_hash.py new file mode 100644 index 0000000..99accbf --- /dev/null +++ b/examples/proxies/get_transaction_by_hash.py @@ -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']) \ No newline at end of file diff --git a/examples/proxies/get_transaction_count.py b/examples/proxies/get_transaction_count.py new file mode 100644 index 0000000..0003c72 --- /dev/null +++ b/examples/proxies/get_transaction_count.py @@ -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)) \ No newline at end of file diff --git a/examples/proxies/get_transaction_receipt.py b/examples/proxies/get_transaction_receipt.py new file mode 100644 index 0000000..8614f9a --- /dev/null +++ b/examples/proxies/get_transaction_receipt.py @@ -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) \ No newline at end of file diff --git a/examples/proxies/get_uncle_by_blocknumber_index.py b/examples/proxies/get_uncle_by_blocknumber_index.py new file mode 100644 index 0000000..fab5455 --- /dev/null +++ b/examples/proxies/get_uncle_by_blocknumber_index.py @@ -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']) \ No newline at end of file diff --git a/pip-requirements.txt b/pip-requirements.txt index 271baf7..a88292a 100644 --- a/pip-requirements.txt +++ b/pip-requirements.txt @@ -1 +1,2 @@ requests==2.18.4 +typing==3.6.4 From 06f7b0435d2546e3c25938bf3e11f0487b4d6a20 Mon Sep 17 00:00:00 2001 From: agatsoh Date: Mon, 11 Jun 2018 18:12:07 +0530 Subject: [PATCH 2/2] Correcting test failures --- etherscan/client.py | 7 ++++++- tests/test_proxies.py | 10 +++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/etherscan/client.py b/etherscan/client.py index 4fe5a66..fbf9311 100644 --- a/etherscan/client.py +++ b/etherscan/client.py @@ -114,7 +114,8 @@ def connect(self): # Check for empty response if req.text: data = req.json() - if data.get('status') == '1' or 'result' in data: + status = data.get('status') + if status == '1' or self.check_keys_api(data): return data else: raise EmptyResponse(data.get('message', 'no message')) @@ -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')) + \ No newline at end of file diff --git a/tests/test_proxies.py b/tests/test_proxies.py index 108a410..49beced 100644 --- a/tests/test_proxies.py +++ b/tests/test_proxies.py @@ -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 - 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))) +