Browse Source

eth/tw.py,obj.py: cleanups, support wei and szabo in ETHAmt()

MMGen 6 years ago
parent
commit
5895221b0b
2 changed files with 20 additions and 22 deletions
  1. 6 17
      mmgen/altcoins/eth/tw.py
  2. 14 5
      mmgen/obj.py

+ 6 - 17
mmgen/altcoins/eth/tw.py

@@ -103,8 +103,9 @@ class EthereumTrackingWallet(TrackingWallet):
 # Use consistent naming, even though Ethereum doesn't have unspent outputs
 class EthereumTwUnspentOutputs(TwUnspentOutputs):
 
-	show_tx = False
+	show_txid = False
 	can_group = False
+	hdr_fmt = 'TRACKED ACCOUNTS (sort order: {})\nTotal {}: {}'
 	prompt = """
 Sort options: [a]mount, a[d]dress, [A]ge, [r]everse, [M]mgen addr
 Display options: show [D]ays, show [m]mgen addr, r[e]draw screen
@@ -114,26 +115,14 @@ Display options: show [D]ays, show [m]mgen addr, r[e]draw screen
 		if key == 'txid': return
 		super(type(self),self).do_sort(key=key,reverse=reverse)
 
-	class MMGenTwUnspentOutput(MMGenListItem):
-	#	attrs = 'txid','vout','amt','label','twmmid','addr','confs','days','skip'
-		txid   = MMGenImmutableAttr('txid',str,typeconv=False)
-		vout   = MMGenImmutableAttr('vout',str,typeconv=False)
-		amt    = MMGenImmutableAttr('amt',g.proto.coin_amt.__name__)
-		label  = MMGenListItemAttr('label','TwComment',reassign_ok=True)
-		twmmid = MMGenImmutableAttr('twmmid','TwMMGenID')
-		addr   = MMGenImmutableAttr('addr','CoinAddr')
-		confs  = MMGenImmutableAttr('confs',int,typeconv=False)
-		days   = MMGenListItemAttr('days',int,typeconv=False)
-		skip   = MMGenListItemAttr('skip',str,typeconv=False,reassign_ok=True)
-
 	def get_unspent_rpc(self):
 		rpc_init()
 		return map(lambda d: {
-				'txid': 'N/A',
-				'vout': '',
+				'txid': '0'*64, # bogus value, not displayed
+				'vout': 0,      # ""
 				'account': TwLabel(d['mmid']+' '+d['comment'],on_fail='raise'),
 				'address': d['addr'],
-				'amount': ETHAmt(int(g.rpch.eth_getBalance('0x'+d['addr']),16),fromWei=True),
+				'amount': ETHAmt(int(g.rpch.eth_getBalance('0x'+d['addr']),16),'wei'),
 				'confirmations': 0, # TODO
 				}, EthereumTrackingWallet().sorted_list())
 
@@ -150,7 +139,7 @@ class EthereumTwAddrList(TwAddrList):
 #			if d['confirmations'] < minconf: continue
 			label = TwLabel(mmid+' '+d['comment'],on_fail='raise')
 			if usr_addr_list and (label.mmid not in usr_addr_list): continue
-			bal = ETHAmt(int(g.rpch.eth_getBalance('0x'+d['addr']),16),fromWei=True)
+			bal = ETHAmt(int(g.rpch.eth_getBalance('0x'+d['addr']),16),'wei')
 			if bal == 0 and not showempty:
 				if not label.comment: continue
 				if not all_labels: continue

+ 14 - 5
mmgen/obj.py

@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -*- coding: UTF-8 -*-
 #
 # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
 # Copyright (C)2013-2018 The MMGen Project <mmgen@tuta.io>
@@ -374,26 +375,34 @@ class BTCAmt(Decimal,Hilite,InitErrors):
 class BCHAmt(BTCAmt): pass
 class B2XAmt(BTCAmt): pass
 class LTCAmt(BTCAmt): max_amt = 84000000
+
+# Kwei (babbage) 3, Mwei (lovelace) 6, Gwei (shannon) 9, µETH (szabo) 12, mETH (finney) 15, ETH 18
 class ETHAmt(BTCAmt):
 	max_prec = 18
 	max_amt = 999999999 # TODO
-	min_coin_unit = Decimal('0.000000000000000001') # wei
+	wei    = Decimal('0.000000000000000001')
+	szabo  = Decimal('0.000000000001')
+	min_coin_unit = wei
 	amt_fs = '4.18'
 
-	def __new__(cls,num,on_fail='die',fromWei=False):
+	def __new__(cls,num,from_unit=None,on_fail='die'):
 		if type(num) == cls: return num
 		cls.arg_chk(cls,on_fail)
 		try:
-			if fromWei:
+			if from_unit:
+				assert from_unit in ('wei','szabo'),"'{}': unrecognized ETH denomination".format(from_unit)
 				assert type(num) in (int,long),'value is not an integer or long integer'
-				return super(cls,cls).__new__(cls,num * cls.min_coin_unit)
+				return super(cls,cls).__new__(cls,num * getattr(cls,from_unit))
 			return super(cls,cls).__new__(cls,num)
 		except Exception as e:
 			m = "{!r}: value cannot be converted to {} ({})"
 			return cls.init_fail(m.format(num,cls.__name__,e[0]),on_fail)
 
 	def toWei(self):
-		return int(Decimal(self) / self.min_coin_unit)
+		return int(Decimal(self) / self.wei)
+
+	def toSzabo(self):
+		return int(Decimal(self) / self.szabo)
 
 class CoinAddr(str,Hilite,InitErrors,MMGenObject):
 	color = 'cyan'