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 authored and matejcik committed Nov 30, 2023
1 parent 2172644 commit 0873a45
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 76 deletions.
1 change: 1 addition & 0 deletions core/.changelog.d/3246.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Always display Ethereum fees in Gwei.
26 changes: 19 additions & 7 deletions core/src/apps/ethereum/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ async def require_confirm_tx(
total_amount = format_ethereum_amount(value, token, network)
maximum_fee = format_ethereum_amount(gas_price * gas_limit, None, network)
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 All @@ -63,7 +65,6 @@ async def require_confirm_tx_eip1559(
token: EthereumTokenInfo | None,
chunkify: bool,
) -> None:

if to_bytes:
to_str = address_from_bytes(to_bytes, network)
else:
Expand All @@ -73,8 +74,12 @@ async def require_confirm_tx_eip1559(
total_amount = format_ethereum_amount(value, token, network)
maximum_fee = format_ethereum_amount(max_gas_fee * gas_limit, None, network)
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 +248,7 @@ def format_ethereum_amount(
value: int,
token: EthereumTokenInfo | None,
network: EthereumNetworkInfo,
force_unit_gwei: bool = False,
) -> str:
from trezor.strings import format_amount

Expand All @@ -253,12 +259,18 @@ 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_gwei:
assert token is None
assert decimals >= 9
decimals = decimals - 9
suffix = "Gwei"
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

return f"{format_amount(value, decimals)} {suffix}"
amount = format_amount(value, decimals)
return f"{amount} {suffix}"


def limit_str(s: str, limit: int = 16) -> str:
Expand Down
7 changes: 7 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,13 @@ 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_gwei=True)
self.assertEqual(text, "0.1 Gwei")

def test_precision(self):
text = format_ethereum_amount(1000000000000000001, None, ETH)
self.assertEqual(text, "1.000000000000000001 ETH")
Expand Down
Loading

0 comments on commit 0873a45

Please sign in to comment.