import csv
import json
import os
import time
import pandas as pd
import requests
import threading
import time
# https://etherscan.io/ 需注册申请
api="T5ZYIGN55M2IIAX8E8UF36U2YGWM1XVP4S"
# eth 余额
# https://api.etherscan.io/api
# ?module=account
# &action=balance
# &address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae
# &tag=latest
# &apikey=YourApiKeyToken
# float(response.json()['data']['balance']) / 10e7
# 查询多个地址 以,隔开 最多20个
# {"status":"1","message":"OK","result":"341270809619405156413191"}
# erc20-token 余额
#
# https://api.etherscan.io/api
# ?module=account
# &action=tokenbalance
# &contractaddress=0xc3761eb917cd790b30dad99f6cc5b4ff93c4f9ea
# &address=0xe04f27eb70e025b78871a2ad7eabe85e61212761
# &tag=latest&apikey=YourApiKeyToken
# {"status":"1","message":"OK","result":"135499"}
# 第一次交易记录
# https://api.etherscan.io/api
# ?module=account
# &action=txlist
# &address=0xc5102fE9359FD9a28f877a67E36B0F050d81a3CC
# &startblock=0
# &endblock=99999999
# &page=1
# &offset=10
# &sort=asc # 升序
# &apikey=YourApiKeyToken
# {"status":"1","message":"OK","result":"result"}
# result:[{"blockNumber":"14923692","timeStamp":"1654646570","hash":"0xaa45b4858ba44230a5fce5a29570a5dec2bf1f0ba95bacdec4fe8f2c4fa99338","nonce":"7","blockHash":"0x8df71a12a8c06b36c06c26bf6248857dd2a2b75b6edbb4e33e9477078897b282","transactionIndex":"27","from":"0x9aa99c23f67c81701c772b106b4f83f6e858dd2e","to":"0xc5102fe9359fd9a28f877a67e36b0f050d81a3cc","value":"0","gas":"6000000","gasPrice":"125521409858","isError":"0","txreceipt_status":"1","input":"0xa9059cbb000000000000000000000000313143c4088a47c469d06fe3fa5fd4196be6a4d600000000000000000000000000000000000000000003b8e97d229a2d54800000","contractAddress":"","cumulativeGasUsed":"1977481","gasUsed":"57168","confirmations":"484697","methodId":"0xa9059cbb","functionName":"transfer(address _to, uint256 _value)"}]
class CSVRW:
def __init__(self):
self.file_list = []
self.write = None
def csv_file(self, dir_path):
for root, dirs, files in os.walk(dir_path):
for file in files:
if ".csv" in file:
self.file_list.append(file)
return self.file_list
# usecols=['ip', 'hostname', 'cpus', 'mems', 'disk_nums']
@staticmethod
def csv_read(file,usecols,encoding="utf-8"):
read_ = pd.read_csv(file, chunksize=10000, usecols=usecols,encoding=encoding)
return [{usecols[0]: line[0]} for dg in read_ for
_, line in dg.iterrows()]
@staticmethod
def csv_write(data):
store_file = os.path.dirname(__file__) + '/statistical.csv'
file = open(store_file, 'a+', newline='')
fieldnames = ['Addr', 'Eth','ERC20-Token','transaction']
writer = csv.DictWriter(f=file, fieldnames=fieldnames)
# writer.writeheader()
writer.writerow(data)
class ReqEth:
def __int__(self):
pass
@staticmethod
def RequestEth(addr):
ethuri = "https://api.etherscan.io/api?module=account&action=balance&address=%s&tag=latest&apikey=%s" %api
res=requests.get(ethuri %addr)
j=json.loads(res.text)
if j.get("status") =="1":
return '%.6f' %(float(int(j.get("result"))) / 10e17)
else:
return 0.0
@staticmethod
def RequestErc(addr):
ercuri = "https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xdac17f958d2ee523a2206206994597c13d831ec7&address=%s&tag=latest&apikey=%s" %api
res = requests.get(ercuri % addr)
j = json.loads(res.text)
if j.get("status") == "1":
print(float(int(j.get("result"))) / 10e15)
return '%.6f' % (float(int(j.get("result"))) / 10e15)
else:
return 0.0
@staticmethod
def RequestTra(addr):
tra="https://api.etherscan.io/api?module=account&action=txlist&address=%s&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=%s" %api
res = requests.get(tra % addr)
j = json.loads(res.text)
if j.get("status") == "1":
if len(j.get("result"))>1:
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(j.get("result")[1].get("timeStamp"))))
elif len(j.get("result")) == 1:
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(int(j.get("result")[0].get("timeStamp"))))
else:
pass
else:
return ""
if __name__ == '__main__':
csv_content = CSVRW.csv_read('addr.csv',['addr'],)
# addr = "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae"
r = ReqEth()
for content in csv_content:
addr = content.get("addr")
eth = r.RequestEth(addr)
erc = r.RequestErc(addr)
tra=r.RequestTra(addr)
data={'Addr':addr,'Eth':eth,'ERC20-Token':erc,'transaction':tra}
CSVRW.csv_write(data)
获取地址eth,usdt 余额及最早一次交易记录
最新推荐文章于 2024-08-19 15:35:55 发布