Beispiel #1
0
def getbalance(minconf=1):
	from mmgen.tx import connect_to_bitcoind,trim_exponent,is_mmgen_addr

	accts = {}
	for d in connect_to_bitcoind().listunspent(0):
		ma = split2(d.account)[0]
		keys = ["TOTAL"]
		if d.spendable: keys += ["SPENDABLE"]
		if is_mmgen_addr(ma): keys += [ma.split(":")[0]]
		confs = d.confirmations
		i = 2 if confs >= minconf else 1

		for key in keys:
			if key not in accts: accts[key] = [0,0,0]
			for j in ([0] if confs == 0 else []) + [i]:
				accts[key][j] += d.amount

	fs = "{:12}  {:<%s} {:<%s} {:<}" % (16,16)
	mc,lbl = str(minconf),"confirms"
	Msg(fs.format("Wallet","Unconfirmed","<%s %s"%(mc,lbl),">=%s %s"%(mc,lbl)))
	for key in sorted(accts.keys()):
		Msg(fs.format(key+":", *[str(trim_exponent(a))+" BTC"
				for a in accts[key]]))
Beispiel #2
0
def listaddresses(addrs='',minconf=1,showempty=False,pager=False,showbtcaddrs=False):

	from mmgen.tx import connect_to_bitcoind,trim_exponent,is_mmgen_addr,is_mmgen_seed_id

	usr_addr_list = []
	if addrs:
		sid,idxs = split2(addrs,':')
		if not idxs:
			s1 = "'%s': invalid address argument\n"
			s2 = "Address argument format: <%s Seed ID>':'<index or range>"
			die(1, (s1+s2) % (addrs,g.proj_name))
		if not is_mmgen_seed_id(sid):
			die(1,'%s: invalid %s Seed ID' % (g.proj_name,sid))
		tmp = parse_addr_idxs(idxs)
		if not tmp: return False
		usr_addr_list = ['%s:%s' % (sid,i) for i in tmp]

	c = connect_to_bitcoind()

	addrs = {} # reusing variable name!
	from decimal import Decimal
	total = Decimal('0')
	for d in c.listunspent(0):
		mmaddr,comment = split2(d.account)
		if usr_addr_list and (mmaddr not in usr_addr_list): continue
		if is_mmgen_addr(mmaddr) and d.confirmations >= minconf:
			key = mmaddr.replace(':','_')
			if key in addrs:
				if addrs[key][2] != d.address:
					die(2,'duplicate BTC address ({}) for this MMGen address! ({})'.format(
							(d.address, addrs[key][2])))
			else:
				addrs[key] = [0,comment,d.address]
			addrs[key][0] += d.amount
			total += d.amount

	# We use listaccounts only for empty addresses, as it shows false positive balances
	if showempty:
		accts = c.listaccounts(minconf=0,includeWatchonly=True,as_dict=True)
		for a in accts:
			mmaddr,comment = split2(a)
			if usr_addr_list and (mmaddr not in usr_addr_list): continue
			if is_mmgen_addr(mmaddr):
				key = mmaddr.replace(':','_')
				if key not in addrs:
					if showbtcaddrs:
						tmp = c.getaddressesbyaccount(a)
						if len(tmp) != 1:
							die(2,"Account '%s' has more or less than one BTC address!" % a)
						baddr = tmp[0]
					else:
						baddr = ''
					addrs[key] = [0,comment,baddr]

	if not addrs:
		die(1,('No addresses with balances!','No tracked addresses!')[showempty])

	fs = '%-{}s %-{}s %-{}s %s'.format(
		max(len(k) for k in addrs),
		(0,36)[showbtcaddrs],
		max(len(addrs[k][1]) for k in addrs) + 1
	)

	def s_mmgen(key):
		return '{}:{:>0{w}}'.format(w=g.mmgen_idx_max_digits, *key.split('_'))

	out = []
	for k in sorted(addrs,key=s_mmgen):
		if out and k.split('_')[0] != out[-1].split(':')[0]: out.append('')
		baddr = ' ' + addrs[k][2] if showbtcaddrs else ''
		out.append(fs % (k.replace('_',':'), baddr, addrs[k][1], trim_exponent(addrs[k][0])))

	o = (fs + '\n%s\nTOTAL: %s BTC') % (
			'ADDRESS','','COMMENT','BALANCE', '\n'.join(out), trim_exponent(total)
		)
	if pager: do_pager(o)
	else: Msg(o)