summaryrefslogtreecommitdiff
path: root/buildbot/check_tip_reserve.py
blob: f682a1a90d587d713817eac13658f1858956a572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/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))