2018-09-17 22:13:30 +02:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-09-17 22:31:44 +02:00
|
|
|
from lbrynet.schema.address import encode_address, decode_address
|
2019-02-28 22:54:37 +01:00
|
|
|
from lbrynet.schema.constants import CURRENCY_NAMES, CURRENCY_MAP
|
2019-02-28 22:21:42 +01:00
|
|
|
from lbrynet.schema.legacy_schema_v1.fee import Fee as FeeHelper
|
2019-02-26 22:26:15 +01:00
|
|
|
from lbrynet.schema.proto2 import fee_pb2
|
2018-09-17 22:13:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def migrate(fee):
|
|
|
|
if len(list(fee.keys())) == 3 and 'currency' in fee and 'amount' in fee and 'address' in fee:
|
|
|
|
return FeeHelper.load({
|
|
|
|
"version": "_0_0_1",
|
|
|
|
"currency": fee['currency'],
|
|
|
|
"amount": fee['amount'],
|
|
|
|
"address": decode_address(fee['address'])
|
|
|
|
})
|
|
|
|
if len(list(fee.keys())) > 1:
|
|
|
|
raise Exception("Invalid fee")
|
|
|
|
|
|
|
|
currency = list(fee.keys())[0]
|
|
|
|
amount = fee[currency]['amount']
|
|
|
|
address = fee[currency]['address']
|
|
|
|
|
|
|
|
return FeeHelper.load({
|
|
|
|
"version": "_0_0_1",
|
|
|
|
"currency": currency,
|
|
|
|
"amount": amount,
|
|
|
|
"address": decode_address(address)
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
class Fee(OrderedDict):
|
|
|
|
def __init__(self, fee):
|
|
|
|
if (len(fee) == 4 and "version" in fee and "currency" in fee
|
|
|
|
and "amount" in fee and "address" in fee):
|
|
|
|
OrderedDict.__init__(self, fee)
|
|
|
|
else:
|
|
|
|
OrderedDict.__init__(self, Fee.load_protobuf(migrate(fee)))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def currency(self):
|
|
|
|
return self['currency']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def address(self):
|
|
|
|
return self['address']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def amount(self):
|
|
|
|
return self['amount']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self):
|
|
|
|
return self['version']
|
|
|
|
|
|
|
|
@property
|
|
|
|
def protobuf(self):
|
|
|
|
pb = {
|
|
|
|
"version": self.version,
|
|
|
|
"currency": CURRENCY_MAP[self.currency],
|
|
|
|
"address": decode_address(self.address),
|
|
|
|
"amount": self.amount
|
|
|
|
}
|
|
|
|
return FeeHelper.load(pb)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_protobuf(cls, pb):
|
|
|
|
return cls({
|
|
|
|
"version": pb.version,
|
|
|
|
"currency": CURRENCY_NAMES[pb.currency],
|
|
|
|
"address": encode_address(pb.address),
|
|
|
|
"amount": pb.amount
|
|
|
|
})
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def deserialize(cls, serialized):
|
|
|
|
pb = fee_pb2.Fee()
|
|
|
|
pb.ParseFromString(serialized)
|
|
|
|
return cls.load_protobuf(pb)
|