def get_balances(services, client): """ Gets wallet balances of given services. """ buffer_balance = get_buffer_balance(client) with open(Two1Composer.COMPOSE_FILE, "r") as f: services_info = yaml.load(f) balances = {} for service in services: template = { "buffer": buffer_balance, "wallet": None, "channels": None, } try: service_mnemonic = services_info.get('services').get(service).get( 'environment').get('TWO1_WALLET_MNEMONIC') service_wallet = Two1Wallet.import_from_mnemonic(mnemonic=service_mnemonic) template["wallet"] = service_wallet.balances["total"] channel_client = channels.PaymentChannelClient(service_wallet) channel_client.sync() channel_urls = channel_client.list() channels_balance = sum(s.balance for s in (channel_client.status(url) for url in channel_urls) if s.state == channels.PaymentChannelState.READY) template["channels"] = channels_balance except AttributeError: template["wallet"] = 0 template["channels"] = 0 balances[service] = template return balances
def get_payments_server_balance(provider): with open(Two1Composer.COMPOSE_FILE, "r") as f: info = yaml.load(f) mnemonic = info['services']['payments']['environment'][ 'TWO1_WALLET_MNEMONIC'] payments_wallet = Two1Wallet.import_from_mnemonic(data_provider=provider, mnemonic=mnemonic) balances = {"onchain": payments_wallet.balances["total"], "channels": None} channel_client = channels.PaymentChannelClient(payments_wallet) channel_client.sync() channel_urls = channel_client.list() channels_balance = sum(s.balance for s in (channel_client.status(url) for url in channel_urls) if s.state == channels.PaymentChannelState.READY) balances["channels"] = channels_balance return balances
def status_wallet(client, wallet, detail=False): """ Logs a formatted string displaying wallet status to the command line Args: client (TwentyOneRestClient): rest client used for communication with the backend api detail (bool): Lists all balance details in status report Returns: dict: a dictionary of 'wallet' and 'buyable' items with formatted strings for each value """ channel_client = channels.PaymentChannelClient(wallet) user_balances = _get_balances(client, wallet, channel_client) status_wallet_dict = { "twentyone_balance": user_balances.twentyone, "onchain": user_balances.onchain, "flushing": user_balances.flushed, "channels_balance": user_balances.channels } logger.info(uxstring.UxString.status_wallet.format(**status_wallet_dict)) if detail: # show balances by address for default wallet address_balances = wallet.balances_by_address(0) status_addresses = [] for addr, balances in address_balances.items(): if balances['confirmed'] > 0 or balances['total'] > 0: status_addresses.append( uxstring.UxString.status_wallet_address.format( addr, balances['confirmed'], balances['total'])) # Display status for all payment channels status_channels = [] for url in channel_client.list(): status_resp = channel_client.status(url) url = urllib.parse.urlparse(url) status_channels.append( uxstring.UxString.status_wallet_channel.format( url.scheme, url.netloc, status_resp.state, status_resp.balance, format_expiration_time(status_resp.expiration_time))) if not len(status_channels): status_channels = [uxstring.UxString.status_wallet_channels_none] logger.info( uxstring.UxString.status_wallet_detail_on.format( addresses=''.join(status_addresses), channels=''.join(status_channels))) else: logger.info(uxstring.UxString.status_wallet_detail_off) total_balance = user_balances.twentyone + user_balances.onchain if total_balance == 0: if bitcoin_computer.has_mining_chip(): command = "21 mine" else: command = "21 earn" logger.info( uxstring.UxString.status_empty_wallet.format( click.style(command, bold=True))) else: buy21 = click.style("21 buy", bold=True) buy21help = click.style("21 buy --help", bold=True) logger.info( uxstring.UxString.status_exit_message.format(buy21, buy21help)) return { "wallet": status_wallet_dict, }
def twentyone_balance(client, wallet): channel_client = channels.PaymentChannelClient(wallet) user_balances = _get_balances(client, wallet, channel_client) return user_balances.twentyone