obj.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  5. # Copyright (C)2013-2018 The MMGen Project <mmgen@tuta.io>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. """
  20. altcoins.eth.obj: Ethereum data type classes for the MMGen suite
  21. """
  22. # Kwei (babbage) 3, Mwei (lovelace) 6, Gwei (shannon) 9, µETH (szabo) 12, mETH (finney) 15, ETH 18
  23. from decimal import Decimal
  24. from mmgen.color import *
  25. from mmgen.obj import *
  26. class ETHAmt(BTCAmt):
  27. max_prec = 18
  28. max_amt = None
  29. wei = Decimal('0.000000000000000001')
  30. Kwei = Decimal('0.000000000000001')
  31. Mwei = Decimal('0.000000000001')
  32. Gwei = Decimal('0.000000001')
  33. szabo = Decimal('0.000001')
  34. finney = Decimal('0.001')
  35. min_coin_unit = wei
  36. units = ('wei','Kwei','Mwei','Gwei','szabo','finney')
  37. amt_fs = '4.18'
  38. def toWei(self): return int(Decimal(self) / self.wei)
  39. def toKwei(self): return int(Decimal(self) / self.Kwei)
  40. def toMwei(self): return int(Decimal(self) / self.Mwei)
  41. def toGwei(self): return int(Decimal(self) / self.Gwei)
  42. def toSzabo(self): return int(Decimal(self) / self.szabo)
  43. def toFinney(self): return int(Decimal(self) / self.finney)
  44. class ETHNonce(int,Hilite,InitErrors): # WIP
  45. def __new__(cls,n,on_fail='die'):
  46. if type(n) == cls: return n
  47. cls.arg_chk(cls,on_fail)
  48. from mmgen.util import is_int
  49. try:
  50. assert is_int(n),"'{}': value is not an integer".format(n)
  51. me = int.__new__(cls,n)
  52. return me
  53. except Exception as e:
  54. m = "{!r}: value cannot be converted to ETH nonce ({})"
  55. return cls.init_fail(m.format(n,e[0]),on_fail)
  56. @classmethod
  57. def colorize(cls,s,color=True):
  58. k = color if type(color) is str else cls.color # hack: override color with str value
  59. return globals()[k](str(s)) if (color or cls.color_always) else str(s)