github.com/pdaian/flashboys2@v0.0.0-20190718175736-b101c35361f0/etherdelta/scripts/find_succ_txs.py (about)

     1  import argparse
     2  from _pysha3 import keccak_256
     3  from web3 import Web3, HTTPProvider
     4  import json
     5  
     6  web3 = Web3(HTTPProvider('http://localhost:8549'))
     7  #web3 = Web3(HTTPProvider('https://mainnet.infura.io/Ky03pelFIxoZdAUsr82w'))
     8  
     9  etherDeltaAddress = '0x8d12A197cB00D4747a1fe03395095ce2A5CC6819'
    10  etherAddress = '0000000000000000000000000000000000000000000000000000000000000000'
    11  
    12  tradeAPI = '0x' + \
    13             keccak_256(
    14                 b'Trade(address,uint256,address,uint256,address,address)'
    15             ).hexdigest()
    16  
    17  parser = argparse.ArgumentParser(description='EtherDelta Arbitrage Bot.')
    18  parser.add_argument('--st',dest='st' ,type=int, action='store', default='5000000')
    19  parser.add_argument('--len',dest='len' ,type=int, action='store', default='100')
    20  parser.add_argument('--r',dest='r' ,type=int, action='store', default='20')
    21  args = parser.parse_args()
    22  
    23  startBlock = args.st
    24  endBlock = args.st + args.len
    25  ratio = args.r
    26  result_dir = '../results/succ_txs-{}-{}-{}.txt'.format(startBlock,endBlock,ratio)
    27  
    28  
    29  import os
    30  if os.path.isfile(result_dir):
    31      print('Previous file exists.')
    32      with open(result_dir, 'r') as f:
    33          lines = f.readlines()
    34          if(len(lines) >= 1):
    35              print('last line:',lines[-1])
    36              number = int(lines[-1].split()[0])
    37  
    38  else:
    39      print('No previous file.')
    40      number = 0
    41  
    42  
    43  
    44  for idx in range(max(startBlock, number), endBlock + 1, ratio):
    45          block = web3.eth.getBlock(idx)
    46          transactions = block['transactions']
    47          print('block number:', idx)
    48          for txHash in transactions:
    49              tx = web3.eth.getTransaction(txHash)
    50              receipt = web3.eth.getTransactionReceipt(txHash)
    51              token_pair_list = []
    52              for log in receipt['logs']:
    53                  if 'topics' in log and len(log['topics']):
    54                      if log['topics'][0].hex() == tradeAPI:
    55                        token_pair_list.append((log['data'][24 + 2: 64 + 2],log['data'][24 + 128 + 2: 64 + 128+ 2]))
    56  
    57              num = len(token_pair_list)
    58              tag = None
    59              if num == 2 and token_pair_list[0][0] == token_pair_list[1][1] and token_pair_list[1][0] == token_pair_list[0][1]:
    60                  tag = 'Arbitrage'
    61              elif num == 1:
    62                  tag = 'Trade'
    63              elif num:
    64                  tag = 'Unknown'
    65              if tag is not None:
    66                  result = "{} {} {} {} {} {}\n".format(idx, txHash.hex(), tag, tx['from'], tx['to'], tx['input'])
    67                  print(result)
    68                  with open(result_dir,'a') as f:
    69                      f.write(result)
    70  
    71