Skip to content

Instantly share code, notes, and snippets.

@achow101
Last active August 25, 2019 21:51
Show Gist options
  • Save achow101/dfb1016f78e653656ec40cd019f8116b to your computer and use it in GitHub Desktop.
Save achow101/dfb1016f78e653656ec40cd019f8116b to your computer and use it in GitHub Desktop.
import requests
import pprint
r = requests.get('https://blockchain.info/rawblock/0000000000000000002540e369b106bfd929585e0f059409bd2a956e6a6af79c')
r = r.json()
txs = r['tx']
first = True
packages = []
for i,tx in enumerate(txs):
if first:
first = False
continue
package = {}
tx_info = {}
in_val = 0
for txin in tx['inputs']:
in_val += txin['prev_out']['value']
package_iter = list(packages)
for p in package_iter:
if txin['prev_out']['tx_index'] in p:
if len(package) == 0:
package = p
else:
for ptx_hash,ptx_info in p.items():
if ptx_hash == 'feerate':
continue
package[ptx_hash] = ptx_info
packages.remove(p)
out_val = 0
for output in tx['out']:
out_val += output['value']
fee = in_val - out_val
vsize = (tx['weight'] + 3) / 4
feerate = fee / vsize
tx_info['hash'] = tx['hash']
tx_info['feerate'] = feerate
tx_info['fee'] = fee
tx_info['vsize'] = vsize
tx_info['index'] = i
package[tx['tx_index']] = tx_info
tot_fee = 0
tot_vsize = 0
for ptx_hash,ptx_info in package.items():
if ptx_hash == 'feerate':
continue
tot_fee += ptx_info['fee']
tot_vsize += ptx_info['vsize']
package['feerate'] = tot_fee / tot_vsize
packages.append(package)
pprint.pprint(sorted(packages, key = lambda package: package['feerate']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment