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

Prefer Decimal over float in utils #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 8 additions & 7 deletions blockcypher/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from decimal import Decimal

from .constants import SHA_COINS, SCRYPT_COINS, COIN_SYMBOL_SET, COIN_SYMBOL_MAPPINGS, FIRST4_MKEY_CS_MAPPINGS_UPPER, UNIT_CHOICES, UNIT_MAPPINGS
from .crypto import script_to_address
Expand Down Expand Up @@ -30,7 +31,7 @@ def to_satoshis(input_quantity, input_type):

# convert to satoshis
if input_type in ('btc', 'mbtc', 'bit'):
satoshis = float(input_quantity) * float(UNIT_MAPPINGS[input_type]['satoshis_per'])
satoshis = Decimal(input_quantity) * Decimal(UNIT_MAPPINGS[input_type]['satoshis_per'])
elif input_type == 'satoshi':
satoshis = input_quantity
else:
Expand All @@ -42,7 +43,7 @@ def to_satoshis(input_quantity, input_type):
def from_satoshis(input_satoshis, output_type):
# convert to output_type,
if output_type in ('btc', 'mbtc', 'bit'):
return input_satoshis / float(UNIT_MAPPINGS[output_type]['satoshis_per'])
return Decimal(input_satoshis) / Decimal(UNIT_MAPPINGS[output_type]['satoshis_per'])
elif output_type == 'satoshi':
return int(input_satoshis)
else:
Expand Down Expand Up @@ -106,15 +107,15 @@ def format_crypto_units(input_quantity, input_type, output_type, coin_symbol=Non
assert is_valid_coin_symbol(coin_symbol=coin_symbol), coin_symbol
assert type(round_digits) is int

satoshis_float = to_satoshis(input_quantity=input_quantity, input_type=input_type)
satoshis = to_satoshis(input_quantity=input_quantity, input_type=input_type)

if round_digits:
satoshis_float = round(satoshis_float, -1*round_digits)
satoshis = round(satoshis, -1*round_digits)

output_quantity = from_satoshis(
input_satoshis=satoshis_float,
input_satoshis=satoshis,
output_type=output_type,
)
)

if output_type == 'bit' and round_digits >= 2:
pass
Expand Down Expand Up @@ -287,7 +288,7 @@ def is_valid_wallet_name(wallet_name):


def btc_to_satoshis(btc):
return int(float(btc) * UNIT_MAPPINGS['btc']['satoshis_per'])
return int(Decimal(btc) * UNIT_MAPPINGS['btc']['satoshis_per'])


def uses_only_hash_chars(string):
Expand Down