Browse Source

Display INFO level log message on estimate_fee_per_kb() with result

master
Kristaps Kaupe 6 years ago
parent
commit
16cf93ab82
No known key found for this signature in database
GPG Key ID: D47B1B4232B55437
  1. 15
      jmbitcoin/jmbitcoin/amount.py
  2. 17
      jmclient/jmclient/blockchaininterface.py

15
jmbitcoin/jmbitcoin/amount.py

@ -1,8 +1,10 @@
from decimal import Decimal from decimal import Decimal
def btc_to_sat(btc): def btc_to_sat(btc):
return int(Decimal(btc) * Decimal('1e8')) return int(Decimal(btc) * Decimal('1e8'))
def sat_to_btc(sat): def sat_to_btc(sat):
return Decimal(sat) / Decimal('1e8') return Decimal(sat) / Decimal('1e8')
@ -15,6 +17,7 @@ def sat_to_btc(sat):
# 1.12300000sat = 0.00000001 BTC = 1sat # 1.12300000sat = 0.00000001 BTC = 1sat
# 1btc = 1.00000000 BTC = 10000000sat # 1btc = 1.00000000 BTC = 10000000sat
def amount_to_sat(amount_str): def amount_to_sat(amount_str):
amount_str = str(amount_str) amount_str = str(amount_str)
if amount_str.lower().endswith("btc"): if amount_str.lower().endswith("btc"):
@ -26,21 +29,31 @@ def amount_to_sat(amount_str):
else: else:
return int(Decimal(amount_str)) return int(Decimal(amount_str))
def amount_to_btc(amount_str): def amount_to_btc(amount_str):
return amount_to_sat(amount_str) / Decimal('1e8') return amount_to_sat(amount_str) / Decimal('1e8')
def amount_to_sat_str(amount_str): def amount_to_sat_str(amount_str):
return str(amount_to_sat(amount_str)) + " sat" return str(amount_to_sat(amount_str)) + " sat"
def amount_to_btc_str(amount_str): def amount_to_btc_str(amount_str):
return str(amount_to_btc(amount_str)) + " BTC" return str(amount_to_btc(amount_str)) + " BTC"
def amount_to_str(amount_str): def amount_to_str(amount_str):
return amount_to_btc_str(amount_str) + " (" + amount_to_sat_str(amount_str) + ")" return (amount_to_btc_str(amount_str) + " (" +
amount_to_sat_str(amount_str) + ")")
def sat_to_str(sat): def sat_to_str(sat):
return '%.8f' % sat_to_btc(sat) return '%.8f' % sat_to_btc(sat)
def sat_to_str_p(sat): def sat_to_str_p(sat):
return '%+.8f' % sat_to_btc(sat) return '%+.8f' % sat_to_btc(sat)
def fee_per_kb_to_str(feerate):
return str(feerate) + " sat/vkB (" + str(feerate / 1000) + " sat/vB)"

17
jmclient/jmclient/blockchaininterface.py

@ -157,8 +157,8 @@ class ElectrumWalletInterface(BlockchainInterface): #pragma: no cover
return int(max(1000, random.uniform(N * float(0.8), N * float(1.2)))) return int(max(1000, random.uniform(N * float(0.8), N * float(1.2))))
fee = self.wallet.network.synchronous_get(('blockchain.estimatefee', [N] fee = self.wallet.network.synchronous_get(('blockchain.estimatefee', [N]
)) ))
log.debug("Got fee: " + str(fee))
fee_per_kb_sat = int(float(fee) * 100000000) fee_per_kb_sat = int(float(fee) * 100000000)
log.info("Got fee: " + btc.fee_per_kb_to_str(fee_per_kb_sat))
return fee_per_kb_sat return fee_per_kb_sat
class BitcoinCoreInterface(BlockchainInterface): class BitcoinCoreInterface(BlockchainInterface):
@ -367,10 +367,12 @@ class BitcoinCoreInterface(BlockchainInterface):
btc_relayfee = rpc_result.get('relayfee', btc_relayfee) btc_relayfee = rpc_result.get('relayfee', btc_relayfee)
if btc_relayfee > 0: if btc_relayfee > 0:
relayfee_in_sat = int(Decimal(1e8) * Decimal(btc_relayfee)) relayfee_in_sat = int(Decimal(1e8) * Decimal(btc_relayfee))
log.debug("Using this min relay fee as tx fee floor: " + str(relayfee_in_sat)) log.info("Using this min relay fee as tx fee floor: " +
btc.fee_per_kb_to_str(relayfee_in_sat))
return int(max(relayfee_in_sat, random.uniform(N * float(0.8), N * float(1.2)))) return int(max(relayfee_in_sat, random.uniform(N * float(0.8), N * float(1.2))))
else: # cannot get valid relayfee: fall back to 1000 sat/kbyte else: # cannot get valid relayfee: fall back to 1000 sat/kbyte
log.debug("Using this min relay fee as tx fee floor (fallback): 1000") log.info("Using this min relay fee as tx fee floor " +
"(fallback): " + btc.fee_per_kb_to_str(1000))
return int(max(1000, random.uniform(N * float(0.8), N * float(1.2)))) return int(max(1000, random.uniform(N * float(0.8), N * float(1.2))))
# Special bitcoin core case: sometimes the highest priority # Special bitcoin core case: sometimes the highest priority
@ -379,14 +381,19 @@ class BitcoinCoreInterface(BlockchainInterface):
tries = 2 if N == 1 else 1 tries = 2 if N == 1 else 1
estimate = -1 estimate = -1
retval = -1
for i in range(tries): for i in range(tries):
rpc_result = self.rpc('estimatesmartfee', [N + i]) rpc_result = self.rpc('estimatesmartfee', [N + i])
estimate = rpc_result.get('feerate', estimate) estimate = rpc_result.get('feerate', estimate)
if estimate > 0: if estimate > 0:
break break
else: # estimate <= 0 else: # estimate <= 0
return 10000 retval = 10000
return int(Decimal(1e8) * Decimal(estimate))
if retval == -1:
retval = int(Decimal(1e8) * Decimal(estimate))
log.info("Using tx fee: " + btc.fee_per_kb_to_str(retval))
return retval
def get_current_block_height(self): def get_current_block_height(self):
return self.rpc("getblockchaininfo", [])["blocks"] return self.rpc("getblockchaininfo", [])["blocks"]

Loading…
Cancel
Save