2018-03-29 12:16:58 +00:00
|
|
|
from common import *
|
|
|
|
|
2019-08-26 16:47:49 +00:00
|
|
|
if not utils.BITCOIN_ONLY:
|
2021-03-23 12:35:27 +00:00
|
|
|
from trezor.messages import NEMMosaic
|
2019-08-26 16:47:49 +00:00
|
|
|
from apps.nem.mosaic.helpers import get_mosaic_definition
|
|
|
|
from apps.nem.transfer import *
|
|
|
|
from apps.nem.transfer.serialize import *
|
2022-09-17 16:27:15 +00:00
|
|
|
from apps.nem.transfer.serialize import _merge_mosaics
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2019-08-26 16:47:49 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
def get_mosaic(namespace: str, quantity: int, mosaic: str) -> NEMMosaic:
|
|
|
|
return NEMMosaic(
|
|
|
|
namespace=namespace,
|
|
|
|
quantity=quantity,
|
|
|
|
mosaic=mosaic,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-09-17 16:27:15 +00:00
|
|
|
# NOTE: copy-pasted from apps.nem.transfer.serialize.py
|
|
|
|
def sort_mosaics(mosaics: list[NEMMosaic]) -> list[NEMMosaic]:
|
|
|
|
return sorted(mosaics, key=lambda m: (m.namespace, m.mosaic))
|
|
|
|
|
|
|
|
|
2019-08-26 16:47:49 +00:00
|
|
|
@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
|
2018-04-24 16:17:40 +00:00
|
|
|
class TestNemMosaic(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_get_mosaic_definition(self):
|
|
|
|
m = get_mosaic_definition("nem", "xem", 104)
|
2021-12-08 09:10:58 +00:00
|
|
|
self.assertEqual(m.name, "NEM")
|
|
|
|
self.assertEqual(m.ticker, " XEM")
|
2018-04-24 16:17:40 +00:00
|
|
|
|
|
|
|
m = get_mosaic_definition("nem", "xxx", 104)
|
|
|
|
self.assertEqual(m, None)
|
|
|
|
|
|
|
|
m = get_mosaic_definition("aaaa", "xxx", 104)
|
|
|
|
self.assertEqual(m, None)
|
|
|
|
|
|
|
|
m = get_mosaic_definition("pacnem", "cheese", 104)
|
2021-12-08 09:10:58 +00:00
|
|
|
self.assertEqual(m.name, "PacNEM Score Tokens")
|
|
|
|
self.assertEqual(m.ticker, " PAC:CHS")
|
|
|
|
self.assertEqual(m.levy.fee, 100)
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2018-04-06 08:40:46 +00:00
|
|
|
def test_mosaic_canonicalization(self):
|
2021-12-08 09:10:58 +00:00
|
|
|
a = get_mosaic("abc", 3, "mosaic")
|
|
|
|
b = get_mosaic("abc", 4, "a")
|
|
|
|
c = get_mosaic("zzz", 3, "mosaic")
|
|
|
|
d = get_mosaic("abc", 8, "mosaic")
|
|
|
|
e = get_mosaic("aaa", 1, "mosaic")
|
|
|
|
f = get_mosaic("aaa", 1, "mosaicz")
|
|
|
|
g = get_mosaic("zzz", 30, "mosaic")
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2018-04-06 08:40:46 +00:00
|
|
|
res = canonicalize_mosaics([a, b, c, d, e, f, g])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(res, [e, f, b, a, c])
|
|
|
|
self.assertEqual(res[2].quantity, b.quantity)
|
|
|
|
self.assertEqual(res[3].quantity, 3 + 8) # a + d
|
|
|
|
self.assertEqual(res[4].quantity, 3 + 30) # c + g
|
|
|
|
|
2018-04-06 08:40:46 +00:00
|
|
|
def test_mosaic_merge(self):
|
2021-12-08 09:10:58 +00:00
|
|
|
a = get_mosaic("abc", 1, "mosaic")
|
|
|
|
b = get_mosaic("abc", 1, "mosaic")
|
|
|
|
c = get_mosaic("abc", 2, "xxx")
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2022-09-17 16:27:15 +00:00
|
|
|
merged = _merge_mosaics([a, b])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(merged[0].quantity, 2)
|
|
|
|
self.assertEqual(len(merged), 1)
|
|
|
|
|
|
|
|
a.quantity = 1
|
|
|
|
b.quantity = 10
|
2022-09-17 16:27:15 +00:00
|
|
|
merged = _merge_mosaics([a, b])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(merged[0].quantity, 11)
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.namespace = "abcdef"
|
2022-09-17 16:27:15 +00:00
|
|
|
merged = _merge_mosaics([a, b])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(len(merged), 2)
|
|
|
|
|
2022-09-17 16:27:15 +00:00
|
|
|
merged = _merge_mosaics([a, b, c])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(len(merged), 3)
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.namespace = "abcdef"
|
2018-03-29 12:16:58 +00:00
|
|
|
a.quantity = 1
|
2021-12-08 09:10:58 +00:00
|
|
|
a.mosaic = "mosaic"
|
|
|
|
b.namespace = "abc"
|
2018-03-29 12:16:58 +00:00
|
|
|
b.quantity = 2
|
2021-12-08 09:10:58 +00:00
|
|
|
b.mosaic = "mosaic"
|
|
|
|
c.namespace = "abc"
|
|
|
|
c.mosaic = "mosaic"
|
2018-03-29 12:16:58 +00:00
|
|
|
c.quantity = 3
|
2022-09-17 16:27:15 +00:00
|
|
|
merged = _merge_mosaics([a, b, c])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(merged[0].quantity, 1)
|
|
|
|
self.assertEqual(merged[1].quantity, 5)
|
|
|
|
self.assertEqual(len(merged), 2)
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.namespace = "abc"
|
2018-03-29 12:16:58 +00:00
|
|
|
a.quantity = 1
|
2021-12-08 09:10:58 +00:00
|
|
|
a.mosaic = "mosaic"
|
|
|
|
b.namespace = "abc"
|
2018-03-29 12:16:58 +00:00
|
|
|
b.quantity = 2
|
2021-12-08 09:10:58 +00:00
|
|
|
b.mosaic = "mosaic"
|
|
|
|
c.namespace = "abc"
|
|
|
|
c.mosaic = "mosaic"
|
2018-03-29 12:16:58 +00:00
|
|
|
c.quantity = 3
|
2022-09-17 16:27:15 +00:00
|
|
|
merged = _merge_mosaics([a, b, c])
|
2018-03-29 12:16:58 +00:00
|
|
|
self.assertEqual(merged[0].quantity, 6)
|
|
|
|
self.assertEqual(len(merged), 1)
|
|
|
|
|
2018-04-06 08:40:46 +00:00
|
|
|
def test_mosaic_sort(self):
|
2021-12-08 09:10:58 +00:00
|
|
|
a = get_mosaic("abcz", 1, "mosaic")
|
|
|
|
b = get_mosaic("abca", 1, "mosaic")
|
|
|
|
c = get_mosaic("a", 0, "zzz")
|
|
|
|
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [b, a])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.namespace = ""
|
|
|
|
b.namespace = "a.b.c"
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [a, b])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.namespace = "z.z.z"
|
|
|
|
b.namespace = "a.b.c"
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [b, a])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.namespace = "a"
|
|
|
|
b.namespace = "a"
|
|
|
|
a.mosaic = "mosaic"
|
|
|
|
b.mosaic = "mosaic"
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [a, b])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
a.mosaic = "www"
|
|
|
|
b.mosaic = "aaa"
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [b, a])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b, c])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [b, a, c])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
c.mosaic = "bbb"
|
2018-04-06 08:40:46 +00:00
|
|
|
res = sort_mosaics([a, b, c])
|
2021-06-01 11:48:50 +00:00
|
|
|
self.assertListEqual(res, [b, c, a])
|
2018-03-29 12:16:58 +00:00
|
|
|
|
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if __name__ == "__main__":
|
2018-03-29 12:16:58 +00:00
|
|
|
unittest.main()
|