pylightning: millisatoshis type is int

Millisatoshi's inner representation expected to be an int, otherwise unwanted exceptions could occur
The following example raises: TypeError: __int__ returned non-int (type decimal.Decimal)

from lightning import Millisatoshi
one_sat = Millisatoshi("1sat")
two_sats = one_sat * 2
This commit is contained in:
Gálli Zoltán
2019-04-16 13:35:55 +02:00
committed by Rusty Russell
parent 4985693bea
commit 7f8e4de522

View File

@@ -34,12 +34,15 @@ class Millisatoshi:
self.millisatoshis = Decimal(v[0:-3]) * 1000 self.millisatoshis = Decimal(v[0:-3]) * 1000
elif v.endswith("btc"): elif v.endswith("btc"):
self.millisatoshis = Decimal(v[0:-3]) * 1000 * 10**8 self.millisatoshis = Decimal(v[0:-3]) * 1000 * 10**8
else:
raise TypeError("Millisatoshi must be string with msat/sat/btc suffix or int")
if self.millisatoshis != int(self.millisatoshis): if self.millisatoshis != int(self.millisatoshis):
raise ValueError("Millisatoshi must be a whole number") raise ValueError("Millisatoshi must be a whole number")
self.millisatoshis = int(self.millisatoshis)
elif isinstance(v, Millisatoshi): elif isinstance(v, Millisatoshi):
self.millisatoshis = v.millisatoshis self.millisatoshis = v.millisatoshis
elif int(v) == v: elif int(v) == v:
self.millisatoshis = v self.millisatoshis = int(v)
else: else:
raise TypeError("Millisatoshi must be string with msat/sat/btc suffix or int") raise TypeError("Millisatoshi must be string with msat/sat/btc suffix or int")