bsc_token_price.py 3.0 KB

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