pendingV4.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # GAS WAR
  2. # 检测到相同LP的pending,就增加gas,gas=别人的gas+1
  3. # 如果gas大于maxprice ,就跳过。 maxprice = tradeTimes - 1e6
  4. # 主角是 我的HASH 对方的HASH
  5. # 我的HASH是唯一的,只能用一次,就丢弃
  6. # 我的hash和对方hash的和是唯一的,只能用一次,就丢弃
  7. from web3Tools import *
  8. import sqlTools as sql
  9. #pending filter
  10. id = w3.eth.filter('pending')
  11. print(id.filter_id)
  12. myTradeInfo = {}
  13. botTradeIfno = {}
  14. blockInfo = {}
  15. initHsS = []
  16. initBlockNumber = w3.eth.blockNumber
  17. # 我的hash和对方hash的和 避免同一个hash 一直加手续费
  18. hashUesdSum2 = []
  19. gasUpHash = []
  20. baseGasPrice = w3.eth.getBlock('pending').baseFeePerGas
  21. while True:
  22. try:
  23. # 拉pending hash
  24. hsArry = id.get_new_entries()
  25. if len(initHsS) > 1e4:
  26. initHsS = []
  27. if hsArry:
  28. block = w3.eth.blockNumber
  29. if block!= initBlockNumber:
  30. # 如果区块更新了,就初始化某些变量
  31. # 因为区块变化了,交易内容就失效了
  32. #printTime(block)
  33. gasUpHash = []
  34. myTradeInfo = {}
  35. botTradeIfno = {}
  36. blockInfo = {}
  37. initBlockNumber = block
  38. baseGasPrice = w3.eth.getBlock('pending').baseFeePerGas
  39. # 从所有PENDING HASH中,找出 我的hash 和 与我相关的hash = 别人的hash
  40. for hsi in hsArry:
  41. hash = hsi.hex()
  42. if hash in initHsS:
  43. continue
  44. initHsS.append(hash)
  45. try:
  46. # 拉交易信息
  47. x = (w3.eth.getTransaction(hsi))
  48. except BaseException as err:
  49. continue
  50. input = x['input']
  51. nonce = x['nonce'] # 对比nonce
  52. fromAdd = x['from'].lower()
  53. function = input[:10]
  54. toAdd = str(x['to']).lower()
  55. hash = x['hash'].hex()
  56. value = x['value']
  57. gasLimit = int(x['gas'])
  58. if x['type'] == '0x2' :
  59. gasPrice = baseGasPrice + x['maxPriorityFeePerGas']
  60. if gasPrice > x['maxFeePerGas']:
  61. gasPrice = x['maxFeePerGas']
  62. elif x['type'] == '0x0':
  63. gasPrice = x['gasPrice']
  64. # 这里面的continue ,只是为了简单过滤,绝对和我无关的hash
  65. if function in swapFunction:
  66. continue
  67. if gasPrice < baseGasPrice * 0.2:
  68. continue
  69. if function in ddosFunction:
  70. continue
  71. if len(input) > 64*64:
  72. continue
  73. if len(input) < 73:
  74. continue
  75. if hash in myTradeInfo:
  76. continue
  77. if toAdd in ddosAddress:
  78. continue
  79. if toAdd == v5Address.lower():
  80. continue
  81. #找出我的hash
  82. if toAdd == v4Address.lower() :
  83. # 大于1E6的是交易hash、
  84. if gasLimit <= 1e6:
  85. continue
  86. if hash in myTradeInfo:
  87. continue
  88. myTradeInfo[hash] = {}
  89. myTradeInfo[hash]['LP0'] = input[34:74]
  90. myTradeInfo[hash]['LP1'] = input[34 + 64:74 + 64]
  91. myTradeInfo[hash]['LP2'] = input[34 + 64 *2 :74 + 64 * 2 ]
  92. myTradeInfo[hash]['fromAddress'] = fromAdd
  93. myTradeInfo[hash]['toAddress'] = toAdd
  94. myTradeInfo[hash]['gasPrice'] = gasPrice
  95. myTradeInfo[hash]['nonce'] = nonce
  96. myTradeInfo[hash]['gasMaxPrice'] = gasLimit - 1e6
  97. myTradeInfo[hash]['gasLimit'] = gasLimit
  98. #printTime(block, myTradeInfo[hash]['fromAddress'], 'nowGasPrice', gasPrice, 'maxGasPrice', myTradeInfo[hash]['gasMaxPrice'], myTradeInfo[hash])
  99. myTradeInfo[hash]['inputData'] = input
  100. continue
  101. # 经过所有能想到的过滤后,和过滤自己的hash后。剩下的hash,都要进行破解。
  102. #trade Info decode
  103. # traceCall 的目的是为了检查出,input里面没有LP信息的交易
  104. r = w3.debug.traceCall(fromAdd, toAdd, input, value, block)
  105. if not r:
  106. printTime('err', fromAdd, toAdd, input, value, block)
  107. continue
  108. if 'error' in r:
  109. continue
  110. if 'calls' not in r:
  111. continue
  112. tradeInfo = w3.debug.transferDecodeFromCalldata(r['calls'])
  113. # lp.swap ?
  114. # [{
  115. # 'token': ,
  116. # 'from':,
  117. # 'to': ',
  118. # 'amount':)})]
  119. if tradeInfo:
  120. # 判断进行了token交易或者转账两次
  121. if len(tradeInfo) >= 2:
  122. # 是否来来回交易
  123. tokenAyyay = []
  124. tradeLaihui = False
  125. for tradeI in tradeInfo:
  126. token = tradeI['token']
  127. fromAdd = tradeI['from']
  128. to = tradeI['to']
  129. # 不管from to 是什么地址,都放入这里,就可以被后面检查出来。
  130. input = input + to[2:]
  131. if token not in tokenAyyay:
  132. tokenAyyay.append(token)
  133. for tradeII in tradeInfo:
  134. fromAddI = tradeII['from']
  135. toI = tradeII['to']
  136. if fromAdd == toI or to == fromAddI:
  137. tradeLaihui = True
  138. break
  139. # 有进出交易
  140. if not tradeLaihui:
  141. continue
  142. # 是否有两个币交易过
  143. if len(tokenAyyay) < 2:
  144. continue
  145. blockInfo[hash] = {}
  146. blockInfo[hash]['input'] = input
  147. blockInfo[hash]['nonce'] = nonce
  148. blockInfo[hash]['fromAddress'] = fromAdd
  149. blockInfo[hash]['toAddress'] = toAdd
  150. blockInfo[hash]['hash'] = hash
  151. blockInfo[hash]['value'] = value
  152. blockInfo[hash]['gasLimit'] = gasLimit
  153. blockInfo[hash]['gasPrice'] = gasPrice
  154. # 循环我的hash,我只用一次
  155. for myHash in myTradeInfo:
  156. # 如果HASH已经被加速了,就跳过
  157. if myHash in gasUpHash:
  158. continue
  159. myGasPrice = myTradeInfo[myHash]['gasPrice']
  160. # 我的LP数组
  161. lpArray = []
  162. LP0 = myTradeInfo[myHash]['LP0']
  163. LP1 = myTradeInfo[myHash]['LP1']
  164. LP2 = myTradeInfo[myHash]['LP2']
  165. lpArray.append(LP0)
  166. lpArray.append(LP1)
  167. if LP2 != '0000000000000000000000000000000000000000':
  168. lpArray.append(LP2)
  169. # 循环相关LP,相关用每一次
  170. for hashI in blockInfo:
  171. if hashI in myTradeInfo:
  172. continue
  173. # 如果HASH已经被加速了,就跳过
  174. if myHash in gasUpHash:
  175. continue
  176. # 只配对一次
  177. sumValue = hex(int(myHash, 16) + int(hashI, 16))
  178. if sumValue in hashUesdSum2:
  179. continue
  180. hashUesdSum2.append(sumValue)
  181. input = blockInfo[hashI]['input']
  182. gasPrice = blockInfo[hashI]['gasPrice']
  183. for lpI in lpArray:
  184. #找出是否相关,并不快。
  185. if lpI in input:
  186. # 如果对方gas 大于我的 maxPrice,就投降
  187. if gasPrice / 1e9 > myTradeInfo[myHash]['gasMaxPrice']:
  188. printTime(block, 'bei la bao', gasPrice/1e9, myTradeInfo[myHash]['gasMaxPrice'])
  189. gasUpHash.append(myHash)
  190. break
  191. # 如果对方gas大于我,我就加gas
  192. if gasPrice > myGasPrice:
  193. printTime(block, 'Find BOT', 'gasPrice', gasPrice/1e9, hashI)
  194. maxGasPrice = gasPrice * 1.13
  195. #只是geth的过滤,确定
  196. if maxGasPrice < myGasPrice * 1.11:
  197. maxGasPrice = myGasPrice * 1.12
  198. printTime(block, 'gasPrice UP', myGasPrice/1e9, '>', maxGasPrice/1e9, '<', myTradeInfo[myHash]['gasMaxPrice'], myHash)
  199. params = {}
  200. try:
  201. params['method'] = 'sendAddress'
  202. params['params'] = {'nonce': myTradeInfo[myHash]['nonce'],
  203. 'fromAddress': myTradeInfo[myHash]['fromAddress'],
  204. 'toAddress': myTradeInfo[myHash]['toAddress'],
  205. 'inputData': myTradeInfo[myHash]['inputData'],
  206. 'gasPrice': maxGasPrice,
  207. 'gasLimit': myTradeInfo[myHash]['gasLimit']}
  208. hs = requests.post(url='http://127.0.0.1:410/operation', json=params).text
  209. if hs !='None':
  210. gasUpHash.append(myHash)
  211. printTime(block, 'https://www.oklink.com/en/ethw/tx/' + str(hs))
  212. #printTime(myTradeInfo[myHash])
  213. printTime('-' * 80)
  214. break
  215. except BaseException as err:
  216. printTime('send err', err)
  217. printTime(myTradeInfo)
  218. hs = 'err seed'
  219. time.sleep(0.001)
  220. except BaseException as err:
  221. print(traceback.format_exc())
  222. print(err)
  223. time.sleep(10)
  224. try:
  225. id = w3.eth.filter('pending')
  226. except:
  227. time.sleep(10)
  228. continue