1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-22 12:32:02 +00:00

refactor(core): Move _get_fee_rate_str() to common and optionally add coin shortcut.

[no changelog]
This commit is contained in:
Andrew Kozlik 2022-10-25 14:44:45 +02:00 committed by Andrew Kozlik
parent 3594de8c7f
commit 318d31d111
3 changed files with 23 additions and 16 deletions

View File

@ -6,6 +6,7 @@ from trezor.crypto import bech32, bip32, der
from trezor.crypto.curve import bip340, secp256k1 from trezor.crypto.curve import bip340, secp256k1
from trezor.crypto.hashlib import sha256 from trezor.crypto.hashlib import sha256
from trezor.enums import InputScriptType, OutputScriptType from trezor.enums import InputScriptType, OutputScriptType
from trezor.strings import format_amount
from trezor.utils import HashWriter, ensure from trezor.utils import HashWriter, ensure
if TYPE_CHECKING: if TYPE_CHECKING:
@ -179,3 +180,20 @@ def tagged_hashwriter(tag: bytes) -> HashWriter:
ctx = sha256(tag_digest) ctx = sha256(tag_digest)
ctx.update(tag_digest) ctx.update(tag_digest)
return HashWriter(ctx) return HashWriter(ctx)
def format_fee_rate(
fee_rate: float, coin: CoinInfo, include_shortcut: bool = False
) -> str:
# Use format_amount to get correct thousands separator -- micropython's built-in
# formatting doesn't add thousands sep to floating point numbers.
# We multiply by 100 to get a fixed-point integer with two decimal places,
# and add 0.5 to round to the nearest integer.
fee_rate_formatted = format_amount(int(fee_rate * 100 + 0.5), 2)
if include_shortcut and coin.coin_shortcut != "BTC":
shortcut = " " + coin.coin_shortcut
else:
shortcut = ""
return f"{fee_rate_formatted} sat{shortcut}/{'v' if coin.segwit else ''}B"

View File

@ -8,6 +8,7 @@ from trezor.strings import format_amount, format_timestamp
from trezor.ui import layouts from trezor.ui import layouts
from .. import addresses from .. import addresses
from ..common import format_fee_rate
from . import omni from . import omni
if not utils.BITCOIN_ONLY: if not utils.BITCOIN_ONLY:
@ -165,7 +166,7 @@ async def confirm_modify_fee(
user_fee_change, user_fee_change,
format_coin_amount(abs(user_fee_change), coin, amount_unit), format_coin_amount(abs(user_fee_change), coin, amount_unit),
format_coin_amount(total_fee_new, coin, amount_unit), format_coin_amount(total_fee_new, coin, amount_unit),
fee_rate_amount=_get_fee_rate_str(fee_rate, coin), fee_rate_amount=format_fee_rate(fee_rate, coin) if fee_rate >= 0 else None,
) )
@ -183,18 +184,6 @@ async def confirm_joint_total(
) )
def _get_fee_rate_str(fee_rate: float, coin: CoinInfo) -> str | None:
if fee_rate >= 0:
# Use format_amount to get correct thousands separator -- micropython's built-in
# formatting doesn't add thousands sep to floating point numbers.
# We multiply by 100 to get a fixed-point integer with two decimal places,
# and add 0.5 to round to the nearest integer.
fee_rate_formatted = format_amount(int(fee_rate * 100 + 0.5), 2)
return f"({fee_rate_formatted} sat/{'v' if coin.segwit else ''}B)"
else:
return None
async def confirm_total( async def confirm_total(
ctx: wire.Context, ctx: wire.Context,
spending: int, spending: int,
@ -207,7 +196,7 @@ async def confirm_total(
ctx, ctx,
total_amount=format_coin_amount(spending, coin, amount_unit), total_amount=format_coin_amount(spending, coin, amount_unit),
fee_amount=format_coin_amount(fee, coin, amount_unit), fee_amount=format_coin_amount(fee, coin, amount_unit),
fee_rate_amount=_get_fee_rate_str(fee_rate, coin), fee_rate_amount=format_fee_rate(fee_rate, coin) if fee_rate >= 0 else None,
) )

View File

@ -886,7 +886,7 @@ async def confirm_total(
text.bold(fee_amount) text.bold(fee_amount)
if fee_rate_amount is not None: if fee_rate_amount is not None:
text.normal("\n" + fee_rate_amount) text.normal(f"\n({fee_rate_amount})")
await raise_if_cancelled(interact(ctx, HoldToConfirm(text), br_type, br_code)) await raise_if_cancelled(interact(ctx, HoldToConfirm(text), br_type, br_code))
@ -999,7 +999,7 @@ async def confirm_modify_fee(
text.normal("Transaction fee:\n") text.normal("Transaction fee:\n")
text.bold(total_fee_new) text.bold(total_fee_new)
if fee_rate_amount is not None: if fee_rate_amount is not None:
text.normal("\n" + fee_rate_amount) text.normal(f"\n({fee_rate_amount})")
await raise_if_cancelled( await raise_if_cancelled(
interact(ctx, HoldToConfirm(text), "modify_fee", ButtonRequestType.SignTx) interact(ctx, HoldToConfirm(text), "modify_fee", ButtonRequestType.SignTx)
) )