1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

src/apps/ethereum: fix layout for values around 1e9 Wei

This commit is contained in:
Pavol Rusnak 2018-03-03 23:37:21 +01:00
parent 34e60899c0
commit c7c5f55508
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
6 changed files with 79 additions and 43 deletions

View File

@ -60,7 +60,7 @@ def format_ethereum_amount(value, token, chain_id):
suffix = networks.suffix_by_chain_id(chain_id)
decimals = 18
if value < 1e18:
if value <= 1e9:
suffix = 'Wei ' + suffix
decimals = 0

View File

@ -1,6 +1,6 @@
def token_by_chain_address(chain_id, address):
if not address:
return 0
return None
for token in tokens:
if chain_id == token[0] and address == token[1]:
return token

View File

@ -18,9 +18,12 @@ def unimport(genfunc):
return inner
def ensure(cond):
def ensure(cond, msg=None):
if not cond:
raise AssertionError()
if msg is None:
raise AssertionError()
else:
raise AssertionError(msg)
def chunks(items, size):

View File

@ -5,33 +5,61 @@ from apps.ethereum.layout import format_ethereum_amount
class TestEthereumLayout(unittest.TestCase):
def test_format(self):
text = format_ethereum_amount((1).to_bytes(5, 'big'), None, 1)
self.assertEqual(text, '1 Wei')
text = format_ethereum_amount((1000).to_bytes(5, 'big'), None, 1)
self.assertEqual(text, '1000 Wei')
text = format_ethereum_amount((1000000000000000000).to_bytes(20, 'big'), None, 1)
text = format_ethereum_amount((1).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1 Wei ETH')
text = format_ethereum_amount((1000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1000 Wei ETH')
text = format_ethereum_amount((1000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1000000 Wei ETH')
text = format_ethereum_amount((10000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '10000000 Wei ETH')
text = format_ethereum_amount((100000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '100000000 Wei ETH')
text = format_ethereum_amount((1000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1000000000 Wei ETH')
text = format_ethereum_amount((10000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.00000001 ETH')
text = format_ethereum_amount((100000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.0000001 ETH')
text = format_ethereum_amount((1000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.000001 ETH')
text = format_ethereum_amount((10000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.00001 ETH')
text = format_ethereum_amount((100000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.0001 ETH')
text = format_ethereum_amount((1000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.001 ETH')
text = format_ethereum_amount((10000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.01 ETH')
text = format_ethereum_amount((100000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '0.1 ETH')
text = format_ethereum_amount((1000000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1 ETH')
text = format_ethereum_amount((10000000000000000000).to_bytes(20, 'big'), None, 1)
text = format_ethereum_amount((10000000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '10 ETH')
text = format_ethereum_amount((10000000000000000000).to_bytes(20, 'big'), None, 61)
self.assertEqual(text, '10 ETC')
text = format_ethereum_amount((1000000000000000000).to_bytes(20, 'big'), None, 31)
text = format_ethereum_amount((100000000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '100 ETH')
text = format_ethereum_amount((1000000000000000000000).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1000 ETH')
text = format_ethereum_amount((1000000000000000000).to_bytes(32, 'big'), None, 61)
self.assertEqual(text, '1 ETC')
text = format_ethereum_amount((1000000000000000000).to_bytes(32, 'big'), None, 31)
self.assertEqual(text, '1 tRSK')
text = format_ethereum_amount((1000000000000000001).to_bytes(20, 'big'), None, 1)
text = format_ethereum_amount((1000000000000000001).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '1.000000000000000001 ETH')
text = format_ethereum_amount((10000000000000000001).to_bytes(20, 'big'), None, 1)
text = format_ethereum_amount((10000000000000000001).to_bytes(32, 'big'), None, 1)
self.assertEqual(text, '10.000000000000000001 ETH')
text = format_ethereum_amount((10000000000000000001).to_bytes(20, 'big'), None, 61)
text = format_ethereum_amount((10000000000000000001).to_bytes(32, 'big'), None, 61)
self.assertEqual(text, '10.000000000000000001 ETC')
text = format_ethereum_amount((1000000000000000001).to_bytes(20, 'big'), None, 31)
text = format_ethereum_amount((1000000000000000001).to_bytes(32, 'big'), None, 31)
self.assertEqual(text, '1.000000000000000001 tRSK')
# unknown chain
text = format_ethereum_amount((1).to_bytes(20, 'big'), None, 9999)
self.assertEqual(text, '1 Wei')
text = format_ethereum_amount((10000000000000000001).to_bytes(20, 'big'), None, 9999)
text = format_ethereum_amount((1).to_bytes(32, 'big'), None, 9999)
self.assertEqual(text, '1 Wei UNKN')
text = format_ethereum_amount((10000000000000000001).to_bytes(32, 'big'), None, 9999)
self.assertEqual(text, '10.000000000000000001 UNKN')

View File

@ -7,15 +7,18 @@ class TestEthereumTokens(unittest.TestCase):
def test_token_by_chain_address(self):
token = tokens.token_by_chain_address(1, b'\x7d\xd7\xf5\x6d\x69\x7c\xc0\xf2\xb5\x2b\xd5\x5c\x05\x7f\x37\x8f\x1f\xe6\xab\x4b')
self.assertEqual(token['symbol'], '$TEAK')
self.assertEqual(token[2], '$TEAK')
token = tokens.token_by_chain_address(1, b'\x59\x41\x6a\x25\x62\x8a\x76\xb4\x73\x0e\xc5\x14\x86\x11\x4c\x32\xe0\xb5\x82\xa1')
self.assertEqual(token['symbol'], 'PLASMA')
self.assertEqual(token['decimal'], 6)
token = tokens.token_by_chain_address(3, b'\x95\xd7\x32\x1e\xdc\xe5\x19\x41\x9b\xa1\xdb\xc6\x0a\x89\xba\xfb\xf5\x5e\xac\x0d')
self.assertEqual(token['symbol'], 'PLASMA')
self.assertEqual(token['decimal'], 6)
self.assertEqual(token[2], 'PLASMA')
self.assertEqual(token[3], 6)
token = tokens.token_by_chain_address(4, b'\x0a\x05\x7a\x87\xce\x9c\x56\xd7\xe3\x36\xb4\x17\xc7\x9c\xf3\x0e\x8d\x27\x86\x0b')
self.assertEqual(token[2], 'WALL')
self.assertEqual(token[3], 15)
token = tokens.token_by_chain_address(8, b'\x4b\x48\x99\xa1\x0f\x3e\x50\x7d\xb2\x07\xb0\xee\x24\x26\x02\x9e\xfa\x16\x8a\x67')
self.assertEqual(token['symbol'], 'QWARK')
self.assertEqual(token[2], 'QWARK')
# invalid adress, invalid chain
token = tokens.token_by_chain_address(999, b'\x00\xFF')

View File

@ -1,3 +1,5 @@
from trezor.utils import ensure
class SkipTest(Exception):
pass
@ -12,7 +14,7 @@ class AssertRaisesContext:
def __exit__(self, exc_type, exc_value, tb):
if exc_type is None:
assert False, "%r not raised" % self.expected
ensure(False, "%r not raised" % self.expected)
if issubclass(exc_type, self.expected):
return True
return False
@ -21,17 +23,17 @@ class AssertRaisesContext:
class TestCase:
def fail(self, msg=''):
assert False, msg
ensure(False, msg)
def assertEqual(self, x, y, msg=''):
if not msg:
msg = "%r vs (expected) %r" % (x, y)
assert x == y, msg
ensure(x == y, msg)
def assertNotEqual(self, x, y, msg=''):
if not msg:
msg = "%r not expected to be equal %r" % (x, y)
assert x != y, msg
ensure(x != y, msg)
def assertAlmostEqual(self, x, y, places=None, msg='', delta=None):
if x == y:
@ -52,7 +54,7 @@ class TestCase:
if not msg:
msg = '%r != %r within %r places' % (x, y, places)
assert False, msg
ensure(False, msg)
def assertNotAlmostEqual(self, x, y, places=None, msg='', delta=None):
if delta is not None and places is not None:
@ -71,52 +73,52 @@ class TestCase:
if not msg:
msg = '%r == %r within %r places' % (x, y, places)
assert False, msg
ensure(False, msg)
def assertIs(self, x, y, msg=''):
if not msg:
msg = "%r is not %r" % (x, y)
assert x is y, msg
ensure(x is y, msg)
def assertIsNot(self, x, y, msg=''):
if not msg:
msg = "%r is %r" % (x, y)
assert x is not y, msg
ensure(x is not y, msg)
def assertIsNone(self, x, msg=''):
if not msg:
msg = "%r is not None" % x
assert x is None, msg
ensure(x is None, msg)
def assertIsNotNone(self, x, msg=''):
if not msg:
msg = "%r is None" % x
assert x is not None, msg
ensure(x is not None, msg)
def assertTrue(self, x, msg=''):
if not msg:
msg = "Expected %r to be True" % x
assert x, msg
ensure(x, msg)
def assertFalse(self, x, msg=''):
if not msg:
msg = "Expected %r to be False" % x
assert not x, msg
ensure(not x, msg)
def assertIn(self, x, y, msg=''):
if not msg:
msg = "Expected %r to be in %r" % (x, y)
assert x in y, msg
ensure(x in y, msg)
def assertIsInstance(self, x, y, msg=''):
assert isinstance(x, y), msg
ensure(isinstance(x, y), msg)
def assertRaises(self, exc, func=None, *args, **kwargs):
if func is None:
return AssertRaisesContext(exc)
try:
func(*args, **kwargs)
assert False, "%r not raised" % exc
ensure(False, "%r not raised" % exc)
except Exception as e:
if isinstance(e, exc):
return