minor Fee changes

This commit is contained in:
Lex Berezhny 2019-03-15 09:05:14 -04:00
parent 129a886f2b
commit d5e37acd8f
2 changed files with 36 additions and 19 deletions

View file

@ -330,10 +330,6 @@ class Fee:
def currency(self) -> str: def currency(self) -> str:
return FeeMessage.Currency.Name(self._fee.currency) return FeeMessage.Currency.Name(self._fee.currency)
@currency.setter
def currency(self, currency: str):
self._fee.currency = FeeMessage.Currency.Value(currency)
@property @property
def address(self) -> str: def address(self) -> str:
return b58encode(self._fee.address).decode() return b58encode(self._fee.address).decode()
@ -357,6 +353,18 @@ class Fee:
if self.currency == 'USD': if self.currency == 'USD':
return self.usd return self.usd
DEWIES = Decimal(COIN)
@property
def lbc(self) -> Decimal:
if self._fee.currency != FeeMessage.LBC:
raise ValueError('LBC can only be returned for LBC fees.')
return Decimal(self._fee.amount / self.DEWIES)
@lbc.setter
def lbc(self, amount: Decimal):
self.dewies = int(amount * self.DEWIES)
@property @property
def dewies(self) -> int: def dewies(self) -> int:
if self._fee.currency != FeeMessage.LBC: if self._fee.currency != FeeMessage.LBC:
@ -368,29 +376,27 @@ class Fee:
self._fee.amount = amount self._fee.amount = amount
self._fee.currency = FeeMessage.LBC self._fee.currency = FeeMessage.LBC
DEWEYS = Decimal(COIN) PENNIES = Decimal(100.0)
@property
def lbc(self) -> Decimal:
if self._fee.currency != FeeMessage.LBC:
raise ValueError('LBC can only be returned for LBC fees.')
return Decimal(self._fee.amount / self.DEWEYS)
@lbc.setter
def lbc(self, amount: Decimal):
self.dewies = int(amount * self.DEWEYS)
USD = Decimal(100.0)
@property @property
def usd(self) -> Decimal: def usd(self) -> Decimal:
if self._fee.currency != FeeMessage.USD: if self._fee.currency != FeeMessage.USD:
raise ValueError('USD can only be returned for USD fees.') raise ValueError('USD can only be returned for USD fees.')
return Decimal(self._fee.amount / self.USD) return Decimal(self._fee.amount / self.PENNIES)
@usd.setter @usd.setter
def usd(self, amount: Decimal): def usd(self, amount: Decimal):
self._fee.amount = int(amount * self.USD) self.pennies = int(amount * self.PENNIES)
@property
def pennies(self) -> int:
if self._fee.currency != FeeMessage.USD:
raise ValueError('Pennies can only be returned for USD fees.')
return self._fee.amount
@pennies.setter
def pennies(self, amount: int):
self._fee.amount = amount
self._fee.currency = FeeMessage.USD self._fee.currency = FeeMessage.USD

View file

@ -40,11 +40,22 @@ class TestFee(TestCase):
self.assertEqual(stream.fee.lbc, Decimal('1.01')) self.assertEqual(stream.fee.lbc, Decimal('1.01'))
self.assertEqual(stream.fee.dewies, 101000000) self.assertEqual(stream.fee.dewies, 101000000)
self.assertEqual(stream.fee.currency, 'LBC') self.assertEqual(stream.fee.currency, 'LBC')
stream.fee.dewies = 203000000
self.assertEqual(stream.fee.lbc, Decimal('2.03'))
self.assertEqual(stream.fee.dewies, 203000000)
self.assertEqual(stream.fee.currency, 'LBC')
with self.assertRaisesRegex(ValueError, 'USD can only be returned for USD fees.'): with self.assertRaisesRegex(ValueError, 'USD can only be returned for USD fees.'):
print(stream.fee.usd) print(stream.fee.usd)
with self.assertRaisesRegex(ValueError, 'Pennies can only be returned for USD fees.'):
print(stream.fee.pennies)
stream.fee.usd = Decimal('1.01') stream.fee.usd = Decimal('1.01')
self.assertEqual(stream.fee.usd, Decimal('1.01')) self.assertEqual(stream.fee.usd, Decimal('1.01'))
self.assertEqual(stream.fee.pennies, 101)
self.assertEqual(stream.fee.currency, 'USD')
stream.fee.pennies = 203
self.assertEqual(stream.fee.usd, Decimal('2.03'))
self.assertEqual(stream.fee.pennies, 203)
self.assertEqual(stream.fee.currency, 'USD') self.assertEqual(stream.fee.currency, 'USD')
with self.assertRaisesRegex(ValueError, 'LBC can only be returned for LBC fees.'): with self.assertRaisesRegex(ValueError, 'LBC can only be returned for LBC fees.'):
print(stream.fee.lbc) print(stream.fee.lbc)