bsc_token_price.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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(fp):
  25. l = []
  26. with open(fp) 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. print(contract,response)
  43. result = response.json()
  44. print(result)
  45. try:
  46. usdPrice = result['usdPrice']
  47. bnbPrice = int(result['nativePrice']['value'])/1000000000000000000
  48. except:
  49. if contract == '0xfc9F81B107F51F2383fCE56650fEDB59C5fd59bD':
  50. usdPrice = 0.24
  51. bnbPrice = 0.00074
  52. try:
  53. priceOfBnb = usdPrice/bnbPrice
  54. except:
  55. priceOfBnb = 0
  56. # print(contract, usdPrice, bnbPrice, priceOfBnb)
  57. return {'contract':contract, 'usdPrice':usdPrice}, priceOfBnb
  58. def getAllPrice(chain, fp):
  59. l = []
  60. bnbPrice = 0
  61. tokenList = getTokenList(fp)
  62. for i in tokenList:
  63. time.sleep(1)
  64. info, bnb = getPrice(i)
  65. l.append(info)
  66. if bnb != 0:
  67. bnbPrice = bnb
  68. l.append({'contract':BNBCONTRACT, 'usdPrice':bnbPrice})
  69. l.append({'contract':DEPROCONTRACT, 'usdPrice':DEPROPRICE})
  70. r.set(TOKENPRICE+chain, json.dumps({'tokenPrice':l}))
  71. return l
  72. def getWithdrawFees(chain, fp):
  73. l = []
  74. priceList = getAllPrice(chain, fp)
  75. for i in priceList:
  76. print(i)
  77. contract = i['contract']
  78. withdrawFeeCount = round(WITHDRAWFEEUSD/i['usdPrice'], 18)
  79. withdrawMinCount = withdrawFeeCount * WITHDRAWCOUNTBYFEE
  80. l.append({'contract':contract, 'withdrawFeeCount':withdrawFeeCount,'withdrawMinCount':withdrawMinCount})
  81. r.set(TOKENWITHDRAW, json.dumps({'tokenWithdraw':l}))
  82. return l
  83. getWithdrawFees("BSC", '/home/sh/token.txt')
  84. print(r.get(TOKENPRICE))
  85. print(r.get(TOKENWITHDRAW))
  86. print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))