Browse Source

pyln: fix Millisatoshi div with msat itself

Before this patch this fails: `Millisatoshi(42) / Millisatoshi(2)`
This is an operation that should return the ratio between the two
operands as a float number. Same goes for __floordiv__ operator `//`.

Changelog-None
ppa
Michael Schmoock 4 years ago
committed by Christian Decker
parent
commit
fffc343dd7
  1. 12
      contrib/pyln-client/pyln/client/lightning.py

12
contrib/pyln-client/pyln/client/lightning.py

@ -176,13 +176,19 @@ class Millisatoshi:
def __sub__(self, other: 'Millisatoshi') -> 'Millisatoshi':
return Millisatoshi(int(self) - int(other))
def __mul__(self, other: int) -> 'Millisatoshi':
def __mul__(self, other: Union[int, float]) -> 'Millisatoshi':
if isinstance(other, Millisatoshi):
raise TypeError("Resulting unit msat^2 is not supported")
return Millisatoshi(floor(self.millisatoshis * other))
def __truediv__(self, other: Union[int, float]) -> 'Millisatoshi':
def __truediv__(self, other: Union[int, float, 'Millisatoshi']) -> Union['Millisatoshi', float]:
if isinstance(other, Millisatoshi):
return self.millisatoshis / other.millisatoshis
return Millisatoshi(floor(self.millisatoshis / other))
def __floordiv__(self, other: Union[int, float]) -> 'Millisatoshi':
def __floordiv__(self, other: Union[int, float, 'Millisatoshi']) -> Union['Millisatoshi', int]:
if isinstance(other, Millisatoshi):
return self.millisatoshis // other.millisatoshis
return Millisatoshi(floor(self.millisatoshis // float(other)))
def __mod__(self, other: Union[float, int]) -> 'Millisatoshi':

Loading…
Cancel
Save