bsc_token_price.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. DEPROCONTRACT = '0x9984086CB9d93dbe47C4e70890aAD5454bBc2518'
  15. DEPROPRICE = 0.000000001
  16. #dev ali
  17. #r = RedisCluster(host="r-bp1ps6my7lzg8rdhwxpi.redis.rds.aliyuncs.com", port=6379, password='Wqsd@2019', decode_responses=True, skip_full_coverage_check=True)
  18. #test
  19. REDIS_TEST = 'denet-test.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com'
  20. #r = RedisCluster(host=REDIS_TEST, port=6379, decode_responses=True, skip_full_coverage_check=True)
  21. #online
  22. REDIS_ONLINE = 'denet-chain-prod.y2slbl.clustercfg.memorydb.us-east-1.amazonaws.com'
  23. r = RedisCluster(host=REDIS_ONLINE, port=6379, decode_responses=True, skip_full_coverage_check=True)
  24. def getTokenList():
  25. l = []
  26. with open('/home/sh/token.txt') as f:
  27. #with open('token.txt') as f:
  28. for line in f:
  29. l.append(line.strip())
  30. return l
  31. def getPrice(contract):
  32. headers = {
  33. 'accept': 'application/json',
  34. 'X-API-Key': 'zS1xPHzJquxUZgXKPDSju9NNtTgxIXpKQ95n9J8AyY24YcIOVpGfup0lMIX7hAiT',
  35. }
  36. params = {
  37. 'chain': 'bsc',
  38. 'exchange': 'PancakeSwapv2',
  39. }
  40. response = requests.get(f'https://deep-index.moralis.io/api/v2/erc20/{contract}/price', headers=headers,
  41. params=params)
  42. result = response.json()
  43. usdPrice = result['usdPrice']
  44. bnbPrice = int(result['nativePrice']['value'])/1000000000000000000
  45. try:
  46. priceOfBnb = usdPrice/bnbPrice
  47. except:
  48. priceOfBnb = 0
  49. # print(contract, usdPrice, bnbPrice, priceOfBnb)
  50. return {'contract':contract, 'usdPrice':usdPrice}, priceOfBnb
  51. def getAllPrice():
  52. l = []
  53. bnbPrice = 0
  54. tokenList = getTokenList()
  55. for i in tokenList:
  56. info, bnb = getPrice(i)
  57. l.append(info)
  58. if bnb != 0:
  59. bnbPrice = bnb
  60. l.append({'contract':BNBCONTRACT, 'usdPrice':bnbPrice})
  61. l.append({'contract':DEPROCONTRACT, 'usdPrice':DEPROPRICE})
  62. r.set(TOKENPRICE, json.dumps({'tokenPrice':l}))
  63. return l
  64. def getWithdrawFees():
  65. l = []
  66. priceList = getAllPrice()
  67. for i in priceList:
  68. print(i)
  69. contract = i['contract']
  70. withdrawFeeCount = round(WITHDRAWFEEUSD/i['usdPrice'], 18)
  71. withdrawMinCount = withdrawFeeCount * WITHDRAWCOUNTBYFEE
  72. l.append({'contract':contract, 'withdrawFeeCount':withdrawFeeCount,'withdrawMinCount':withdrawMinCount})
  73. r.set(TOKENWITHDRAW, json.dumps({'tokenWithdraw':l}))
  74. return l
  75. getWithdrawFees()
  76. print(r.get(TOKENPRICE))
  77. print(r.get(TOKENWITHDRAW))
  78. print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))