From e3a1d1a7f341366d186370898c8bbd47ad27c12b Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 15 Dec 2020 14:08:10 -0600 Subject: [PATCH] pyln-client: to_whole_satoshi returns the rounded up satoshi value A fractional satoshi value isn't really useful; rounding up loses precision but that's why you called "whole satoshi", wasn't it? Changelog-Changed: pyln-client: Millisatoshi has new method, `to_whole_satoshi`; *rounds value up* to the nearest whole satoshi --- contrib/pyln-client/pyln/client/lightning.py | 7 +++++++ tests/plugins/df_accepter.py | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/contrib/pyln-client/pyln/client/lightning.py b/contrib/pyln-client/pyln/client/lightning.py index 4c4bbf142..2d9b3c946 100644 --- a/contrib/pyln-client/pyln/client/lightning.py +++ b/contrib/pyln-client/pyln/client/lightning.py @@ -92,6 +92,13 @@ class Millisatoshi: """ return Decimal(self.millisatoshis) / 1000 + def to_whole_satoshi(self) -> int: + """ + Return an int respresenting the number of satoshis; + rounded up to the nearest satoshi + """ + return (self.millisatoshis + 999) // 1000 + def to_btc(self) -> Decimal: """ Return a Decimal representing the number of bitcoin. diff --git a/tests/plugins/df_accepter.py b/tests/plugins/df_accepter.py index 770489640..b7f013058 100755 --- a/tests/plugins/df_accepter.py +++ b/tests/plugins/df_accepter.py @@ -84,12 +84,12 @@ def on_openchannel(openchannel2, plugin, **kwargs): psbt_obj = psbt_from_base64(funding['psbt']) excess = Millisatoshi(funding['excess_msat']) - change_cost = Millisatoshi(124 * feerate // 1000 * 1000) + change_cost = Millisatoshi(124 * feerate) dust_limit = Millisatoshi(253 * 1000) if excess > (dust_limit + change_cost): addr = plugin.rpc.newaddr()['bech32'] change = excess - change_cost - output = tx_output_init(int(change.to_satoshi()), get_script(addr)) + output = tx_output_init(change.to_whole_satoshi(), get_script(addr)) psbt_add_output_at(psbt_obj, 0, 0, output) return {'result': 'continue', 'psbt': psbt_to_base64(psbt_obj, 0),