Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use coins_to_satoshi() and satoshi_to_coins() from bitcointx everywhere #1599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/bond-calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from optparse import OptionParser

from jmbase import EXIT_ARGERROR, jmprint, get_log, utxostr_to_utxo, EXIT_FAILURE
from jmbitcoin import amount_to_sat, sat_to_btc
from jmbitcoin import amount_to_sat, amount_to_str
from jmclient import add_base_options, load_program_config, jm_single, get_bond_values

DESCRIPTION = """Given either a Bitcoin UTXO in the form TXID:n
Expand Down Expand Up @@ -110,7 +110,7 @@ def main() -> None:
options.interest,
options.exponent,
orderbook)
jmprint(f"Amount locked: {amount} ({sat_to_btc(amount)} btc)")
jmprint(f"Amount locked: {amount_to_str(amount)}")
jmprint(f"Confirmation time: {datetime.fromtimestamp(parameters['confirm_time'])}")
jmprint(f"Interest rate: {parameters['interest']} ({parameters['interest'] * 100}%)")
jmprint(f"Exponent: {parameters['exponent']}")
Expand Down
9 changes: 5 additions & 4 deletions scripts/qtsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from jmbitcoin.amount import amount_to_sat, btc_to_sat, sat_to_btc
from bitcointx.core import satoshi_to_coins
from jmbitcoin.amount import amount_to_sat, btc_to_sat, sat_to_str
from jmbitcoin.bip21 import decode_bip21_uri
from jmclient import (jm_single, validate_address, get_tumble_schedule)

Expand Down Expand Up @@ -584,7 +585,7 @@ def __init__(self, default_value):
self.setModeBTC()
layout.addWidget(self.unitChooser)
if default_value:
self.valueInputBox.setText(str(sat_to_btc(amount_to_sat(
self.valueInputBox.setText(str(satoshi_to_coins(amount_to_sat(
default_value))))
self.setLayout(layout)

Expand All @@ -605,7 +606,7 @@ def onUnitChanged(self, index):
sat_amount = self.valueInputBox.text()
self.setModeBTC()
if sat_amount:
self.valueInputBox.setText('%.8f' % sat_to_btc(sat_amount))
self.valueInputBox.setText(sat_to_str(sat_amount))
else:
# switch from BTC to sat
btc_amount = self.valueInputBox.text()
Expand All @@ -616,7 +617,7 @@ def onUnitChanged(self, index):
def setText(self, text):
if text:
if self.unitChooser.currentIndex() == 0:
self.valueInputBox.setText(str(sat_to_btc(text)))
self.valueInputBox.setText(str(satoshi_to_coins(text)))
else:
self.valueInputBox.setText(str(text))
else:
Expand Down
12 changes: 5 additions & 7 deletions src/jmbitcoin/amount.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import re
from bitcointx.core import coins_to_satoshi, satoshi_to_coins
from decimal import Decimal
from typing import Any, Tuple, Union
import re


def btc_to_sat(btc: Union[int, str, Tuple, float, Decimal]) -> int:
return int(Decimal(btc) * Decimal('1e8'))

return coins_to_satoshi(Decimal(btc))

def sat_to_btc(sat: int) -> Decimal:
return Decimal(sat) / Decimal('1e8')

# 1 = 0.00000001 BTC = 1sat
# 1sat = 0.00000001 BTC = 1sat
Expand Down Expand Up @@ -53,11 +51,11 @@ def amount_to_str(amount_str: str) -> str:


def sat_to_str(sat: int) -> str:
return '%.8f' % sat_to_btc(sat)
return '%.8f' % satoshi_to_coins(sat)


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


def fee_per_kb_to_str(feerate: Any) -> str:
Expand Down
5 changes: 0 additions & 5 deletions test/jmbitcoin/test_amounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ def test_btc_to_sat() -> None:
assert(btc.btc_to_sat(Decimal("1.00000000")) == 100000000)


def test_sat_to_btc() -> None:
assert(btc.sat_to_btc(1) == Decimal("0.00000001"))
assert(btc.sat_to_btc(100000000) == Decimal("1.00000000"))


def test_amount_to_sat() -> None:
assert(btc.amount_to_sat("1") == 1)
assert(btc.amount_to_sat("1sat") == 1)
Expand Down