""" 计算各币种持币地址数 占比 """ import time
from ethereum_address.driver import DbEthereumConn, DbMytokenConn, logging, get_etherscan_target from config import env
查询占比数据
def get_percentage_data(db, per_num):
sql = " SELECT currency_id , SUM(percentage),SUM(quantity) FROM currency_ethereum_address "
"WHERE holder_rank <= {} GROUP BY currency_id".format(per_num)
return db.eth_engine.execute(sql)
获取当前币种的更新时间
def get_time_data(db):
sql = "SELECT currency_id ,any_value(updated_at) FROM currency_ethereum_address GROUP BY currency_id "
return db.eth_engine.execute(sql)
查询当前币种的持币地址数
def get_address_nums_by_db(dbmt, cid):
sql = "SELECT cp.currency_id , field_value FROM currency_project as cp "
"join currency_project_meta as cpm on cp.id = cpm.currency_project_id WHERE "
"cp.currency_id = {} AND field_key = 'holders'".format(cid)
return dbmt.mt_engine.execute(sql)
转化成map
def to_dict_simple(per): return {k: v for k, v in per}
def to_dict(per): return {v[0]: str(v[1]) + "_" + str(v[2]) for v in per if v}
获取非erc20链上的 币种ID 和 链接的对应关系
def get_no_erc20_clink(db_eth):
sql = "SELECT currency_id ,holder_site FROM subchain_holder_site_address WHERE enable = 1"
return db_eth.eth_engine.execute(sql)
获取当前币种的浏览器的跳转链接 和币种id的映射关系
def get_currency_link(db_eth): #读取非ERC20的币种 采用的事字典类型 CLMap = to_dict_simple(get_no_erc20_clink(db_eth)) # print(CLMap) #get_etherscan_target 获取要抓取的币种 for target_list in get_etherscan_target(): for target in target_list: if target['explorer'] and target['blockchain'] == 'Ethereum' and len( str(target['contract_address'])) > 40: CLMap[target['currency_id']] = target['explorer']
print(CLMap)
# return CLMap
获取当前币种的持币地址数 和币种id的映射关系
def get_address_num(dbmt, clmap): CNMap = {} for k in clmap.keys(): cnnnmap = to_dict_simple(get_address_nums_by_db(dbmt, k)) # print(cnnnmap) if k in cnnnmap: CNMap[k] = cnnnmap[k] return CNMap
field = ( 'currency_id', 'qunatity_10', 'percent_10', 'qunatity_30', 'percent_30', 'qunatity_50', 'percent_50', 'qunatity_100', 'percent_100', 'address_num', 'link', 'create_at', 'update_at',)
def update_percentage_data(db, data):
try:
sql = "insert into currency_ethereum_address_analysis "
"({}) values ({})".format(','.join(field), ','.join('%s' % id for id in data))
db.eth_engine.execute(sql)
logging.debug(data[0])
except Exception as m:
logging.error(str(m))
def calculate_and_update_percentage(db, per_10, per_30, per_50, per_100, time_list, clmap, cnmap): update_at = int(time.time() // 86400 * 86400 + 86400) print(update_at) print(per_10.keys()) print(clmap.keys())
for key in per_10.keys():
if key in clmap.keys():
link = "'" + clmap[key] + "'"
per10, qua10 = per_10[key].split("_")
per30, qua30 = per_30[key].split("_")
per50, qua50 = per_50[key].split("_")
per100, qua100 = per_100[key].split("_")
if float(per10) >= 100:
per10 = 99.99
if float(per10) < 0:
per10 = 0
if float(per30) >= 100:
per30 = 99.99
if float(per30) < 0:
per30 = 0
if float(per50) >= 100:
per50 = 99.99
if float(per50) < 0:
per50 = 0
if float(per100) >= 100:
per100 = 99.99
if float(per100) < 0:
per100 = 0
analysis_datas = [key, qua10, per10, qua30, per30, qua50, per50,
qua100, per100, int(cnmap[key]), link, time_list[key], update_at]
logging.info(analysis_datas)
update_percentage_data(db, analysis_datas)
else:
print('不会更新')
def eth_data_by_time(timestamp):
sql = 'SELECT percent_10 ,percent_30 ,percent_50 ,percent_100,currency_id FROM currency_ethereum_address_analysis WHERE update_at = %s'
db = DbEthereumConn()
try:
return db.eth_engine.execute(sql, int(timestamp) - 86400)
except Exception as e:
print(e)
def update_eth_data(timestamp): datas = eth_data_by_time(timestamp) sql = 'UPDATE currency_ethereum_address_analysis SET percent_10 = %s,percent_30 = %s,percent_50 = %s,percent_100 = %s WHERE currency_id = %s and update_at = ' + str( timestamp) print(sql) db = DbEthereumConn() for d in datas: db.eth_engine.execute(sql, d)
入口函数
def main(): logging.info('Env:{}'.format(env)) db = DbEthereumConn() dbmt = DbMytokenConn() # 初始化 币种ID和 链接的映射关系 clmap = get_currency_link(db) #字典的列表sss print(clmap) # 初始化 币种ID和 持币地址数的映射关系 cnmap = get_address_num(dbmt, clmap) #{345612: '36135'} print(cnmap) # # 初始化 币种ID 和 获取时间的映射关系 time_list = to_dict_simple(get_time_data(db)) #{10694: 1625169871} 币id:时间 print(time_list) # # 计算前10 占比 map per_10 = to_dict(get_percentage_data(db, 10)) # # 计算前30 占比 map 币id:持币占比_持币数量 per_30 = to_dict(get_percentage_data(db, 30)) #{10694: '94.28619779646397_141429004.0'} # # 计算前50 占比 map per_50 = to_dict(get_percentage_data(db, 50)) # # 计算前100 占比 map per_100 = to_dict(get_percentage_data(db, 100)) # print('start.....') print(per_10) calculate_and_update_percentage(db, per_10, per_30, per_50, per_100, time_list, clmap, cnmap)
if name == 'main': # update_eth_data(1577232000) main()