#!/usr/bin/env python3 """ This script makes sure that the merchant backend instances used by the test/demo environment are created. We assume that the merchant backend is running, and that the "~/activate" file has been sourced to provide the right environment variables. """ import requests from os import environ from sys import exit from urllib.parse import urljoin from taler.util.amount import Amount def expect_env(name): val = environ.get(name) if not val: print(f"{name} not defined. Please source the ~/activate file.") exit(1) return val MERCHANT_BACKEND_BASE_URL = expect_env("TALER_ENV_MERCHANT_BACKEND") TALER_ENV_FRONTENDS_APITOKEN = expect_env("TALER_ENV_FRONTENDS_APITOKEN") TALER_CONFIG_CURRENCY = expect_env("TALER_CONFIG_CURRENCY") resp = requests.get( urljoin(MERCHANT_BACKEND_BASE_URL, "instances/survey/private/reserves"), headers = {"Authorization": f"Bearer {TALER_ENV_FRONTENDS_APITOKEN}"} ) if resp.status_code != 200: print("merchant backend failed at providing a list of tip reserves!") exit(1) reserves = resp.json().get("reserves") if len(reserves) == 0: print("merchant backend has NO tip reserves active!") exit(1) max_amount = Amount(TALER_CONFIG_CURRENCY, 0, 0) for item in reserves: committed_amount = Amount.parse(item.get("committed_amount")) allocated_amount = Amount.parse(item.get("merchant_initial_amount")) confirmed_amount = Amount.parse(item.get("exchange_initial_amount")) if allocated_amount != confirmed_amount: print("WARNING: merchant_initial_amount != exchange_initial_amount") exit(1) remaining_amount = allocated_amount - committed_amount if remaining_amount > max_amount: max_amount = remaining_amount print("Max tip reserve: {}".format(max_amount))