Skip to content

Instantly share code, notes, and snippets.

@petertodd
Created March 7, 2013 06:22
Show Gist options
  • Save petertodd/5105934 to your computer and use it in GitHub Desktop.
Save petertodd/5105934 to your computer and use it in GitHub Desktop.
Shitty mempool analyzer
#!/usr/bin/python3
import os
import sys
import struct
from opentimestamps._internal import hexlify,unhexlify
import opentimestamps._bitcoinrpc as btcrpc
proxy = btcrpc.ServiceProxy('http://')
def fees_for_tx(tx):
vout_sum = 0
for vout in tx['vout']:
vout_sum += vout['value']
vin_sum = 0
for vin in tx['vin']:
vin_txhash = vin['txid']
vin_tx = proxy.getrawtransaction(vin_txhash,1)
for vin_vout in vin_tx['vout']:
if vin_vout['n'] == vin['vout']:
vin_sum += vin_vout['value']
break
return (float(vin_sum-vout_sum),float(vin_sum),float(vout_sum))
total_fees = 0
max_fee = 0
max_fee_hash = None
fees_by_tx = []
for tx in proxy.getrawmempool():
tx = proxy.getrawtransaction(tx,1)
fee,vin_sum,vout_sum = fees_for_tx(tx)
total_fees += fee
tx['size'] = len(tx['hex'])/2
tx['adjfee'] = fee/tx['size']
fees_by_tx.append((fee,tx))
fees_by_tx = sorted(fees_by_tx,key = lambda v: v[1]['adjfee'])
sum_above_threshold = 0
sum_below_theshold = 0
sum_size_above = 0
sum_size_below = 0
sum_max_size_fees = 0
total_size = 0
last_above = None
for (fee,tx) in reversed(fees_by_tx):
total_size += len(tx['hex'])/2
if total_size < 1000000:
sum_max_size_fees += fee
if total_size < 250000:
sum_size_above += len(tx['hex'])/2
sum_above_threshold += fee
print(tx['adjfee'],fee,tx['txid'])
else:
if total_size - len(tx['hex'])/2 < 250000:
last_above = (fee,tx)
sum_size_below += len(tx['hex'])/2
sum_below_theshold += fee
print('below margin fee:',last_above[0],last_above[1]['adjfee'],last_above[1]['txid'])
print('above:',sum_above_threshold,'below:',sum_below_theshold,'max:',sum_max_size_fees)
print('above size:',sum_size_above,'below size:',sum_size_below)
print('total size:',total_size)
print('Total:',total_fees)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment