import math import time from rediscluster import RedisCluster import requests import json BNBCONTRACT = '0x0000000000000000000000000000000000000000' WITHDRAWFEEUSD = 0.5 #提现手续费,标的是usdt单位 WITHDRAWCOUNTBYFEE = 2 #提现所需对应手续费token数量的倍数 TOKENPRICE = 'TOKENPRICE' TOKENPRICEDICT = 'TOKENPRICEDICT' TOKENWITHDRAW = 'TOKENWITHDRAW' #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:6379' r = RedisCluster(host=REDIS_ONLINE, port=6379, decode_responses=True, skip_full_coverage_check=True) def getTokenList(): l = [] with open('/home/sh/token.txt') as f: for line in f: l.append(line.strip()) return l def getPrice(contract): headers = { 'accept': 'application/json', 'X-API-Key': 'e757C7HGUAiQSY37e3hxmJKCelBwWzt0caFAYfoXutJXVMISI3mX7w3CIO4ga5vo', } params = { 'chain': 'bsc', 'exchange': 'PancakeSwapv2', } response = requests.get(f'https://deep-index.moralis.io/api/v2/erc20/{contract}/price', headers=headers, params=params) result = response.json() print(result) usdPrice = result['usdPrice'] bnbPrice = int(result['nativePrice']['value'])/1000000000000000000 priceOfBnb = usdPrice/bnbPrice # print(contract, usdPrice, bnbPrice, priceOfBnb) return {'contract':contract, 'usdPrice':usdPrice}, priceOfBnb def getAllPrice(): l = [] bnbPrice = 0 tokenList = getTokenList() for i in tokenList: info, bnb = getPrice(i) l.append(info) bnbPrice = bnb l.append({'contract':BNBCONTRACT, 'usdPrice':bnbPrice}) r.set(TOKENPRICE, json.dumps({'tokenPrice':l})) return l def getWithdrawFees(): l = [] priceList = getAllPrice() for i in priceList: 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() print(r.get(TOKENPRICE)) print(r.get(TOKENWITHDRAW))