Skip to content

Commit 88d2f06

Browse files
committed
Adding proxy API's for etherscan
1 parent c90962e commit 88d2f06

11 files changed

+139
-1
lines changed

etherscan/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def connect(self):
114114
# Check for empty response
115115
if req.text:
116116
data = req.json()
117-
if data.get('status') == '1':
117+
if data.get('status') == '1' or 'result' in data:
118118
return data
119119
else:
120120
raise EmptyResponse(data.get('message', 'no message'))

etherscan/proxies.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .client import Client
2+
from typing import Union
23

34

45
class Proxies(Client):
@@ -11,3 +12,62 @@ def get_most_recent_block(self):
1112
self.build_url()
1213
req = self.connect()
1314
return req['result']
15+
16+
def get_block_by_number(self, block_number: Union[str, int]):
17+
self.url_dict[self.ACTION] = 'eth_getBlockByNumber'
18+
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
19+
self.url_dict[self.BOOLEAN] = 'true'
20+
self.build_url()
21+
req = self.connect()
22+
return req['result']
23+
24+
def get_uncle_by_blocknumber_index(self,
25+
block_number: Union[str, int],
26+
index: Union[str, int]):
27+
self.url_dict[self.ACTION] = 'eth_getUncleByBlockNumberAndIndex'
28+
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
29+
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
30+
self.build_url()
31+
req = self.connect()
32+
return req['result']
33+
34+
def get_block_transaction_count_by_number(self, block_number: Union[str, int]):
35+
self.url_dict[self.ACTION] = 'eth_getBlockTransactionCountByNumber'
36+
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
37+
self.build_url()
38+
req = self.connect()
39+
return req['result']
40+
41+
def get_transaction_by_hash(self, tx_hash: str):
42+
self.url_dict[self.ACTION] = 'eth_getTransactionByHash'
43+
self.url_dict[self.TXHASH] = tx_hash
44+
self.build_url()
45+
req = self.connect()
46+
return req['result']
47+
48+
def get_transaction_by_blocknumber_index(self,
49+
block_number: Union[str, int],
50+
index: Union[str, int]):
51+
self.url_dict[self.ACTION] = 'eth_getTransactionByBlockNumberAndIndex'
52+
self.url_dict[self.TAG] = block_number if type(block_number) is str else hex(block_number)
53+
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
54+
self.build_url()
55+
req = self.connect()
56+
return req['result']
57+
58+
def get_transaction_count(self, address: str):
59+
self.url_dict[self.ACTION] = 'eth_getTransactionCount'
60+
self.url_dict[self.ADDRESS] = address
61+
self.url_dict[self.TAG] = 'latest'
62+
self.build_url()
63+
req = self.connect()
64+
return req['result']
65+
66+
def get_transaction_receipt(self, tx_hash: str):
67+
self.url_dict[self.ACTION] = 'eth_getTransactionReceipt'
68+
self.url_dict[self.TXHASH] = tx_hash
69+
self.build_url()
70+
req = self.connect()
71+
return req['result']
72+
73+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
8+
api = Proxies(api_key=key)
9+
block = api.get_block_by_number(5747732)
10+
print(block['number'])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
8+
api = Proxies(api_key=key)
9+
tx_count = api.get_block_transaction_count_by_number(block_number='0x10FB78')
10+
print(int(tx_count, 16))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
api = Proxies(api_key=key)
8+
block = api.get_most_recent_block()
9+
print(int(block, 16))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
8+
api = Proxies(api_key=key)
9+
transaction = api.get_transaction_by_blocknumber_index(block_number='0x57b2cc', index='0x2')
10+
print(transaction['transactionIndex'])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
api = Proxies(api_key=key)
8+
transaction = api.get_transaction_by_hash(
9+
tx_hash='0x1e2910a262b1008d0616a0beb24c1a491d78771baa54a33e66065e03b1f46bc1')
10+
print(transaction['hash'])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
8+
api = Proxies(api_key=key)
9+
count = api.get_transaction_count('0x6E2446aCfcec11CC4a60f36aFA061a9ba81aF7e0')
10+
print(int(count, 16))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
api = Proxies(api_key=key)
8+
receipt = api.get_transaction_receipt('0xb03d4625fd433ad05f036abdc895a1837a7d838ed39f970db69e7d832e41205d')
9+
print(receipt)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from etherscan.proxies import Proxies
2+
import json
3+
4+
with open('../../api_key.json', mode='r') as key_file:
5+
key = json.loads(key_file.read())['key']
6+
7+
api = Proxies(api_key=key)
8+
uncles = api.get_uncle_by_blocknumber_index(block_number='0x210A9B', index='0x0')
9+
print(uncles['uncles'])

0 commit comments

Comments
 (0)