bsc_token_price.py 2.8 KB

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