Browse Source

mmgen-tool getbalance: implement using new class + subclass for ETH

MMGen 6 years ago
parent
commit
8683257b54
3 changed files with 75 additions and 34 deletions
  1. 18 0
      mmgen/altcoins/eth/tw.py
  2. 5 34
      mmgen/tool.py
  3. 52 0
      mmgen/tw.py

+ 18 - 0
mmgen/altcoins/eth/tw.py

@@ -170,6 +170,24 @@ class EthereumTwAddrList(TwAddrList):
 			self[label.mmid]['amt'] += bal
 			self.total += bal
 
+from mmgen.tw import TwGetBalance
+class EthereumTwGetBalance(TwGetBalance):
+
+	fs = '{w:13} {c}\n' # TODO - for now, just suppress display of meaningless data
+
+	def create_data(self):
+		data = EthereumTrackingWallet().mmid_ordered_dict()
+		for d in data:
+			keys = ['TOTAL']
+			keys += [str(d.obj.sid)] if d.type == 'mmgen' else ['Non-MMGen']
+			confs = 9999 # TODO
+			i = (1,2)[confs >= self.minconf]
+
+			for key in keys:
+				if key not in self.data: self.data[key] = [g.proto.coin_amt('0')] * 3
+				for j in ([],[0])[confs==0] + [i]:
+					self.data[key][j] += ETHAmt(int(g.rpch.eth_getBalance('0x'+data[d]['addr']),16),'wei')
+
 class EthereumAddrData(AddrData):
 
 	@classmethod

+ 5 - 34
mmgen/tool.py

@@ -79,7 +79,7 @@ cmd_data = OrderedDict([
 
 	('Listaddress',['<{} address> [str]'.format(pnm),'minconf [int=1]','pager [bool=False]','showempty [bool=True]','showbtcaddr [bool=True]','show_age [bool=False]','show_days [bool=True]']),
 	('Listaddresses',["addrs [str='']",'minconf [int=1]','showempty [bool=False]','pager [bool=False]','showbtcaddrs [bool=True]','all_labels [bool=False]',"sort [str=''] (options: reverse, age)",'show_age [bool=False]','show_days [bool=True]']),
-	('Getbalance',   ['minconf [int=1]','quiet [bool=False]']),
+	('Getbalance',   ['minconf [int=1]','quiet [bool=False]','pager [bool=False]']),
 	('Txview',       ['<{} TX file(s)> [str]'.format(pnm),'pager [bool=False]','terse [bool=False]',"sort [str='mtime'] (options: ctime, atime)",'MARGS']),
 	('Twview',       ["sort [str='age']",'reverse [bool=False]','show_days [bool=True]','show_mmid [bool=True]','minconf [int=1]','wide [bool=False]','pager [bool=False]']),
 
@@ -665,39 +665,10 @@ def Listaddresses(addrs='',minconf=1,
 	o = al.format(showbtcaddrs,sort,show_age,show_days)
 	return do_pager(o) if pager else Msg(o)
 
-def Getbalance(minconf=1,quiet=False,return_val=False):
-	rpc_init()
-	accts = {}
-	for d in g.rpch.listunspent(0):
-		ma = split2(d['account'] if 'account' in d else '')[0] # include coinbase outputs if spendable
-		keys = ['TOTAL']
-		if d['spendable']: keys += ['SPENDABLE']
-		if is_mmgen_id(ma): keys += [ma.split(':')[0]]
-		confs = d['confirmations']
-		i = (1,2)[confs >= minconf]
-
-		for key in keys:
-			if key not in accts: accts[key] = [g.proto.coin_amt('0')] * 3
-			for j in ([],[0])[confs==0] + [i]:
-				accts[key][j] += d['amount']
-
-	if quiet:
-		o = ['{}'.format(accts['TOTAL'][2] if accts else g.proto.coin_amt('0'))]
-	else:
-		fs = '{:13} {} {} {}'
-		mc,lbl = str(minconf),'confirms'
-		o = [fs.format(
-				'Wallet',
-				*[s.ljust(16) for s in ' Unconfirmed',' <{} {}'.format(mc,lbl),' >={} {}'.format(mc,lbl)])]
-		for key in sorted(accts.keys()):
-			o += [fs.format(key+':', *[a.fmt(color=True,suf=' '+g.coin) for a in accts[key]])]
-
-	if 'SPENDABLE' in accts:
-		Msg(red('Warning: this wallet contains PRIVATE KEYS for the SPENDABLE balance!'))
-
-	o = '\n'.join(o)
-	if return_val: return o
-	else:          Msg(o)
+def Getbalance(minconf=1,quiet=False,return_val=False,pager=False):
+	from mmgen.tw import TwGetBalance
+	o = TwGetBalance(minconf,quiet).format()
+	return o if return_val else do_pager(o) if pager else Msg_r(o)
 
 def Txview(*infiles,**kwargs):
 	from mmgen.filename import MMGenFileList

+ 52 - 0
mmgen/tw.py

@@ -540,3 +540,55 @@ class TrackingWallet(MMGenObject):
 			return True
 
 	def remove_label(self,mmaddr): self.add_label(mmaddr,'')
+
+class TwGetBalance(MMGenObject):
+
+	fs = '{w:13} {u:<16} {p:<16} {c}\n'
+
+	def __new__(cls,*args,**kwargs):
+		if g.coin == 'ETH':
+			from mmgen.altcoins.eth.tw import EthereumTwGetBalance
+			cls = EthereumTwGetBalance
+		return MMGenObject.__new__(cls,*args,**kwargs)
+
+	def __init__(self,minconf,quiet):
+
+		rpc_init()
+		self.minconf = minconf
+		self.quiet = quiet
+		self.data = {}
+		self.create_data()
+
+	def create_data(self):
+		for d in g.rpch.listunspent(0):
+			try:    lbl = TwLabel(d['account'],on_fail='silent')
+			except: lbl = None
+			keys = ['TOTAL']
+			if lbl and lbl.mmid.type == 'mmgen':
+				keys += [lbl.mmid.obj.sid]
+			if d['spendable']: keys += ['SPENDABLE']
+			confs = d['confirmations']
+			i = (1,2)[confs >= self.minconf]
+
+			for key in keys:
+				if key not in self.data: self.data[key] = [g.proto.coin_amt('0')] * 3
+				for j in ([],[0])[confs==0] + [i]:
+					self.data[key][j] += d['amount']
+
+	def format(self):
+		if self.quiet:
+			o = str(self.data['TOTAL'][2] if self.data else 0) + '\n'
+		else:
+			o = self.fs.format( w='Wallet',
+								u=' Unconfirmed',
+								p=' <{} confirms'.format(self.minconf),
+								c=' >={} confirms'.format(self.minconf))
+			for key in sorted(self.data):
+				o += self.fs.format(**dict(zip(
+							('w','u','p','c'),
+							[key+':'] + [a.fmt(color=True,suf=' '+g.coin) for a in self.data[key]]
+							)))
+
+		if 'SPENDABLE' in self.data:
+			o += red('Warning: this wallet contains PRIVATE KEYS for the SPENDABLE balance!\n')
+		return o