Skip to content

Commit

Permalink
chore(core): hardcode ETH and Gwei units in ETH send summary
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
grdddj committed Nov 22, 2023
1 parent 2172644 commit 4e8c68d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
25 changes: 16 additions & 9 deletions core/src/apps/ethereum/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
9 changes: 9 additions & 0 deletions core/tests/test_apps.ethereum.layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4e8c68d

Please sign in to comment.