import math import time from rediscluster import RedisCluster import requests import json import datetime print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) BNBCONTRACT = '0x0000000000000000000000000000000000000000' WITHDRAWFEEUSD = 0.5 #提现手续费,标的是usdt单位 WITHDRAWCOUNTBYFEE = 2 #提现所需对应手续费token数量的倍数 TOKENPRICE = 'TOKENPRICE_' TOKENPRICEDICT = 'TOKENPRICEDICT' TOKENWITHDRAW = 'TOKENWITHDRAW' DEPROCONTRACT = '0x9984086CB9d93dbe47C4e70890aAD5454bBc2518' DEPROPRICE = 0.000000001 #dev ali #r = RedisCluster(host="r-bp1ps6my7lzg8rdhwxpi.redis.rds.aliyuncs.com", port=6379, password='Wqsd@2019', decode_responses=True, skip_full_coverage_check=True) #test REDIS_TEST = 'denet-test.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com' #r = RedisCluster(host=REDIS_TEST, port=6379, decode_responses=True, skip_full_coverage_check=True) #online REDIS_ONLINE = 'denet-chain-prod.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com' r = RedisCluster(host=REDIS_ONLINE, port=6379, decode_responses=True, skip_full_coverage_check=True) def getTokenList(fp): l = [] with open(fp) as f: #with open('token.txt') as f: for line in f: l.append(line.strip()) return l def getPrice(contract): headers = { 'accept': 'application/json', 'X-API-Key': 'zS1xPHzJquxUZgXKPDSju9NNtTgxIXpKQ95n9J8AyY24YcIOVpGfup0lMIX7hAiT', } params = { 'chain': 'bsc', 'exchange': 'PancakeSwapv2', } response = requests.get(f'https://deep-index.moralis.io/api/v2/erc20/{contract}/price', headers=headers, params=params) print(contract,response) result = response.json() print(result) try: usdPrice = result['usdPrice'] bnbPrice = int(result['nativePrice']['value'])/1000000000000000000 except: if contract == '0xfc9F81B107F51F2383fCE56650fEDB59C5fd59bD': usdPrice = 0.24 bnbPrice = 0.00074 try: priceOfBnb = usdPrice/bnbPrice except: priceOfBnb = 0 # print(contract, usdPrice, bnbPrice, priceOfBnb) return {'contract':contract, 'usdPrice':usdPrice}, priceOfBnb def getAllPrice(chain, fp): l = [] bnbPrice = 0 tokenList = getTokenList(fp) for i in tokenList: time.sleep(1) info, bnb = getPrice(i) l.append(info) if bnb != 0: bnbPrice = bnb l.append({'contract':BNBCONTRACT, 'usdPrice':bnbPrice}) l.append({'contract':DEPROCONTRACT, 'usdPrice':DEPROPRICE}) r.set(TOKENPRICE+chain, json.dumps({'tokenPrice':l})) return l def getWithdrawFees(chain, fp): l = [] priceList = getAllPrice(chain, fp) for i in priceList: print(i) contract = i['contract'] withdrawFeeCount = round(WITHDRAWFEEUSD/i['usdPrice'], 18) withdrawMinCount = withdrawFeeCount * WITHDRAWCOUNTBYFEE l.append({'contract':contract, 'withdrawFeeCount':withdrawFeeCount,'withdrawMinCount':withdrawMinCount}) r.set(TOKENWITHDRAW, json.dumps({'tokenWithdraw':l})) return l getWithdrawFees("BSC", '/home/sh/token.txt') print(r.get(TOKENPRICE)) print(r.get(TOKENWITHDRAW)) print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))