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

     1  import csv, csv_hack, json
     2  from receipts import write_receipt
     3  
     4  auctions = {}
     5  
     6  bidsdict = csv.DictReader(open('data/auctions.csv'))
     7  for bid in bidsdict:
     8      auction_num = bid['auction_id']
     9      if not auction_num in auctions:
    10          auctions[auction_num] = []
    11      auctions[auction_num].append(bid)
    12  
    13  self_price_deltas = {}
    14  pairwise_price_deltas = {}
    15  pairwise_price_percent_deltas = {}
    16  self_price_percent_deltas = {}
    17  self_time_deltas = {}
    18  pairwise_time_deltas = {}
    19  
    20  pairs = {}
    21  auctions_participated = {}
    22  
    23  for auction_num in auctions:
    24      last_bid = None
    25      last_bids_by_id = {}
    26      for bid in auctions[auction_num]:
    27          sender = bid['sender']
    28          if not sender in auctions_participated:
    29              auctions_participated[sender] = set()
    30          auctions_participated[sender].add(auction_num)
    31          bid['gas_price'] = int(bid['gas_price'])
    32          bid['time_seen'] = int(bid['time_seen'])
    33          if last_bid is not None and last_bid['gas_price'] != 0:
    34              counterbidder = last_bid['sender']
    35              price_delta = bid['gas_price'] - last_bid['gas_price']
    36              price_percent_delta = (price_delta / (float(bid['gas_price'] + last_bid['gas_price'])/2)) * 100
    37              time_delta = (bid['time_seen'] - last_bid['time_seen']) / (10 ** 9)
    38              price_delta /=(10 ** 9)
    39              if price_delta < 0:
    40                  continue # ignore bids that aren't raises; TODO check effects
    41              bidder_pairs = str(sender) + "-" + str(counterbidder)
    42              if not bidder_pairs in pairwise_price_deltas:
    43                  pairwise_price_deltas[bidder_pairs] = []
    44                  pairwise_price_percent_deltas[bidder_pairs] = []
    45                  pairwise_time_deltas[bidder_pairs] = []
    46                  pairs[sender] = set()
    47              pairwise_time_deltas[bidder_pairs].append(time_delta)
    48              pairwise_price_deltas[bidder_pairs].append(price_delta)
    49              pairwise_price_percent_deltas[bidder_pairs].append(price_percent_delta)
    50              pairs[sender].add(bidder_pairs)
    51  
    52          if sender in last_bids_by_id and last_bids_by_id[sender]['gas_price'] != 0:
    53              last_self_bid = last_bids_by_id[sender]
    54              price_delta = bid['gas_price'] - last_self_bid['gas_price']
    55              time_delta = (bid['time_seen'] - last_self_bid['time_seen']) / (10 ** 9)
    56              price_percent_delta = (price_delta / ((bid['gas_price'] + last_self_bid['gas_price'])/2)) * 100
    57              price_delta /=(10 ** 9)
    58              if not sender in self_price_deltas:
    59                  self_time_deltas[sender] = []
    60                  self_price_deltas[sender] = []
    61                  self_price_percent_deltas[sender] = []
    62              self_time_deltas[sender].append(time_delta)
    63              self_price_deltas[sender].append(price_delta)
    64              self_price_percent_deltas[sender].append(price_percent_delta)
    65          last_bid = bid
    66          last_bids_by_id[sender] = bid
    67  
    68  # convert sets to lists for json reasons
    69  for pair in pairs:
    70      pairs[pair] = list(pairs[pair])
    71  
    72  for sender in auctions_participated:
    73      auctions_participated[sender] = list(auctions_participated[sender])
    74  
    75  print(pairs)
    76  
    77  data = {"pairwise_time": pairwise_time_deltas, "pairwise_price": pairwise_price_deltas,"pairwise_price_percent": pairwise_price_percent_deltas,"self_time": self_time_deltas,"self_price": self_price_deltas, "self_price_percent": self_price_deltas, "pairs": pairs, "auctions": auctions_participated}
    78  f = open('data/pairwise_self.csv', 'w')
    79  f.write(json.dumps(data))
    80