Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb2cf96

Browse files
Cleaned up code to comply with flake8 linter
- tox enforces linting on etherscan folder
1 parent b6ad1a0 commit bb2cf96

24 files changed

+96
-78
lines changed

‎etherscan/accounts.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44

55
class Account(Client):
6+
PAGE_NUM_PATTERN = re.compile(
7+
'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0')
8+
69
def __init__(self, address=Client.dao_address, api_key='YourApiKeyToken'):
710
Client.__init__(self, address=address, api_key=api_key)
811
self.url_dict[self.MODULE] = 'account'
@@ -21,9 +24,11 @@ def get_balance_multiple(self):
2124
req = self.connect()
2225
return req['result']
2326

24-
def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False) -> list:
27+
def get_transaction_page(self, page=1, offset=10000, sort='asc',
28+
internal=False) -> list:
2529
"""
26-
Get a page of transactions, each transaction returns list of dict with keys:
30+
Get a page of transactions, each transaction
31+
returns list of dict with keys:
2732
nonce
2833
hash
2934
cumulativeGasUsed
@@ -62,7 +67,8 @@ def get_transaction_page(self, page=1, offset=10000, sort='asc', internal=False)
6267
req = self.connect()
6368
return req['result']
6469

65-
def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list:
70+
def get_all_transactions(self, offset=10000, sort='asc',
71+
internal=False) -> list:
6672
if internal:
6773
self.url_dict[self.ACTION] = 'txlistinternal'
6874
else:
@@ -77,19 +83,23 @@ def get_all_transactions(self, offset=10000, sort='asc', internal=False) -> list
7783
self.build_url()
7884
req = self.connect()
7985
if "No transactions found" in req['message']:
80-
print("Total number of transactions: {}".format(len(trans_list)))
86+
print(
87+
"Total number of transactions: {}".format(len(trans_list)))
8188
self.page = ''
8289
return trans_list
8390
else:
8491
trans_list += req['result']
8592
# Find any character block that is a integer of any length
86-
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])
93+
page_number = re.findall(Account.PAGE_NUM_PATTERN,
94+
self.url_dict[self.PAGE])
8795
print("page {} added".format(page_number[0]))
8896
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
8997

90-
def get_blocks_mined_page(self, blocktype='blocks', page=1, offset=10000) -> list:
98+
def get_blocks_mined_page(self, blocktype='blocks', page=1,
99+
offset=10000) -> list:
91100
"""
92-
Get a page of blocks mined by given address, returns list of dict with keys:
101+
Get a page of blocks mined by given address,
102+
returns list of dict with keys:
93103
blockReward (in wei)
94104
blockNumber
95105
timeStamp
@@ -117,12 +127,15 @@ def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list:
117127
req = self.connect()
118128
print(req['message'])
119129
if "No transactions found" in req['message']:
120-
print("Total number of blocks mined: {}".format(len(blocks_list)))
130+
print(
131+
"Total number of blocks mined: {}".format(
132+
len(blocks_list)))
121133
return blocks_list
122134
else:
123135
blocks_list += req['result']
124136
# Find any character block that is a integer of any length
125-
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])
137+
page_number = re.findall(Account.PAGE_NUM_PATTERN,
138+
self.url_dict[self.PAGE])
126139
print("page {} added".format(page_number[0]))
127140
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
128141

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

136149
def update_transactions(self, address, trans):
137150
"""
138-
Gets last page of transactions (last 10k trans) and updates current trans book (book)
151+
Gets last page of transactions (last 10k trans)
152+
and updates current trans book (book)
139153
"""
140154
pass

‎etherscan/client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class BadRequest(ClientException):
3030
"""Invalid request passed"""
3131

3232

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

@@ -65,7 +66,6 @@ class Client(object):
6566
def __init__(self, address, api_key=''):
6667
self.http = requests.session()
6768
self.url_dict = collections.OrderedDict([
68-
6969
(self.MODULE, ''),
7070
(self.ADDRESS, ''),
7171
(self.OFFSET, ''),
@@ -86,12 +86,12 @@ def __init__(self, address, api_key=''):
8686
(self.TAG, ''),
8787
(self.BOOLEAN, ''),
8888
(self.INDEX, ''),
89-
(self.API_KEY, api_key)]
90-
)
89+
(self.API_KEY, api_key)])
90+
91+
# Var initialization should take place within init
92+
self.url = None
9193

92-
# self.url_dict[API_KEY] = str(api_key)
9394
self.check_and_get_api()
94-
# self.key = self.URL_BASES['key'] + self.API_KEY
9595

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

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

106108
def connect(self):
107109
# TODO: deal with "unknown exception" error
@@ -119,14 +121,16 @@ def connect(self):
119121
return data
120122
else:
121123
raise EmptyResponse(data.get('message', 'no message'))
122-
raise BadRequest("Problem with connection, status code: %s" % req.status_code)
124+
raise BadRequest(
125+
"Problem with connection, status code: %s" % req.status_code)
123126

124127
def check_and_get_api(self):
125128
if self.url_dict[self.API_KEY]: # Check if api_key is empty string
126129
pass
127130
else:
128-
self.url_dict[self.API_KEY] = input('Please type your EtherScan.io API key: ')
131+
self.url_dict[self.API_KEY] = input(
132+
'Please type your EtherScan.io API key: ')
129133

130-
defcheck_keys_api(self, data):
131-
returnall (kindataforkin ('jsonrpc', 'id', 'result'))
132-
134+
@staticmethod
135+
defcheck_keys_api(data):
136+
returnall(kindataforkin ('jsonrpc', 'id', 'result'))

‎etherscan/proxies.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,32 @@ def get_most_recent_block(self):
1212
self.build_url()
1313
req = self.connect()
1414
return req['result']
15-
15+
1616
def get_block_by_number(self, block_number: Union[str, int]):
1717
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)
18+
self.url_dict[self.TAG] = block_number if type(
19+
block_number) is str else hex(block_number)
1920
self.url_dict[self.BOOLEAN] = 'true'
2021
self.build_url()
2122
req = self.connect()
2223
return req['result']
2324

2425
def get_uncle_by_blocknumber_index(self,
25-
block_number: Union[str, int],
26-
index: Union[str, int]):
26+
block_number: Union[str, int],
27+
index: Union[str, int]):
2728
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.TAG] = block_number if type(
30+
block_number) is str else hex(block_number)
2931
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
3032
self.build_url()
3133
req = self.connect()
3234
return req['result']
3335

34-
def get_block_transaction_count_by_number(self, block_number: Union[str, int]):
36+
def get_block_transaction_count_by_number(self,
37+
block_number: Union[str, int]):
3538
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)
39+
self.url_dict[self.TAG] = block_number if type(
40+
block_number) is str else hex(block_number)
3741
self.build_url()
3842
req = self.connect()
3943
return req['result']
@@ -46,10 +50,11 @@ def get_transaction_by_hash(self, tx_hash: str):
4650
return req['result']
4751

4852
def get_transaction_by_blocknumber_index(self,
49-
block_number: Union[str, int],
50-
index: Union[str, int]):
53+
block_number: Union[str, int],
54+
index: Union[str, int]):
5155
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)
56+
self.url_dict[self.TAG] = block_number if type(
57+
block_number) is str else hex(block_number)
5358
self.url_dict[self.INDEX] = index if type(index) is str else hex(index)
5459
self.build_url()
5560
req = self.connect()
@@ -69,5 +74,3 @@ def get_transaction_receipt(self, tx_hash: str):
6974
self.build_url()
7075
req = self.connect()
7176
return req['result']
72-
73-

‎examples/accounts/Accounts Examples Notebook.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@
457457
"language_info": {
458458
"codemirror_mode": {
459459
"name": "ipython",
460-
"version": 3.0
460+
"version": 3
461461
},
462462
"file_extension": ".py",
463463
"mimetype": "text/x-python",
@@ -489,4 +489,4 @@
489489
},
490490
"nbformat": 4,
491491
"nbformat_minor": 0
492-
}
492+
}

‎examples/accounts/get_all_blocks_mined.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
api = Account(address=address, api_key=key)
1010
blocks = api.get_all_blocks_mined(offset=10000, blocktype='uncles')
11-
print(blocks)
11+
print(blocks)

‎examples/accounts/get_all_transactions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
# address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
87
address = '0x49edf201c1e139282643d5e7c6fb0c7219ad1db7'
98

109
api = Account(address=address, api_key=key)
11-
transactions = api.get_all_transactions(offset=10000, sort='asc', internal=True)
10+
transactions = api.get_all_transactions(offset=10000, sort='asc',
11+
internal=False)
1212

13-
print(transactions[0])
13+
print(transactions[0])

‎examples/accounts/get_balance.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
api = Account(address=address, api_key=key)
1010
balance = api.get_balance()
1111
print(balance)
12-

‎examples/accounts/get_balance_multi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
with open('../../api_key.json', mode='r') as key_file:
55
key = json.loads(key_file.read())['key']
66

7-
address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
7+
address = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
8+
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
89

910
api = Account(address=address, api_key=key)
1011
balances = api.get_balance_multiple()
11-
print(balances)
12+
print(balances)

‎examples/accounts/get_blocks_mined.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
api = Account(address=address, api_key=key)
1010
blocks = api.get_blocks_mined_page(page=1, offset=10000, blocktype='blocks')
11-
print(blocks)
11+
print(blocks)

‎examples/accounts/get_transaction_page.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
api = Account(address=address, api_key=key)
1010
transactions = api.get_transaction_page(page=1, offset=10000, sort='des')
11-
print(transactions)
11+
print(transactions)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /