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
2 changes: 1 addition & 1 deletion etherscan/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Account(Client):
PAGE_NUM_PATTERN = re.compile(
'[1-9](?:\d{0,2})(?:,\d{3})*(?:\.\d*[1-9])?|0?\.\d*[1-9]|0')
r'[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)
Expand Down
16 changes: 9 additions & 7 deletions etherscan/client.ropsten.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class BadRequest(ClientException):
"""Invalid request passed"""


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

# Constants
PREFIX = 'https://api-ropsten.etherscan.io/api?' # TESTNET
PREFIX = 'https://api-ropsten.etherscan.io/api?' # TESTNET
MODULE = 'module='
ACTION = '&action='
CONTRACT_ADDRESS = '&contractaddress='
Expand Down Expand Up @@ -101,7 +101,8 @@ 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(
[parm + val if val else '' for parm, val in self.url_dict.items()])

def connect(self):
# TODO: deal with "unknown exception" error
Expand All @@ -119,14 +120,15 @@ 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(
f"Problem with connection, status code: {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'))

return all(k in data for k in ('jsonrpc', 'id', 'result'))
2 changes: 1 addition & 1 deletion etherscan/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def get_sourcecode(self):
self.url_dict[self.ACTION] = 'getsourcecode'
self.build_url()
req = self.connect()
return req['result']
return req['result']
23 changes: 15 additions & 8 deletions tests/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

from etherscan.accounts import Account

SINGLE_BALANCE = '40807168566070000000000'
SINGLE_BALANCE = '40807178566070000000000'
SINGLE_ACCOUNT = '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a'
MULTI_ACCOUNT = ['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a']
MULTI_ACCOUNT = [
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
]
MULTI_BALANCE = [
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'balance': '40807168566070000000000'},
{'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'balance': '40807168566070000000000'}
{
'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'balance': '40807178566070000000000'
},
{
'account': '0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a',
'balance': '40807178566070000000000',
}
]
API_KEY = 'YourAPIkey'


class AccountsTestCase(unittest.TestCase):

def test_get_balance(self):
Expand All @@ -22,4 +29,4 @@ def test_get_balance(self):

def test_get_balance_multi(self):
api = Account(address=MULTI_ACCOUNT, api_key=API_KEY)
self.assertEqual(api.get_balance_multiple(), MULTI_BALANCE)
self.assertEqual(api.get_balance_multiple(), MULTI_BALANCE)