mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 17:31:36 +00:00
util: Satoshis and Fiat should not be namedtuples
undo part of 37b009a342
due to json encoding problems
This commit is contained in:
parent
960855d0aa
commit
923a9c36cb
1 changed files with 27 additions and 5 deletions
|
@ -130,15 +130,35 @@ class UserCancelled(Exception):
|
|||
'''An exception that is suppressed from the user'''
|
||||
pass
|
||||
|
||||
class Satoshis(NamedTuple):
|
||||
value: int
|
||||
|
||||
# note: this is not a NamedTuple as then its json encoding cannot be customized
|
||||
class Satoshis(object):
|
||||
__slots__ = ('value',)
|
||||
|
||||
def __new__(cls, value):
|
||||
self = super(Satoshis, cls).__new__(cls)
|
||||
self.value = value
|
||||
return self
|
||||
|
||||
def __repr__(self):
|
||||
return 'Satoshis(%d)'%self.value
|
||||
|
||||
def __str__(self):
|
||||
return format_satoshis(self.value) + " BTC"
|
||||
|
||||
class Fiat(NamedTuple):
|
||||
value: Optional[Decimal]
|
||||
ccy: str
|
||||
|
||||
# note: this is not a NamedTuple as then its json encoding cannot be customized
|
||||
class Fiat(object):
|
||||
__slots__ = ('value', 'ccy')
|
||||
|
||||
def __new__(cls, value, ccy):
|
||||
self = super(Fiat, cls).__new__(cls)
|
||||
self.ccy = ccy
|
||||
self.value = value
|
||||
return self
|
||||
|
||||
def __repr__(self):
|
||||
return 'Fiat(%s)'% self.__str__()
|
||||
|
||||
def __str__(self):
|
||||
if self.value is None or self.value.is_nan():
|
||||
|
@ -146,8 +166,10 @@ class Fiat(NamedTuple):
|
|||
else:
|
||||
return "{:.2f}".format(self.value) + ' ' + self.ccy
|
||||
|
||||
|
||||
class MyEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
# note: this does not get called for namedtuples :( https://bugs.python.org/issue30343
|
||||
from .transaction import Transaction
|
||||
if isinstance(obj, Transaction):
|
||||
return obj.as_dict()
|
||||
|
|
Loading…
Add table
Reference in a new issue