-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathaccounts.py
executable file
·172 lines (152 loc) · 5.94 KB
/
accounts.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from .client import Client
import re
class Account(Client):
PAGE_NUM_PATTERN = re.compile(
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)
self.url_dict[self.MODULE] = 'account'
def get_balance(self):
self.url_dict[self.ACTION] = 'balance'
self.url_dict[self.TAG] = 'latest'
self.build_url()
req = self.connect()
return req['result']
def get_balance_multiple(self):
self.url_dict[self.ACTION] = 'balancemulti'
self.url_dict[self.TAG] = 'latest'
self.build_url()
req = self.connect()
return req['result']
def get_transaction_page(self, page=1, offset=10000, sort='asc',
internal=False, erc20=False, erc721=False, erc1155=False) -> list:
"""
Get a page of transactions, each transaction
returns list of dict with keys:
nonce
hash
cumulativeGasUsed
gasUsed
timeStamp
blockHash
value (in wei)
input
gas
isInternalTx
contractAddress
confirmations
gasPrice
transactionIncex
to
from
isError
blockNumber
sort options:
'asc' -> ascending order
'desc' -> descending order
internal options: (currently marked at Beta for etherscan.io)
True -> Gets the internal transactions of the address
False -> (default) get normal external transactions
erc20 options: (currently marked at Beta for etherscan.io)
True -> Gets the erc20 token transcations of the address
False -> (default) get normal external transactions
NOTE: not sure if this works for contract addresses, requires testing
"""
if internal:
self.url_dict[self.ACTION] = 'txlistinternal' # interact with contract
elif erc20:
self.url_dict[self.ACTION] = 'tokentx' # erc20 token trans
elif erc721:
self.url_dict[self.ACTION] = 'tokennfttx' # nfts
elif erc1155:
self.url_dict[self.ACTION] = 'token1155tx' # nfts
else:
# return 'no such category'
self.url_dict[self.ACTION] = 'txlist'
self.url_dict[self.PAGE] = str(page)
self.url_dict[self.OFFSET] = str(offset)
self.url_dict[self.SORT] = sort
self.build_url()
req = self.connect()
return req['result']
def get_all_transactions(self, offset=10000, sort='asc',
internal=False) -> list:
if internal:
self.url_dict[self.ACTION] = 'txlistinternal'
else:
self.url_dict[self.ACTION] = 'txlist'
self.url_dict[self.PAGE] = str(1)
self.url_dict[self.OFFSET] = str(offset)
self.url_dict[self.SORT] = sort
self.build_url()
trans_list = []
while True:
self.build_url()
req = self.connect()
if "No transactions found" in req['message']:
print(
"Total number of transactions: {}".format(len(trans_list)))
self.page = ''
return trans_list
else:
trans_list += req['result']
# Find any character block that is a integer of any length
page_number = re.findall(Account.PAGE_NUM_PATTERN,
self.url_dict[self.PAGE])
print("page {} added".format(page_number[0]))
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
def get_blocks_mined_page(self, blocktype='blocks', page=1,
offset=10000) -> list:
"""
Get a page of blocks mined by given address,
returns list of dict with keys:
blockReward (in wei)
blockNumber
timeStamp
blocktype options:
'blocks' -> full blocks only
'uncles' -> uncles only
"""
self.url_dict[self.ACTION] = 'getminedblocks'
self.url_dict[self.BLOCK_TYPE] = blocktype
self.url_dict[self.PAGE] = str(page)
self.url_dict[self.OFFSET] = str(offset)
self.build_url()
req = self.connect()
return req['result']
def get_all_blocks_mined(self, blocktype='blocks', offset=10000) -> list:
self.url_dict[self.ACTION] = 'getminedblocks'
self.url_dict[self.BLOCK_TYPE] = blocktype
self.url_dict[self.PAGE] = str(1)
self.url_dict[self.OFFSET] = str(offset)
blocks_list = []
while True:
self.build_url()
req = self.connect()
print(req['message'])
if "No transactions found" in req['message']:
print(
"Total number of blocks mined: {}".format(
len(blocks_list)))
return blocks_list
else:
blocks_list += req['result']
# Find any character block that is a integer of any length
page_number = re.findall(Account.PAGE_NUM_PATTERN,
self.url_dict[self.PAGE])
print("page {} added".format(page_number[0]))
self.url_dict[self.PAGE] = str(int(page_number[0]) + 1)
def get_internal_by_hash(self, tx_hash:str):
"""
"""
self.url_dict[self.ACTION] = 'txlistinternal'
self.url_dict[self.TXHASH] = tx_hash
self.build_url()
req = self.connect()
return req['result']
def update_transactions(self, address, trans):
"""
Gets last page of transactions (last 10k trans)
and updates current trans book (book)
"""
pass