amt.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. """
  3. test.modtest_d.amt: CoinAmt unit tests for the MMGen suite
  4. """
  5. from decimal import Decimal
  6. from mmgen.protocol import init_proto
  7. from mmgen.tx.new import parse_fee_spec
  8. from mmgen.cfg import Config
  9. from mmgen.amt import TokenAmt
  10. from ..include.common import cfg, vmsg
  11. def get_protos(data):
  12. return {coin: init_proto(cfg, coin, need_amt=True) for coin in set(d[0] for d in data)}
  13. def test_to_unit(data):
  14. protos = get_protos(data)
  15. for proto, amt, unit, chk in data:
  16. amt = protos[proto].coin_amt(amt)
  17. res = amt.to_unit(unit)
  18. vmsg(f' {proto.upper()} {amt.fmt(8)} => {res:<14} {unit}')
  19. if '.' in chk:
  20. assert res == Decimal(chk), f'{res} != {Decimal(chk)}'
  21. else:
  22. assert res == int(chk), f'{res} != {int(chk)}'
  23. return True
  24. def test_fee_spec(data):
  25. protos = get_protos(data)
  26. for proto, spec, amt, unit in data:
  27. vmsg(f' {proto.upper():6} {spec:<5} => {amt:<4} {unit}')
  28. res = parse_fee_spec(protos[proto], spec)
  29. assert res.amt == amt, f' {res.amt} != {amt}'
  30. assert res.unit == unit, f' {res.unit} != {unit}'
  31. return True
  32. class unit_tests:
  33. altcoin_deps = ('fee_spec_alt', 'to_unit_alt', 'token_amt')
  34. def to_unit(self, name, ut, desc='CoinAmt.to_unit() (BTC)'):
  35. return test_to_unit((
  36. ('btc', '0.00000001', 'satoshi', '1'),
  37. ('btc', '1.23456789', 'satoshi', '123456789')))
  38. def to_unit_alt(self, name, ut, desc='CoinAmt.to_unit() (LTC, BCH, ETH, XMR)'):
  39. return test_to_unit((
  40. ('ltc', '0.00000001', 'satoshi', '1'),
  41. ('ltc', '1.23456789', 'satoshi', '123456789'),
  42. ('bch', '0.00000001', 'satoshi', '1'),
  43. ('bch', '1.23456789', 'satoshi', '123456789'),
  44. ('eth', '1.234567890123456789', 'wei', '1234567890123456789'),
  45. ('eth', '1.234567890123456789', 'Kwei', '1234567890123456.789'),
  46. ('eth', '1.234567890123456789', 'Mwei', '1234567890123.456789'),
  47. ('eth', '1.234567890123456789', 'finney', '1234.567890123456789'),
  48. ('eth', '0.000000012345678901', 'Mwei', '12345.678901'),
  49. ('eth', '0.000000000000000001', 'Kwei', '0.001'),
  50. ('eth', '0.000000000000000001', 'Gwei', '0.000000001'),
  51. ('eth', '0.00000001', 'Gwei', '10'),
  52. ('eth', '1', 'Gwei', '1000000000'),
  53. ('eth', '1', 'finney', '1000'),
  54. ('xmr', '1', 'atomic', '1000000000000'),
  55. ('xmr', '0.000000000001', 'atomic', '1'),
  56. ('xmr', '1.234567890123', 'atomic', '1234567890123')))
  57. def fee_spec(self, name, ut, desc='fee spec parsing (BTC)'):
  58. return test_fee_spec((
  59. ('btc', '32s', '32', 'satoshi'),
  60. ('btc', '1s', '1', 'satoshi')))
  61. def fee_spec_alt(self, name, ut, desc='fee spec parsing (LTC, BCH, ETH, XMR)'):
  62. return test_fee_spec((
  63. ('ltc', '3.07s', '3.07', 'satoshi'),
  64. ('bch', '3.07s', '3.07', 'satoshi'),
  65. ('eth', '3.07G', '3.07', 'Gwei'),
  66. ('eth', '37M', '37', 'Mwei'),
  67. ('eth', '3701w', '3701', 'wei'),
  68. ('eth', '3.07M', '3.07', 'Mwei'),
  69. ('xmr', '3.07a', '3.07', 'atomic')))
  70. def token_amt(self, name, ut, desc='TokenAmt (ETH)'):
  71. for n, dec, unit, chk in (
  72. (1234567, 6, 'atomic', '1.234567'),
  73. ('1.234567', 6, None, '1.234567'),
  74. (1234567, 22, 'atomic', '0.0000000000000001234567'),
  75. ('0.0000000000000001234567', 22, None, '0.0000000000000001234567'),
  76. ):
  77. amt = TokenAmt(n, decimals=dec, from_unit=unit)
  78. amt_disp = amt.hl(color=False)
  79. vmsg(' ' + amt_disp)
  80. if unit == 'atomic':
  81. assert amt.to_unit('atomic') == n
  82. assert amt_disp == chk, f'{amt_disp} != {chk}'
  83. return True