-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEtherscanAPI.py
106 lines (95 loc) · 4.64 KB
/
EtherscanAPI.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
# Powered by Etherscan.io APIs
import requests, os
import pandas as pd
import numpy as np
def save_transaction_data(data_list:list, save_path:str):
store_list = []
for item_dict in data_list:
store_list.append([item_dict["blockNumber"], item_dict["timeStamp"], item_dict["hash"], item_dict["from"], item_dict["to"], item_dict["value"], item_dict["gas"], item_dict["gasPrice"], item_dict["contractAddress"], item_dict["gasUsed"], item_dict["isError"]])
df_titles = ["blockNumber", "timeStamp", "hash", "from", "to", "value", "gas", "gasPrice", "contractAddress", "gasUsed", "isError"]
store_df = pd.DataFrame(np.array(store_list), index=None, columns=df_titles)
store_df.to_csv(save_path)
return
class Contract:
def __init__(self, address:str):
self.api_key = 'IGK5V2U9EW3ZJRCZHQUVGW3DEPXNVCB73G'
self.address = address
def r_process(self, r:str):
r_dict = eval(r)
if r_dict["status"] == '1':
return r_dict["result"]
else:
return r_dict["message"]
def account(self, type:str, startblock:str='10001', endblock:str='99999999'):
'''
param: type
balance: Get Ether Balance for a single Address
txlist: Get a list of 'Normal' Transactions By Address
txlistinternal: Get a list of 'Internal' Transactions by Address
tokentx: Get a list of "ERC20 - Token Transfer Events" by Address
tokennfttx: Get a list of "ERC721 - Token Transfer Events" by Address
getminedblocks: Get list of Blocks Mined by Address
'''
api_link = f"https://api.etherscan.io/api?module=account&action={ type }&address={ self.address }&tag=latest"
if type == "balance":
api_link = api_link + f"&apikey={ self.api_key }"
r = requests.get(api_link).text
return self.r_process(r)
elif type == "getminedblocks":
api_link = api_link + f"&blocktype=blocks&apikey={ self.api_key }"
r = requests.get(api_link).text
return self.r_process(r)
else:
api_link = api_link + f"&startblock={ startblock }&endblock={ endblock }&sort=asc" + f"&apikey={ self.api_key }"
# print(api_link)
r = requests.get(api_link, timeout=200).text
return self.r_process(r)
return
def contract(self):
return
if __name__ == '__main__':
# df = pd.read_csv("./etherscan_top_account.csv")
# address_list = df["Address"].values[7500:10000] #20200712 18:00 from 0 to 2500
# =====================================================================================
index_path = "./transaction/all_cate_top10_transaction_simplified_1.csv"
save_path = "./transaction/all_cate_top10_user_transaction/"
df = pd.read_csv(index_path)
address_list = df["from"].values
failed_count = 0
index = 1
total = len(address_list)
for item in address_list:
try:
request_entity = Contract(item)
a = request_entity.account("txlist")
save_transaction_data(a, save_path + request_entity.address + ".csv")
# save_transaction_data(a, f"./transaction/game/GodsUnchained_raw/{ request_entity.address }.csv")
# save_transaction_data(a, f"./transaction/contract_tx_csv/GodsUnchained2.csv")
print(f"processed: { index }/{ total }")
index += 1
except:
print(f"failed: { failed_count } at {request_entity.address}")
failed_count += 1
index += 1
continue
# =====================================================================================
# address_list= ["0x06012c8cf97BEaD5deAe237070F9587f8E7A266d"]
# index = 1
# total = len(address_list)
# for item in address_list:
# # try:
# request_entity = Contract(item)
# a = request_entity.account("txlist")
# save_transaction_data(a, f"./{ request_entity.address }.csv")
# # save_transaction_data(a, f"./contract_db/contract_transaction/{ request_entity.address }.csv")
# # save_transaction_data(a, f"./transaction/game/GodsUnchained_raw/{ request_entity.address }.csv")
# # save_transaction_data(a, f"./transaction/contract_tx_csv/GodsUnchained2.csv")
# # except:
# # print("failed")
# # index += 1
# # continue
# # print(f"processed { index }/{ total }")
# index += 1
# request_entity = Contract("0xC7af99Fe5513eB6710e6D5f44F9989dA40F27F26")
# a = request_entity.account("txlist")
# save_transaction_data(a, f"./transaction/{ request_entity.address }.csv")