Cleanup rebalance sendinvoiceless (#33)

* doc: cleanup and key-value for optional parameters

* rebalance: cleanup and refinements

* sendinvoiceless: cleanup and refinements

* summary: reflects upcoming changes of pylightning to_approx_str

* rebalance: check peer connection on local channels
This commit is contained in:
Michael Schmoock
2019-05-31 22:52:43 +02:00
committed by GitHub
parent 6d9c5a1f3c
commit f6309a2da0
6 changed files with 167 additions and 94 deletions

View File

@@ -48,29 +48,43 @@ def to_fiatstr(msat: Millisatoshi):
return "{}{:.2f}".format(plugin.currency_prefix,
int(msat) / 10**11 * plugin.fiat_per_btc)
# this is included here for backwards compatibility to old pylightning version
def msat_to_short_str(msat, digits: int = 3):
"""
Returns the shortmost string using common units representation.
# This is part of pylightning, but its just merged,
# so old releases wont have it yet.
def msat_to_approx_str(msat, digits: int = 3):
"""Returns the shortmost string using common units representation.
Rounds to significant `digits`. Default: 3
"""
# first round everything down 3 effective digits
round_to_n = lambda x, n: round(x, -int(floor(log10(x))) + (n - 1))
amount_eff = round_to_n(msat, digits)
result = None
# try different units and take shortest resulting normalized string
amounts = [
"%gbtc" % (amount_eff / 1000 / 10**8),
"%gmbtc" % (amount_eff / 1000 / 10**5),
"%gµbtc" % (amount_eff / 1000 / 10**2),
"%gsat" % (amount_eff / 1000),
"%gmsat" % (amount_eff),
]
return min(amounts, key=len)
# we try to increase digits to check if we did loose out on precision
# without gaining a shorter string, since this is a rarely used UI
# function, performance is not an issue. Adds at least one iteration.
while True:
# first round everything down to effective digits
amount_rounded = round_to_n(msat.millisatoshis, digits)
# try different units and take shortest resulting normalized string
amounts_str = [
"%gbtc" % (amount_rounded / 1000 / 10**8),
"%gsat" % (amount_rounded / 1000),
"%gmsat" % (amount_rounded),
]
test_result = min(amounts_str, key=len)
# check result and do another run if necessary
if test_result == result:
return result
elif not result or len(test_result) <= len(result):
digits = digits + 1
result = test_result
else:
return result
# appends an output table header that explains fields and capacity
def append_header(table, max_msat):
short_str = msat_to_short_str(max_msat)
short_str = msat_to_approx_str(Millisatoshi(max_msat))
table.append("%c%-13sOUT/OURS %c IN/THEIRS%12s%c SCID FLAG ALIAS"
% (draw.left, short_str, draw.mid, short_str, draw.right))