bsc_token_price.py 2.5 KB

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