sqlTools.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import time
  2. import os
  3. import requests
  4. import json
  5. import hashlib
  6. import logging
  7. from osTools import *
  8. # 设置日志格式
  9. logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
  10. DIR = os.path.dirname(os.path.abspath(__file__))
  11. IS_DEV = os.path.isfile(os.path.join(DIR, '.debug'))
  12. # 定义base url
  13. BASE_URL = "http://www.410eth.com:8088"
  14. if IS_DEV:
  15. BASE_URL = "http://localhost:8088/ethmev"
  16. class Auth:
  17. @staticmethod
  18. def build_auth(json_data):
  19. timestamp = int(time.time())
  20. ready_str = '410410' + str(timestamp)
  21. replaced_str = ready_str.replace(ready_str[-1:], '')
  22. json_data['timestamp'] = timestamp
  23. json_data['replace_str'] = replaced_str
  24. json_data['auth'] = hashlib.md5(replaced_str.encode('utf-8')).hexdigest()
  25. return json_data
  26. class EthMevModel:
  27. @staticmethod
  28. def append_or_update(block_number, hash_code, data):
  29. url = BASE_URL + '/ethmev/appendOrUpdate'
  30. json_data = {
  31. 'block': block_number,
  32. 'hash': hash_code,
  33. 'data': json.dumps(data),
  34. }
  35. json_data = Auth.build_auth(json_data)
  36. response = requests.post(url, data=json_data)
  37. return response.json()
  38. @staticmethod
  39. def find_by_block(block_number):
  40. url = BASE_URL + '/ethmev/findByBlock'
  41. json_data = {
  42. 'block': str(block_number)
  43. }
  44. json_data = Auth.build_auth(json_data)
  45. response = requests.post(url, data=json_data).json()
  46. return response
  47. @staticmethod
  48. def find_by_hash(hash_code):
  49. url = BASE_URL + '/ethmev/findByHash'
  50. json_data = {
  51. 'hash': hash_code
  52. }
  53. json_data = Auth.build_auth(json_data)
  54. response = requests.post(url, data=json_data)
  55. return response.json()
  56. @staticmethod
  57. def find_by_data_vague(data_vague):
  58. url = BASE_URL + '/ethmev/findByDataVague'
  59. json_data = {
  60. 'dataVague': data_vague
  61. }
  62. json_data = Auth.build_auth(json_data)
  63. response = requests.post(url, data=json_data)
  64. return response.json()
  65. @staticmethod
  66. def find_by_block_or_hash_or_data_vague(block_number, hash_code, data_vague):
  67. url = BASE_URL + '/ethmev/findByHashOrBlockOrDataVague'
  68. json_data = {
  69. 'block': block_number,
  70. 'hash': hash_code,
  71. 'dataVague': data_vague
  72. }
  73. json_data = Auth.build_auth(json_data)
  74. response = requests.post(url, data=json_data)
  75. return response.json()
  76. @staticmethod
  77. def find_sum2(token0, token1, level):
  78. url = BASE_URL + '/swappath/findBySumValueAndLevel'
  79. sumValue = hex(int(token0, 16) + int(token1, 16))
  80. json_data = {
  81. 'sum_value': sumValue,
  82. 'level': level
  83. }
  84. json_data = Auth.build_auth(json_data)
  85. response = requests.post(url, data=json_data)
  86. return response.json()
  87. @staticmethod
  88. def find_lp_route(lp):
  89. url = BASE_URL + '/swappath/findLevel2PathByLpAddress'
  90. json_data = {
  91. 'lp_address': lp,
  92. }
  93. json_data = Auth.build_auth(json_data)
  94. response = requests.post(url, data=json_data)
  95. return response.json()
  96. #[{'factory': '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac', 'LP': '0x397FF1542f962076d0BFE58eA045FfA2d347ACa0',
  97. # 'isEthW': False, 'feei': '3000', 'symbol1': 'WETH', 'symbol0': 'DEAD',
  98. # 'sum2': '0x160e313cb784589c3cbdff999568989d6727c580a', 'router': '0xd9e1ce17f2641f24ae83637ab66a2cca9c378b9f',
  99. # 'token0': '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 'name': 'SUSHI_DEAD_WETH',
  100. # 'token1': '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', 'decimals1': '18', 'id': 17,
  101. # 'decimals0': '6', 'r0': '454361158501473', 'r1': '324837295322546263677'},
  102. # {'factory': '0x9DEB29c9a4c7A88a3C0257393b7f3335338D9A9D', 'LP': '0x3Aa370AacF4CB08C7E1E7AA8E8FF9418D73C7e0F',
  103. # 'isEthW': False, 'feei': '3000', 'symbol1': 'WETH', 'symbol0': 'DEAD',
  104. # 'sum2': '0x160e313cb784589c3cbdff999568989d6727c580a', 'router': '0xCeB90E4C17d626BE0fACd78b79c9c87d7ca181b3',
  105. # 'token0': '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 'name': 'CRO_DEAD_WETH',
  106. # 'token1': '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', 'decimals1': '18', 'id': 1,
  107. # 'decimals0': '6', 'r0': '78658280524836', 'r1': '56258963016259372187'}]
  108. @staticmethod
  109. def delete_by_hash(hash_code):
  110. url = BASE_URL + '/ethmev/deleteByHash'
  111. json_data = {
  112. 'hash': hash_code,
  113. }
  114. json_data = Auth.build_auth(json_data)
  115. response = requests.post(url, data=json_data)
  116. return json.loads(response.text)
  117. def newBot(add):
  118. name = 'BOT.' + add[2:4]+ add[-2:]
  119. print(EthMevModel.append_or_update(2, add, {'name': name}))
  120. def isHsInSql(hash):
  121. r = EthMevModel.find_by_hash(hash)
  122. if r['data']:
  123. return eval(r['data'][0]['data'])
  124. return False
  125. def getSUM2Path(token0, token1):
  126. r = EthMevModel.find_sum2(token0, token1, 2)
  127. data = []
  128. if r['data']:
  129. r = eval(r['data']['data'])
  130. for lp2 in r:
  131. if lp2[0]['type'] != lp2[1]['type']:
  132. continue
  133. lp0 = lp2[0]['LP']
  134. lp1 = lp2[1]['LP']
  135. if {'lp0': lp0, 'lp1': lp1} in data:
  136. continue
  137. data.append({'lp0': lp0, 'lp1': lp1})
  138. return data
  139. printTime('SQL:OK')
  140. if __name__ == '__main__':
  141. #print('SQL:OK')
  142. #newBot('0x04156f068e0e8b5abe30fac002ae3b05529a3ddc')
  143. #printTime()
  144. #printTime((EthMevModel.find_by_block('topLp')['data']) + (EthMevModel.find_by_block('normalLp')['data']))
  145. printTime(speedTest(EthMevModel.find_by_hash, '0x123'))
  146. #printTime(EthMevModel.find_lp_route('0x397FF1542f962076d0BFE58eA045FfA2d347ACa0'))
  147. #printTime()
  148. #print(EthMevModel.append_or_update(1, '0xethw', {'name': 'ETHW', 'symbol': 'ETHW', 'decimals': 18}))
  149. #print(EthMevModel.append_or_update(2, '0xb4971d0dda22359ad86867362b7fc3206ea0d86b', {'name': '410.v4.0'}))
  150. #0x3b3ae79`0df4f312e745d270119c6052904fb6790
  151. #print(EthMevModel.find_by_hash('0xc5831a345bbc3fef632705ca72b5a16c5d0f2e22642a03cc22779cca56b2a0c8'))
  152. #print(EthMevModel.find_by_data_vague("0x19aec29cf3f52e3b0ac2ebe55c13070c979f88b89"))
  153. #print(EthMevModel.find_by_block_or_hash_or_data_vague(1, '', ''))
  154. #print(EthMevModel.delete_by_hash(123))
  155. #printTime((EthMevModel.find_by_block(15766460)))
  156. #dexArray = (EthMevModel.find_by_block(2))
  157. #tokenArray = (EthMevModel.find_by_block(1))
  158. #lpArray = (EthMevModel.find_by_block(0))