bsc_token_price.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import math
  2. import time
  3. from rediscluster import RedisCluster
  4. import requests
  5. import json
  6. import datetime
  7. print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
  8. BNBCONTRACT = '0x0000000000000000000000000000000000000000'
  9. WITHDRAWFEEUSD = 0.5 #提现手续费,标的是usdt单位
  10. WITHDRAWCOUNTBYFEE = 2 #提现所需对应手续费token数量的倍数
  11. TOKENPRICE = 'TOKENPRICE'
  12. TOKENPRICEDICT = 'TOKENPRICEDICT'
  13. TOKENWITHDRAW = 'TOKENWITHDRAW'
  14. #dev ali
  15. #r = RedisCluster(host="r-bp1ps6my7lzg8rdhwxpi.redis.rds.aliyuncs.com", port=6379, password='Wqsd@2019', decode_responses=True, skip_full_coverage_check=True)
  16. #test
  17. REDIS_TEST = 'denet-test.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com'
  18. #r = RedisCluster(host=REDIS_TEST, port=6379, decode_responses=True, skip_full_coverage_check=True)
  19. #online
  20. REDIS_ONLINE = 'denet-chain-prod.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com'
  21. r = RedisCluster(host=REDIS_ONLINE, port=6379, decode_responses=True, skip_full_coverage_check=True)
  22. def getTokenList():
  23. l = []
  24. with open('/home/sh/token.txt') as f:
  25. for line in f:
  26. l.append(line.strip())
  27. return l
  28. def getPrice(contract):
  29. headers = {
  30. 'accept': 'application/json',
  31. 'X-API-Key': 'zS1xPHzJquxUZgXKPDSju9NNtTgxIXpKQ95n9J8AyY24YcIOVpGfup0lMIX7hAiT',
  32. }
  33. params = {
  34. 'chain': 'bsc',
  35. 'exchange': 'PancakeSwapv2',
  36. }
  37. response = requests.get(f'https://deep-index.moralis.io/api/v2/erc20/{contract}/price', headers=headers,
  38. params=params)
  39. result = response.json()
  40. usdPrice = result['usdPrice']
  41. bnbPrice = int(result['nativePrice']['value'])/1000000000000000000
  42. priceOfBnb = usdPrice/bnbPrice
  43. # print(contract, usdPrice, bnbPrice, priceOfBnb)
  44. return {'contract':contract, 'usdPrice':usdPrice}, priceOfBnb
  45. def getAllPrice():
  46. l = []
  47. bnbPrice = 0
  48. tokenList = getTokenList()
  49. for i in tokenList:
  50. info, bnb = getPrice(i)
  51. l.append(info)
  52. bnbPrice = bnb
  53. l.append({'contract':BNBCONTRACT, 'usdPrice':bnbPrice})
  54. r.set(TOKENPRICE, json.dumps({'tokenPrice':l}))
  55. return l
  56. def getWithdrawFees():
  57. l = []
  58. priceList = getAllPrice()
  59. for i in priceList:
  60. contract = i['contract']
  61. withdrawFeeCount = round(WITHDRAWFEEUSD/i['usdPrice'], 18)
  62. withdrawMinCount = withdrawFeeCount * WITHDRAWCOUNTBYFEE
  63. l.append({'contract':contract, 'withdrawFeeCount':withdrawFeeCount,'withdrawMinCount':withdrawMinCount})
  64. r.set(TOKENWITHDRAW, json.dumps({'tokenWithdraw':l}))
  65. return l
  66. getWithdrawFees()
  67. print(r.get(TOKENPRICE))
  68. print(r.get(TOKENWITHDRAW))
  69. print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))