From 4e8c68de24b7abaabc205235d3dce25dd8f36785 Mon Sep 17 00:00:00 2001 From: grdddj Date: Wed, 22 Nov 2023 16:23:19 +0100 Subject: [PATCH] chore(core): hardcode ETH and Gwei units in ETH send summary [no changelog] --- core/src/apps/ethereum/layout.py | 25 ++++++++++++++++--------- core/tests/test_apps.ethereum.layout.py | 9 +++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/core/src/apps/ethereum/layout.py b/core/src/apps/ethereum/layout.py index 71e5f07369e..34b014f1daf 100644 --- a/core/src/apps/ethereum/layout.py +++ b/core/src/apps/ethereum/layout.py @@ -38,10 +38,10 @@ async def require_confirm_tx( to_str = "new contract?" chunkify = False - total_amount = format_ethereum_amount(value, token, network) - maximum_fee = format_ethereum_amount(gas_price * gas_limit, None, network) + total_amount = format_ethereum_amount(value, token, network, force_unit_eth=True) + maximum_fee = format_ethereum_amount(gas_price * gas_limit, None, network, force_unit_eth=True) gas_limit_str = f"{gas_limit} units" - gas_price_str = format_ethereum_amount(gas_price, None, network) + gas_price_str = format_ethereum_amount(gas_price, None, network, force_unit_gwei=True) items = ( ("Gas limit:", gas_limit_str), @@ -70,11 +70,11 @@ async def require_confirm_tx_eip1559( to_str = "new contract?" chunkify = False - total_amount = format_ethereum_amount(value, token, network) - maximum_fee = format_ethereum_amount(max_gas_fee * gas_limit, None, network) + total_amount = format_ethereum_amount(value, token, network, force_unit_eth=True) + maximum_fee = format_ethereum_amount(max_gas_fee * gas_limit, None, network, force_unit_eth=True) gas_limit_str = f"{gas_limit} units" - max_gas_fee_str = format_ethereum_amount(max_gas_fee, None, network) - max_priority_fee_str = format_ethereum_amount(max_priority_fee, None, network) + max_gas_fee_str = format_ethereum_amount(max_gas_fee, None, network, force_unit_gwei=True) + max_priority_fee_str = format_ethereum_amount(max_priority_fee, None, network, force_unit_gwei=True) items = ( ("Gas limit:", gas_limit_str), @@ -243,6 +243,8 @@ def format_ethereum_amount( value: int, token: EthereumTokenInfo | None, network: EthereumNetworkInfo, + force_unit_eth: bool = False, + force_unit_gwei: bool = False, ) -> str: from trezor.strings import format_amount @@ -253,8 +255,13 @@ def format_ethereum_amount( suffix = network.symbol decimals = 18 - # Don't want to display wei values for tokens with small decimal numbers - if decimals > 9 and value < 10 ** (decimals - 9): + if force_unit_eth: + pass + elif force_unit_gwei: + decimals = decimals - 9 + suffix = "Gwei " + suffix + elif decimals > 9 and value < 10 ** (decimals - 9): + # Don't want to display wei values for tokens with small decimal numbers suffix = "Wei " + suffix decimals = 0 diff --git a/core/tests/test_apps.ethereum.layout.py b/core/tests/test_apps.ethereum.layout.py index 324dfac79a0..3d40552be0e 100644 --- a/core/tests/test_apps.ethereum.layout.py +++ b/core/tests/test_apps.ethereum.layout.py @@ -50,6 +50,15 @@ def test_denominations(self): text = format_ethereum_amount(1000000000000000000000, None, ETH) self.assertEqual(text, "1,000 ETH") + def test_force_units(self): + wei_amount = 100_000_000 + text = format_ethereum_amount(wei_amount, None, ETH) + self.assertEqual(text, "100,000,000 Wei ETH") + text = format_ethereum_amount(wei_amount, None, ETH, force_unit_eth=True) + self.assertEqual(text, "0.0000000001 ETH") + text = format_ethereum_amount(wei_amount, None, ETH, force_unit_gwei=True) + self.assertEqual(text, "0.1 Gwei ETH") + def test_precision(self): text = format_ethereum_amount(1000000000000000001, None, ETH) self.assertEqual(text, "1.000000000000000001 ETH")