From fe65a70dce66722d7e2f9fc311682bf61ded721b Mon Sep 17 00:00:00 2001 From: Konnor Klashinsky <31221309+kklash@users.noreply.github.com> Date: Thu, 16 May 2019 03:50:23 -0700 Subject: [PATCH] Do not display token values in Wei for low-decimal value tokens (#141) * Do not display token values in Wei for low-decimal value tokens * update logic * add unit tests --- core/src/apps/ethereum/layout.py | 3 ++- core/tests/test_apps.ethereum.layout.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/core/src/apps/ethereum/layout.py b/core/src/apps/ethereum/layout.py index d446990852..3f977e1538 100644 --- a/core/src/apps/ethereum/layout.py +++ b/core/src/apps/ethereum/layout.py @@ -65,7 +65,8 @@ def format_ethereum_amount(value: int, token, chain_id: int, tx_type=None): suffix = networks.shortcut_by_chain_id(chain_id, tx_type) decimals = 18 - if value <= 1e9: + # Don't want to display wei values for tokens with small decimal numbers + if decimals > 9 and value < 10 ** (decimals - 9): suffix = "Wei " + suffix decimals = 0 diff --git a/core/tests/test_apps.ethereum.layout.py b/core/tests/test_apps.ethereum.layout.py index 5da5746864..3417bf1a88 100644 --- a/core/tests/test_apps.ethereum.layout.py +++ b/core/tests/test_apps.ethereum.layout.py @@ -1,5 +1,6 @@ from common import * from apps.ethereum.layout import format_ethereum_amount +from apps.ethereum.tokens import token_by_chain_address class TestEthereumLayout(unittest.TestCase): @@ -16,7 +17,7 @@ class TestEthereumLayout(unittest.TestCase): text = format_ethereum_amount(100000000, None, 1) self.assertEqual(text, '100000000 Wei ETH') text = format_ethereum_amount(1000000000, None, 1) - self.assertEqual(text, '1000000000 Wei ETH') + self.assertEqual(text, '0.000000001 ETH') text = format_ethereum_amount(10000000000, None, 1) self.assertEqual(text, '0.00000001 ETH') text = format_ethereum_amount(100000000000, None, 1) @@ -62,6 +63,27 @@ class TestEthereumLayout(unittest.TestCase): text = format_ethereum_amount(10000000000000000001, None, 9999) self.assertEqual(text, '10.000000000000000001 UNKN') + # tokens with low decimal values + # USDC has 6 decimals + usdc_token = token_by_chain_address(1, bytes.fromhex("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48")) + # ICO has 10 decimals + ico_token = token_by_chain_address(1, bytes.fromhex("a33e729bf4fdeb868b534e1f20523463d9c46bee")) + + # when decimals < 10, should never display 'Wei' format + text = format_ethereum_amount(1, usdc_token, 1) + self.assertEqual(text, '0.000001 USDC') + text = format_ethereum_amount(0, usdc_token, 1) + self.assertEqual(text, '0 USDC') + + text = format_ethereum_amount(1, ico_token, 1) + self.assertEqual(text, '1 Wei ICO') + text = format_ethereum_amount(9, ico_token, 1) + self.assertEqual(text, '9 Wei ICO') + text = format_ethereum_amount(10, ico_token, 1) + self.assertEqual(text, '0.000000001 ICO') + text = format_ethereum_amount(11, ico_token, 1) + self.assertEqual(text, '0.0000000011 ICO') + if __name__ == '__main__': unittest.main()