Skip to content

Commit dcb0054

Browse files
committed
Merge #1599: Use coins_to_satoshi() and satoshi_to_coins() from bitcointx everywhere
395050c Use coins_to_satoshi() and satoshi_to_coins() from bitcointx everywhere (Kristaps Kaupe) Pull request description: It's bad to have different functions for the same thing, `coins_to_satoshi()` were already used in some tests. Top commit has no ACKs. Tree-SHA512: aa9f464528472d9ffabea72b10212cb56f6ce3ee96c04ab4463053fa1361a01b171ac93671a1db54a026f427c65baefd8af81641cd22fe1f202d7dc4eb125434
2 parents 4062550 + 395050c commit dcb0054

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

scripts/bond-calculator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from optparse import OptionParser
77

88
from jmbase import EXIT_ARGERROR, jmprint, get_log, utxostr_to_utxo, EXIT_FAILURE
9-
from jmbitcoin import amount_to_sat, sat_to_btc
9+
from jmbitcoin import amount_to_sat, amount_to_str
1010
from jmclient import add_base_options, load_program_config, jm_single, get_bond_values
1111

1212
DESCRIPTION = """Given either a Bitcoin UTXO in the form TXID:n
@@ -110,7 +110,7 @@ def main() -> None:
110110
options.interest,
111111
options.exponent,
112112
orderbook)
113-
jmprint(f"Amount locked: {amount} ({sat_to_btc(amount)} btc)")
113+
jmprint(f"Amount locked: {amount_to_str(amount)}")
114114
jmprint(f"Confirmation time: {datetime.fromtimestamp(parameters['confirm_time'])}")
115115
jmprint(f"Interest rate: {parameters['interest']} ({parameters['interest'] * 100}%)")
116116
jmprint(f"Exponent: {parameters['exponent']}")

scripts/qtsupport.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
from PySide2.QtGui import *
2525
from PySide2.QtWidgets import *
2626

27-
from jmbitcoin.amount import amount_to_sat, btc_to_sat, sat_to_btc
27+
from bitcointx.core import satoshi_to_coins
28+
from jmbitcoin.amount import amount_to_sat, btc_to_sat, sat_to_str
2829
from jmbitcoin.bip21 import decode_bip21_uri
2930
from jmclient import (jm_single, validate_address, get_tumble_schedule)
3031

@@ -584,7 +585,7 @@ def __init__(self, default_value):
584585
self.setModeBTC()
585586
layout.addWidget(self.unitChooser)
586587
if default_value:
587-
self.valueInputBox.setText(str(sat_to_btc(amount_to_sat(
588+
self.valueInputBox.setText(str(satoshi_to_coins(amount_to_sat(
588589
default_value))))
589590
self.setLayout(layout)
590591

@@ -605,7 +606,7 @@ def onUnitChanged(self, index):
605606
sat_amount = self.valueInputBox.text()
606607
self.setModeBTC()
607608
if sat_amount:
608-
self.valueInputBox.setText('%.8f' % sat_to_btc(sat_amount))
609+
self.valueInputBox.setText(sat_to_str(sat_amount))
609610
else:
610611
# switch from BTC to sat
611612
btc_amount = self.valueInputBox.text()
@@ -616,7 +617,7 @@ def onUnitChanged(self, index):
616617
def setText(self, text):
617618
if text:
618619
if self.unitChooser.currentIndex() == 0:
619-
self.valueInputBox.setText(str(sat_to_btc(text)))
620+
self.valueInputBox.setText(str(satoshi_to_coins(text)))
620621
else:
621622
self.valueInputBox.setText(str(text))
622623
else:

src/jmbitcoin/amount.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import re
2+
from bitcointx.core import coins_to_satoshi, satoshi_to_coins
13
from decimal import Decimal
24
from typing import Any, Tuple, Union
3-
import re
45

56

67
def bitcoin_unit_to_power(btc_unit: str) -> int:
@@ -19,7 +20,7 @@ def bitcoin_unit_to_power(btc_unit: str) -> int:
1920

2021

2122
def btc_to_sat(btc: Union[int, str, Tuple, float, Decimal]) -> int:
22-
return int(Decimal(btc) * Decimal('1e8'))
23+
return coins_to_satoshi(Decimal(btc))
2324

2425

2526
def sat_to_unit_power(sat: int, power: int) -> Decimal:
@@ -77,11 +78,11 @@ def amount_to_str(amount_str: str) -> str:
7778

7879

7980
def sat_to_str(sat: int) -> str:
80-
return '%.8f' % sat_to_btc(sat)
81+
return '%.8f' % satoshi_to_coins(sat)
8182

8283

8384
def sat_to_str_p(sat: int) -> str:
84-
return '%+.8f' % sat_to_btc(sat)
85+
return '%+.8f' % satoshi_to_coins(sat, check_range=False)
8586

8687

8788
def fee_per_kb_to_str(feerate: Any) -> str:

0 commit comments

Comments
 (0)