2024-01-26 11:16:55 +00:00
|
|
|
from common import * # isort:skip
|
2017-12-27 11:48:05 +00:00
|
|
|
|
2019-08-26 16:47:49 +00:00
|
|
|
if not utils.BITCOIN_ONLY:
|
2024-01-26 11:16:55 +00:00
|
|
|
from ethereum_common import make_network, make_token
|
|
|
|
|
2023-02-03 13:39:31 +00:00
|
|
|
from apps.ethereum import networks
|
2024-02-11 17:30:20 +00:00
|
|
|
from apps.ethereum.helpers import format_ethereum_amount
|
2023-02-03 13:39:31 +00:00
|
|
|
from apps.ethereum.tokens import UNKNOWN_TOKEN
|
|
|
|
|
|
|
|
ETH = networks.by_chain_id(1)
|
2017-12-27 11:48:05 +00:00
|
|
|
|
2019-08-26 16:47:49 +00:00
|
|
|
@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
|
2021-08-31 09:47:56 +00:00
|
|
|
class TestFormatEthereumAmount(unittest.TestCase):
|
2023-02-03 13:39:31 +00:00
|
|
|
def test_denominations(self):
|
|
|
|
text = format_ethereum_amount(1, None, ETH)
|
|
|
|
self.assertEqual(text, "1 Wei ETH")
|
|
|
|
text = format_ethereum_amount(1000, None, ETH)
|
|
|
|
self.assertEqual(text, "1,000 Wei ETH")
|
|
|
|
text = format_ethereum_amount(1000000, None, ETH)
|
|
|
|
self.assertEqual(text, "1,000,000 Wei ETH")
|
|
|
|
text = format_ethereum_amount(10000000, None, ETH)
|
|
|
|
self.assertEqual(text, "10,000,000 Wei ETH")
|
|
|
|
text = format_ethereum_amount(100000000, None, ETH)
|
|
|
|
self.assertEqual(text, "100,000,000 Wei ETH")
|
|
|
|
text = format_ethereum_amount(1000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.000000001 ETH")
|
|
|
|
text = format_ethereum_amount(10000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.00000001 ETH")
|
|
|
|
text = format_ethereum_amount(100000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.0000001 ETH")
|
|
|
|
text = format_ethereum_amount(1000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.000001 ETH")
|
|
|
|
text = format_ethereum_amount(10000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.00001 ETH")
|
|
|
|
text = format_ethereum_amount(100000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.0001 ETH")
|
|
|
|
text = format_ethereum_amount(1000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.001 ETH")
|
|
|
|
text = format_ethereum_amount(10000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.01 ETH")
|
|
|
|
text = format_ethereum_amount(100000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "0.1 ETH")
|
|
|
|
text = format_ethereum_amount(1000000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "1 ETH")
|
|
|
|
text = format_ethereum_amount(10000000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "10 ETH")
|
|
|
|
text = format_ethereum_amount(100000000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "100 ETH")
|
|
|
|
text = format_ethereum_amount(1000000000000000000000, None, ETH)
|
|
|
|
self.assertEqual(text, "1,000 ETH")
|
2017-12-27 11:48:05 +00:00
|
|
|
|
2023-11-22 15:23:19 +00:00
|
|
|
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")
|
|
|
|
|
2023-02-03 13:39:31 +00:00
|
|
|
def test_precision(self):
|
|
|
|
text = format_ethereum_amount(1000000000000000001, None, ETH)
|
|
|
|
self.assertEqual(text, "1.000000000000000001 ETH")
|
|
|
|
text = format_ethereum_amount(10000000000000000001, None, ETH)
|
|
|
|
self.assertEqual(text, "10.000000000000000001 ETH")
|
2017-12-27 11:48:05 +00:00
|
|
|
|
2023-02-03 13:39:31 +00:00
|
|
|
def test_symbols(self):
|
|
|
|
fake_network = make_network(symbol="FAKE")
|
|
|
|
text = format_ethereum_amount(1, None, fake_network)
|
|
|
|
self.assertEqual(text, "1 Wei FAKE")
|
|
|
|
text = format_ethereum_amount(1000000000000000000, None, fake_network)
|
|
|
|
self.assertEqual(text, "1 FAKE")
|
|
|
|
text = format_ethereum_amount(1000000000000000001, None, fake_network)
|
|
|
|
self.assertEqual(text, "1.000000000000000001 FAKE")
|
2018-02-09 12:36:08 +00:00
|
|
|
|
2021-08-31 09:47:56 +00:00
|
|
|
def test_unknown_chain(self):
|
2017-12-27 11:48:05 +00:00
|
|
|
# unknown chain
|
2023-02-03 13:39:31 +00:00
|
|
|
text = format_ethereum_amount(1, None, networks.UNKNOWN_NETWORK)
|
|
|
|
self.assertEqual(text, "1 Wei UNKN")
|
|
|
|
text = format_ethereum_amount(
|
|
|
|
10000000000000000001, None, networks.UNKNOWN_NETWORK
|
|
|
|
)
|
|
|
|
self.assertEqual(text, "10.000000000000000001 UNKN")
|
2017-12-27 11:48:05 +00:00
|
|
|
|
2021-08-31 09:47:56 +00:00
|
|
|
def test_tokens(self):
|
2019-05-16 10:50:23 +00:00
|
|
|
# tokens with low decimal values
|
|
|
|
# USDC has 6 decimals
|
2023-02-03 13:39:31 +00:00
|
|
|
usdc_token = make_token(symbol="USDC", decimals=6)
|
2019-05-16 10:50:23 +00:00
|
|
|
# when decimals < 10, should never display 'Wei' format
|
2023-02-03 13:39:31 +00:00
|
|
|
text = format_ethereum_amount(1, usdc_token, ETH)
|
|
|
|
self.assertEqual(text, "0.000001 USDC")
|
|
|
|
text = format_ethereum_amount(0, usdc_token, ETH)
|
|
|
|
self.assertEqual(text, "0 USDC")
|
2019-05-16 10:50:23 +00:00
|
|
|
|
2023-02-03 13:39:31 +00:00
|
|
|
# ICO has 10 decimals
|
|
|
|
ico_token = make_token(symbol="ICO", decimals=10)
|
|
|
|
text = format_ethereum_amount(1, ico_token, ETH)
|
|
|
|
self.assertEqual(text, "1 Wei ICO")
|
|
|
|
text = format_ethereum_amount(9, ico_token, ETH)
|
|
|
|
self.assertEqual(text, "9 Wei ICO")
|
|
|
|
text = format_ethereum_amount(10, ico_token, ETH)
|
|
|
|
self.assertEqual(text, "0.000000001 ICO")
|
|
|
|
text = format_ethereum_amount(11, ico_token, ETH)
|
|
|
|
self.assertEqual(text, "0.0000000011 ICO")
|
2019-05-16 10:50:23 +00:00
|
|
|
|
2021-08-31 09:47:56 +00:00
|
|
|
def test_unknown_token(self):
|
2023-02-03 13:39:31 +00:00
|
|
|
text = format_ethereum_amount(1, UNKNOWN_TOKEN, ETH)
|
|
|
|
self.assertEqual(text, "1 Wei UNKN")
|
|
|
|
text = format_ethereum_amount(0, UNKNOWN_TOKEN, ETH)
|
|
|
|
self.assertEqual(text, "0 Wei UNKN")
|
2021-08-31 09:47:56 +00:00
|
|
|
# unknown token has 0 decimals so is always wei
|
2023-02-03 13:39:31 +00:00
|
|
|
text = format_ethereum_amount(1000000000000000000, UNKNOWN_TOKEN, ETH)
|
|
|
|
self.assertEqual(text, "1,000,000,000,000,000,000 Wei UNKN")
|
2021-08-31 09:47:56 +00:00
|
|
|
|
2017-12-27 11:48:05 +00:00
|
|
|
|
2023-02-03 13:39:31 +00:00
|
|
|
if __name__ == "__main__":
|
2017-12-27 11:48:05 +00:00
|
|
|
unittest.main()
|