Skip to content

Commit

Permalink
handled dust
Browse files Browse the repository at this point in the history
  • Loading branch information
fametrano committed Jan 3, 2021
1 parent ad30214 commit 0212284
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions btclib/amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@
_MAX_BITCOIN = Decimal("20_999_999.9769")


def valid_btc_amount(amount: Any) -> Decimal:
"Return the BTC amount as Decimal, if valid."
if amount is None:
return Decimal("0")
def valid_btc_amount(amount: Any, dust: Decimal = Decimal("0")) -> Decimal:
"Return the BTC amount as Decimal, if valid and not less than dust."
# any input that can be converted to str is fine
amount = "0" if amount is None else str(amount)
# using str in the Decimal constructor avoids the
# FloatOperation exception
# even if trapped by the context (which is the btclib default)
btc = Decimal(str(amount))
if not 0 <= btc <= _MAX_BITCOIN:
btc = Decimal(amount)
if not dust <= btc <= _MAX_BITCOIN:
raise BTClibValueError(f"invalid BTC amount: {amount}")
if btc == btc.quantize(_BITCOIN_PER_SATOSHI):
return btc
Expand All @@ -69,14 +68,13 @@ def sats_from_btc(amount: Decimal) -> int:
return int(btc * _SATOSHI_PER_BITCOIN)


def valid_sats_amount(amount: Any) -> int:
"Return the satoshi amount as int, if valid."
if amount is None:
return 0
sats = int(amount)
if sats != amount:
def valid_sats_amount(amount: Any, dust: int = 0) -> int:
"Return the satoshi amount as int, if valid and not less than dust."
# any input that can be converted to int is fine
sats = 0 if amount is None else int(amount)
if amount is not None and sats != amount:
raise BTClibTypeError(f"non-integer satoshi amount: {amount}")
if not 0 <= sats <= _MAX_SATOSHI:
if not dust <= sats <= _MAX_SATOSHI:
raise BTClibValueError(f"invalid satoshi amount: {amount}")
return sats

Expand Down

0 comments on commit 0212284

Please sign in to comment.