lbry-sdk/lbrynet/daemon/json_response_encoder.py

47 lines
1.5 KiB
Python
Raw Normal View History

2018-08-16 01:23:06 +02:00
from decimal import Decimal
from binascii import hexlify
from datetime import datetime
from json import JSONEncoder
2018-08-16 17:24:22 +02:00
from lbrynet.wallet.transaction import Transaction, Output
2018-08-16 01:23:06 +02:00
class JSONResponseEncoder(JSONEncoder):
def __init__(self, *args, ledger, **kwargs):
super().__init__(*args, **kwargs)
self.ledger = ledger
2018-08-16 17:24:22 +02:00
def default(self, obj): # pylint: disable=method-hidden
2018-08-16 01:23:06 +02:00
if isinstance(obj, Transaction):
return self.encode_transaction(obj)
if isinstance(obj, Output):
return self.encode_output(obj)
if isinstance(obj, datetime):
return obj.strftime("%Y%m%dT%H:%M:%S")
if isinstance(obj, Decimal):
return float(obj)
2018-08-16 21:55:33 +02:00
if isinstance(obj, bytes):
return obj.decode()
2018-08-16 01:23:06 +02:00
return super().default(obj)
def encode_transaction(self, tx):
return {
'txid': tx.id,
'inputs': [self.encode_input(txo) for txo in tx.inputs],
'outputs': [self.encode_output(txo) for txo in tx.outputs],
'total_input': tx.input_sum,
'total_output': tx.input_sum - tx.fee,
'total_fee': tx.fee,
'hex': hexlify(tx.raw).decode(),
}
def encode_output(self, txo):
return {
'nout': txo.position,
'amount': txo.amount,
'address': txo.get_address(self.ledger)
}
def encode_input(self, txi):
return self.encode_output(txi.txo_ref.txo)