def __init__(self,mode='r'): TrackingWallet.__init__(self,mode=mode) check_or_create_dir(self.data_dir) try: self.orig_data = get_data_from_file(self.tw_file,quiet=True) self.data = json.loads(self.orig_data) except: try: os.stat(self.tw_file) except: self.orig_data = '' self.data = {'coin':g.coin,'accounts':{},'tokens':{}} else: die(2,"File '{}' exists but does not contain valid json data".format(self.tw_file)) else: self.upgrade_wallet_maybe() m = 'Tracking wallet coin ({}) does not match current coin ({})!' assert self.data['coin'] == g.coin,m.format(self.data['coin'],g.coin) if not 'tokens' in self.data: self.data['tokens'] = {} def conv_types(ad): for v in ad.values(): v['mmid'] = TwMMGenID(v['mmid'],on_fail='raise') v['comment'] = TwComment(v['comment'],on_fail='raise') conv_types(self.data['accounts']) for v in self.data['tokens'].values(): conv_types(v)
def __init__(self, mode='r'): TrackingWallet.__init__(self, mode=mode) check_or_create_dir(self.data_dir) try: self.orig_data = get_data_from_file(self.tw_file, quiet=True) self.data = json.loads(self.orig_data) except: try: os.stat(self.tw_file) except: self.orig_data = '' self.data = {'coin': g.coin, 'accounts': {}, 'tokens': {}} else: die( 2, "File '{}' exists but does not contain valid json data". format(self.tw_file)) else: self.upgrade_wallet_maybe() m = 'Tracking wallet coin ({}) does not match current coin ({})!' assert self.data['coin'] == g.coin, m.format( self.data['coin'], g.coin) if not 'tokens' in self.data: self.data['tokens'] = {} def conv_types(ad): for v in ad.values(): v['mmid'] = TwMMGenID(v['mmid'], on_fail='raise') v['comment'] = TwComment(v['comment'], on_fail='raise') conv_types(self.data['accounts']) for v in self.data['tokens'].values(): conv_types(v)
def remove_address(self,mmgen_or_coin_addr:str): "remove an address from tracking wallet" from mmgen.tw import TrackingWallet tw = TrackingWallet(mode='w') ret = tw.remove_address(mmgen_or_coin_addr) # returns None on failure if ret: msg("Address '{}' deleted from tracking wallet".format(ret)) return ret
def __init__(self, mode='r', no_rpc=False): TrackingWallet.__init__(self, mode=mode) for v in self.data['tokens'].values(): self.conv_types(v) if g.token and not is_coin_addr(g.token): ret = self.sym2addr(g.token, no_rpc=no_rpc) if ret: g.token = ret
def __init__(self, usr_addr_list, minconf, showempty, showbtcaddrs, all_labels, wallet=None): self.wallet = wallet or TrackingWallet(mode='w') tw_dict = self.wallet.mmid_ordered_dict self.total = g.proto.coin_amt('0') from mmgen.obj import CoinAddr for mmid, d in list(tw_dict.items()): # if d['confirmations'] < minconf: continue # cannot get confirmations for eth account label = TwLabel(mmid + ' ' + d['comment'], on_fail='raise') if usr_addr_list and (label.mmid not in usr_addr_list): continue bal = self.wallet.get_balance(d['addr']) if bal == 0 and not showempty: if not label.comment: continue if not all_labels: continue self[label.mmid] = {'amt': g.proto.coin_amt('0'), 'lbl': label} if showbtcaddrs: self[label.mmid]['addr'] = CoinAddr(d['addr']) self[label.mmid]['lbl'].mmid.confs = None self[label.mmid]['amt'] += bal self.total += bal
def __init__(self,*args,**kwargs): if not kwargs.get('offline'): from mmgen.tw import TrackingWallet self.decimals = TrackingWallet().get_param('decimals') from .contract import Token self.token_obj = Token(g.token,self.decimals) EthereumMMGenTX.__init__(self,*args,**kwargs)
def get_unspent_rpc(self): rpc_init() return map(lambda d: { 'account': TwLabel(d['mmid']+' '+d['comment'],on_fail='raise'), 'address': d['addr'], 'amount': self.get_addr_bal(d['addr']), 'confirmations': 0, # TODO }, TrackingWallet().sorted_list())
def add_label(self, mmgen_or_coin_addr: str, label: str): "add descriptive label for address in tracking wallet" rpc_init() from mmgen.tw import TrackingWallet TrackingWallet(mode='w').add_label(mmgen_or_coin_addr, label, on_fail='raise') return True
def __init__(self,usr_addr_list,minconf,showempty,showbtcaddrs,all_labels): rpc_init() if g.token: self.token = Token(g.token) tw = TrackingWallet().mmid_ordered_dict() self.total = g.proto.coin_amt('0') from mmgen.obj import CoinAddr for mmid,d in list(tw.items()): # if d['confirmations'] < minconf: continue # cannot get confirmations for eth account label = TwLabel(mmid+' '+d['comment'],on_fail='raise') if usr_addr_list and (label.mmid not in usr_addr_list): continue bal = self.get_addr_balance(d['addr']) if bal == 0 and not showempty: if not label.comment: continue if not all_labels: continue self[label.mmid] = {'amt': g.proto.coin_amt('0'), 'lbl': label } if showbtcaddrs: self[label.mmid]['addr'] = CoinAddr(d['addr']) self[label.mmid]['lbl'].mmid.confs = None self[label.mmid]['amt'] += bal self.total += bal
def create_data(self): data = TrackingWallet().mmid_ordered_dict() for d in data: if d.type == 'mmgen': key = d.obj.sid if key not in self.data: self.data[key] = [g.proto.coin_amt('0')] * 4 else: key = 'Non-MMGen' conf_level = 2 # TODO amt = self.get_addr_balance(data[d]['addr']) self.data['TOTAL'][conf_level] += amt self.data[key][conf_level] += amt
def resolve_token_arg(token_arg): from mmgen.tw import TrackingWallet from mmgen.obj import CoinAddr from mmgen.altcoins.eth.contract import Token try: addr = CoinAddr(token_arg,on_fail='raise') except: addr = TrackingWallet().sym2addr(token_arg) else: Token(addr) # test for presence in blockchain if not addr: m = "'{}': unrecognized token symbol" raise UnrecognizedTokenSymbol(m.format(token_arg)) sym = Token(addr).symbol().upper() vmsg('ERC20 token resolved: {} ({})'.format(addr,sym)) return addr,sym
def get_cmdline_input_addrs(self): ret = [] if opt.inputs: from mmgen.tw import TrackingWallet r = TrackingWallet().data_root # must create new instance here m = 'Address {!r} not in tracking wallet' for i in opt.inputs.split(','): if is_mmgen_id(i): for addr in r: if r[addr]['mmid'] == i: ret.append(addr) break else: raise UserAddressNotInWallet(m.format(i)) elif is_coin_addr(i): if not i in r: raise UserAddressNotInWallet(m.format(i)) ret.append(i) else: die(1,"'{}': not an MMGen ID or coin address".format(i)) return ret
class EthereumTwGetBalance(TwGetBalance): fs = '{w:13} {c}\n' # TODO - for now, just suppress display of meaningless data def __init__(self, *args, **kwargs): self.wallet = TrackingWallet(mode='w') TwGetBalance.__init__(self, *args, **kwargs) def create_data(self): data = self.wallet.mmid_ordered_dict for d in data: if d.type == 'mmgen': key = d.obj.sid if key not in self.data: self.data[key] = [g.proto.coin_amt('0')] * 4 else: key = 'Non-MMGen' conf_level = 2 # TODO amt = self.wallet.get_balance(data[d]['addr']) self.data['TOTAL'][conf_level] += amt self.data[key][conf_level] += amt
trim_comments=True)) else: al = import_mmgen_list(infile) elif len(cmd_args) == 0 and opt.address: al = AddrList(addrlist=[opt.address]) infile = 'command line' else: die(1,ai_msgs('bad_args')) m = ' from Seed ID {}'.format(al.al_id.sid) if hasattr(al.al_id,'sid') else '' qmsg('OK. {} addresses{}'.format(al.num_addrs,m)) err_msg = None from mmgen.tw import TrackingWallet tw = TrackingWallet(mode='w') if opt.rescan and not 'rescan' in tw.caps: msg("'--rescan' ignored: not supported by {}".format(type(tw).__name__)) opt.rescan = False if opt.rescan and not opt.quiet: confirm_or_raise(ai_msgs('rescan'),'continue',expect='YES') if opt.batch and not 'batch' in tw.caps: msg("'--batch' ignored: not supported by {}".format(type(tw).__name__)) opt.batch = False def import_address(addr,label,rescan): try: tw.import_address(addr,label,rescan) except Exception as e:
def __init__(self, *args, **kwargs): self.wallet = TrackingWallet(mode='w') TwGetBalance.__init__(self, *args, **kwargs)
trim_comments=True)) else: al = import_mmgen_list(infile) elif len(cmd_args) == 0 and opt.address: al = AddrList(addrlist=[opt.address]) infile = 'command line' else: die(1, ai_msgs('bad_args')) m = ' from Seed ID {}'.format(al.al_id.sid) if hasattr(al.al_id, 'sid') else '' qmsg('OK. {} addresses{}'.format(al.num_addrs, m)) err_msg = None from mmgen.tw import TrackingWallet tw = TrackingWallet(mode='w') if opt.rescan and not 'rescan' in tw.caps: msg("'--rescan' ignored: not supported by {}".format(type(tw).__name__)) opt.rescan = False if opt.rescan and not opt.quiet: confirm_or_raise(ai_msgs('rescan'), 'continue', expect='YES') if opt.batch and not 'batch' in tw.caps: msg("'--batch' ignored: not supported by {}".format(type(tw).__name__)) opt.batch = False def import_address(addr, label, rescan): try:
def get_tw_data(cls): vmsg('Getting address data from tracking wallet') tw = TrackingWallet().mmid_ordered_dict() # emulate the output of RPC 'listaccounts' and 'getaddressesbyaccount' return [(mmid+' '+d['comment'],[d['addr']]) for mmid,d in list(tw.items())]
def Add_label(mmaddr_or_coin_addr,label): rpc_init() from mmgen.tw import TrackingWallet TrackingWallet(mode='w').add_label(mmaddr_or_coin_addr,label,on_fail='raise')
def Remove_address(mmaddr_or_coin_addr): from mmgen.tw import TrackingWallet tw = TrackingWallet(mode='w') ret = tw.remove_address(mmaddr_or_coin_addr) if ret: msg("Address '{}' deleted from tracking wallet".format(ret))
def get_tw_data(cls, wallet=None): vmsg('Getting address data from tracking wallet') tw = (wallet or TrackingWallet()).mmid_ordered_dict # emulate the output of RPC 'listaccounts' and 'getaddressesbyaccount' return [(mmid + ' ' + d['comment'], [d['addr']]) for mmid, d in list(tw.items())]
trim_comments=True)) else: al = import_mmgen_list(infile) elif len(cmd_args) == 0 and opt.address: al = AddrList(addrlist=[opt.address]) infile = 'command line' else: die(1, ai_msgs('bad_args')) m = ' from Seed ID {}'.format(al.al_id.sid) if hasattr(al.al_id, 'sid') else '' qmsg('OK. {} addresses{}'.format(al.num_addrs, m)) err_msg = None from mmgen.tw import TrackingWallet tw = TrackingWallet(mode='w') if g.token: if not is_coin_addr(g.token): m = "When importing addresses for a new token, the token must be specified by address, not symbol." raise InvalidTokenAddress('{!r}: invalid token address\n{}'.format(m)) sym = tw.addr2sym( g.token ) # check for presence in wallet or blockchain; raises exception on failure if opt.rescan and not 'rescan' in tw.caps: msg("'--rescan' ignored: not supported by {}".format(type(tw).__name__)) opt.rescan = False if opt.rescan and not opt.quiet: confirm_or_raise(ai_msgs('rescan'), 'continue', expect='YES')