-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
60 lines (52 loc) · 2.3 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests, json, re
from requests.exceptions import RequestException
class ApiException(Exception):
pass
class NoResultsException(ApiException):
pass
class EtherScanAPI:
def __init__(self, testnet=False):
self.API_URL = 'https://api.etherscan.io/api' if not testnet \
else 'https://ropsten.etherscan.io/api'
self.BASE_URL = 'https://etherscan.io' if not testnet \
else 'https://ropsten.etherscan.io'
def get_address_first_transaction(self, address):
try:
r = requests.get(self.API_URL, params={'module': 'account', 'action': 'txlist',
'address': address,
'startblock': '0', 'page': '1',
'offset': 1, 'sort': 'asc'})
result = json.loads(r.text)
if result['status'] != '1' or len(result['result']) < 1:
raise NoResultsException()
return result['result'][0]
except RequestException:
raise ApiException()
def get_address_out_transaction(self, address):
try:
r = requests.get(self.API_URL, params={'module': 'account', 'action': 'txlist',
'address': address,
'startblock': '0', 'page': '1',
'offset': 20, 'sort': 'asc'})
result = json.loads(r.text)
if result['status'] != '1' or len(result['result']) < 1:
raise NoResultsException()
for tx in result['result']:
if tx['from'] == address:
return tx
raise NoResultsException()
except RequestException:
raise ApiException()
def get_raw_transaction(self, txhash):
try:
r = requests.get(self.BASE_URL + '/getRawTx', params={'tx': txhash})
html = r.text
if 'Returned Raw Transaction Hex : ' not in html:
raise ApiException()
r = re.compile('Returned Raw Transaction Hex : [^0]+([x0-9a-fA-F]+)')
found = r.findall(html)
if len(found) == 0:
raise ApiException()
return found[0]
except RequestException:
raise ApiException()