Roman Zeyde
5 years ago
6 changed files with 79 additions and 43 deletions
@ -0,0 +1,41 @@ |
|||||
|
import binascii |
||||
|
import json |
||||
|
import os |
||||
|
import socket |
||||
|
|
||||
|
|
||||
|
class Daemon: |
||||
|
def __init__(self, port, cookie_dir): |
||||
|
self.sock = socket.create_connection(('localhost', port)) |
||||
|
self.fd = self.sock.makefile() |
||||
|
path = os.path.join(os.path.expanduser(cookie_dir), '.cookie') |
||||
|
cookie = binascii.b2a_base64(open(path, 'rb').read()) |
||||
|
self.cookie = cookie.decode('ascii').strip() |
||||
|
self.index = 0 |
||||
|
|
||||
|
def request(self, method, params_list): |
||||
|
obj = [{"method": method, "params": params, "id": self.index} |
||||
|
for params in params_list] |
||||
|
request = json.dumps(obj) |
||||
|
|
||||
|
msg = ('POST / HTTP/1.1\n' |
||||
|
'Authorization: Basic {}\n' |
||||
|
'Content-Length: {}\n\n' |
||||
|
'{}'.format(self.cookie, len(request), request)) |
||||
|
self.sock.sendall(msg.encode('ascii')) |
||||
|
|
||||
|
status = self.fd.readline().strip() |
||||
|
while True: |
||||
|
if self.fd.readline().strip(): |
||||
|
continue # skip headers |
||||
|
else: |
||||
|
break # next line will contain the response |
||||
|
|
||||
|
data = self.fd.readline().strip() |
||||
|
replies = json.loads(data) |
||||
|
for reply in replies: |
||||
|
assert reply['error'] is None, reply |
||||
|
assert reply['id'] == self.index |
||||
|
|
||||
|
self.index += 1 |
||||
|
return [d['result'] for d in replies] |
@ -0,0 +1,33 @@ |
|||||
|
#!/usr/bin/env python3 |
||||
|
import argparse |
||||
|
import daemon |
||||
|
|
||||
|
|
||||
|
def main(): |
||||
|
parser = argparse.ArgumentParser() |
||||
|
parser.add_argument('txid') |
||||
|
args = parser.parse_args() |
||||
|
|
||||
|
d = daemon.Daemon(port=8332, cookie_dir='~/.bitcoin') |
||||
|
txid = args.txid |
||||
|
|
||||
|
txn, = d.request('getrawtransaction', [[txid, True]]) |
||||
|
vin = txn['vin'] |
||||
|
|
||||
|
fee = 0.0 |
||||
|
for txi in txn['vin']: |
||||
|
prev_txid = txi['txid'] |
||||
|
prev_tx, = d.request('getrawtransaction', [[prev_txid, True]]) |
||||
|
index = txi['vout'] |
||||
|
prev_txo = prev_tx['vout'][index] |
||||
|
print(f"{prev_txid}:{index:<5} {prev_txo['value']:+20.8f}") |
||||
|
fee += prev_txo['value'] |
||||
|
|
||||
|
for i, txo in enumerate(txn['vout']): |
||||
|
print(f"{txid}:{i:<5} {-txo['value']:+20.8f}") |
||||
|
fee -= txo['value'] |
||||
|
|
||||
|
print(f"Fee = {1e6 * fee:.2f} uBTC = {1e8 * fee / txn['vsize']:.2f} sat/vB") |
||||
|
|
||||
|
if __name__ == '__main__': |
||||
|
main() |
Loading…
Reference in new issue