From 6cb7e36bd6767784342f4c6513b3de99d043c769 Mon Sep 17 00:00:00 2001 From: poka Date: Wed, 30 Jun 2021 06:10:48 +0200 Subject: [PATCH] Add command currentUd --- jaklis.py | 8 ++++++-- lib/currentUd.py | 43 +++++++++++++++++++++++++++++++++++++++++++ lib/gva.py | 12 +++++++++--- 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 lib/currentUd.py diff --git a/jaklis.py b/jaklis.py index f88044f..d4c4d0f 100755 --- a/jaklis.py +++ b/jaklis.py @@ -6,7 +6,7 @@ from shutil import copyfile from dotenv import load_dotenv from duniterpy.key import SigningKey -__version__ = "0.0.3" +__version__ = "0.0.4" MY_PATH = os.path.realpath(os.path.dirname(sys.argv[0])) + '/' @@ -39,6 +39,7 @@ history_cmd = subparsers.add_parser('history', help="Voir l'historique des trans balance_cmd = subparsers.add_parser('balance', help="Voir le solde d'un compte Ḡ1") id_cmd = subparsers.add_parser('id', help="Voir l'identité d'une clé publique/username") id_balance_cmd = subparsers.add_parser('idBalance', help="Voir l'identité d'une clé publique/username et son solde") +currentUd = subparsers.add_parser('currentUd', help="Affiche la montant actuel du dividende Universel") # Messages management read_cmd.add_argument('-n', '--number',type=int, default=3, help="Affiche les NUMBER derniers messages") @@ -99,6 +100,7 @@ balance_cmd.add_argument('-m', '--mempool', action='store_true', help="Utilise l id_cmd.add_argument('-p', '--pubkey', help="Clé publique du compte visé") id_cmd.add_argument('-u', '--username', help="Username du compte visé") id_balance_cmd.add_argument('-p', '--pubkey', help="Pubkey du compte visé") +currentUd.add_argument('-p', '--pubkey', help="Pubkey du compte visé") args = parser.parse_args() @@ -225,7 +227,7 @@ if cmd in ("read","send","delete","set","get","erase","stars","unstars","getoffe cesium.deleteOffer(args.id) # Construct GVA object -elif cmd in ("pay","history","balance","id","idBalance"): +elif cmd in ("pay","history","balance","id","idBalance","currentUd"): from lib.gva import GvaApi if args.node: @@ -252,6 +254,8 @@ elif cmd in ("pay","history","balance","id","idBalance"): gva.id(args.pubkey, args.username) elif cmd == "idBalance": gva.idBalance(args.pubkey) + elif cmd == "currentUd": + gva.currentUd() if keyPath: diff --git a/lib/currentUd.py b/lib/currentUd.py new file mode 100644 index 0000000..ae8db26 --- /dev/null +++ b/lib/currentUd.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import sys, re, os.path, json, ast +from termcolor import colored +from lib.natools import fmt, sign, get_privkey +from gql import gql, Client +from gql.transport.aiohttp import AIOHTTPTransport + +PUBKEY_REGEX = "(?![OIl])[1-9A-Za-z]{42,45}" + +class currentUd: + + def __init__(self, node): + # Define Duniter GVA node + transport = AIOHTTPTransport(url=node) + self.client = Client(transport=transport, fetch_schema_from_transport=True) + + def sendDoc(self): + # Build balance generation document + queryBuild = gql( + """ + query { + currentUd { + amount + } + } + """ + ) + paramsBuild = { + } + + # Send balance document + try: + udValue = self.client.execute(queryBuild, variable_values=paramsBuild) + except Exception as e: + message = ast.literal_eval(str(e))["message"] + sys.stderr.write("Echec de récupération du DU:\n" + message + "\n") + sys.exit(1) + + udValueFinal = udValue['currentUd']['amount'] + + # print(balanceValue) + return udValueFinal diff --git a/lib/gva.py b/lib/gva.py index efb57f0..c4173c9 100755 --- a/lib/gva.py +++ b/lib/gva.py @@ -1,3 +1,4 @@ +from lib.currentUd import currentUd import sys, re from lib.natools import get_privkey from lib.gvaPay import Transaction, PUBKEY_REGEX @@ -65,6 +66,11 @@ class GvaApi(): print(result) def idBalance(self, pubkey): - gva = Id(self.dunikey, self.node, pubkey) - result = gva.sendDoc(True) - print(result) \ No newline at end of file + gva = Id(self.dunikey, self.node, pubkey) + result = gva.sendDoc(True) + print(result) + + def currentUd(self): + gva = currentUd(self.node) + result = gva.sendDoc() + print(result) \ No newline at end of file