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

feat(core/ethereum): use TokenInfo object instead of tuples

This commit is contained in:
matejcik 2021-08-31 11:45:21 +02:00 committed by matejcik
parent 4b34e4c001
commit 0c2863fc8d
4 changed files with 1838 additions and 1818 deletions

View File

@ -95,6 +95,12 @@ def format_ethereum_amount(value: int, token, chain_id: int):
elif token:
suffix = token[2]
decimals = token[3]
def format_ethereum_amount(
value: int, token: tokens.TokenInfo | None, chain_id: int
) -> str:
if token:
suffix = token.symbol
decimals = token.decimals
else:
suffix = networks.shortcut_by_chain_id(chain_id)
decimals = 18

File diff suppressed because it is too large Load Diff

View File

@ -12,10 +12,16 @@ def group_tokens(tokens):
return r
%>\
UNKNOWN_TOKEN = (None, None, None, None)
class TokenInfo:
def __init__(self, symbol: str, decimals: int) -> None:
self.symbol = symbol
self.decimals = decimals
UNKNOWN_TOKEN = TokenInfo("Wei UNKN", 0)
def token_by_chain_address(chain_id, address):
def token_by_chain_address(chain_id: int, address: bytes) -> TokenInfo:
if False:
pass
% for token_chain_id, tokens in group_tokens(supported_on("trezor2", erc20)).items():
@ -24,7 +30,7 @@ def token_by_chain_address(chain_id, address):
pass
% for t in tokens:
elif address == ${black_repr(t.address_bytes)}:
return (chain_id, address, ${black_repr(t.symbol)}, ${t.decimals}) # ${t.chain} / ${t.name.strip()}
return TokenInfo(${black_repr(t.symbol)}, ${t.decimals}) # ${t.chain} / ${t.name.strip()}
% endfor
% endfor
return UNKNOWN_TOKEN

View File

@ -10,22 +10,24 @@ 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[2], '$TEAK')
self.assertEqual(token.symbol, '$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[2], 'PLASMA')
self.assertEqual(token[3], 6)
self.assertEqual(token.symbol, 'PLASMA')
self.assertEqual(token.decimals, 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)
self.assertEqual(token.symbol, 'WALL')
self.assertEqual(token.decimals, 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[2], 'QWARK')
self.assertEqual(token.symbol, 'QWARK')
# invalid adress, invalid chain
token = tokens.token_by_chain_address(999, b'\x00\xFF')
self.assertIs(token, tokens.UNKNOWN_TOKEN)
self.assertEqual(token.symbol, 'Wei UNKN')
self.assertEqual(token.decimals, 0)
if __name__ == '__main__':