1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

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
This commit is contained in:
Konnor Klashinsky 2019-05-16 03:50:23 -07:00 committed by matejcik
parent 0d90aa1932
commit fe65a70dce
2 changed files with 25 additions and 2 deletions

View File

@ -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) suffix = networks.shortcut_by_chain_id(chain_id, tx_type)
decimals = 18 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 suffix = "Wei " + suffix
decimals = 0 decimals = 0

View File

@ -1,5 +1,6 @@
from common import * from common import *
from apps.ethereum.layout import format_ethereum_amount from apps.ethereum.layout import format_ethereum_amount
from apps.ethereum.tokens import token_by_chain_address
class TestEthereumLayout(unittest.TestCase): class TestEthereumLayout(unittest.TestCase):
@ -16,7 +17,7 @@ class TestEthereumLayout(unittest.TestCase):
text = format_ethereum_amount(100000000, None, 1) text = format_ethereum_amount(100000000, None, 1)
self.assertEqual(text, '100000000 Wei ETH') self.assertEqual(text, '100000000 Wei ETH')
text = format_ethereum_amount(1000000000, None, 1) 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) text = format_ethereum_amount(10000000000, None, 1)
self.assertEqual(text, '0.00000001 ETH') self.assertEqual(text, '0.00000001 ETH')
text = format_ethereum_amount(100000000000, None, 1) text = format_ethereum_amount(100000000000, None, 1)
@ -62,6 +63,27 @@ class TestEthereumLayout(unittest.TestCase):
text = format_ethereum_amount(10000000000000000001, None, 9999) text = format_ethereum_amount(10000000000000000001, None, 9999)
self.assertEqual(text, '10.000000000000000001 UNKN') 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__': if __name__ == '__main__':
unittest.main() unittest.main()