Add balance lib: Miss consumed UD
This commit is contained in:
parent
6126dad72b
commit
b0333ab908
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys, argparse, os
|
||||
from os.path import join, dirname
|
||||
from shutil import copyfile
|
||||
from dotenv import load_dotenv
|
||||
from libs.balancelib import Balance
|
||||
|
||||
# Get variables environment
|
||||
if not os.path.isfile('.env'):
|
||||
copyfile(".env.template", ".env")
|
||||
dotenv_path = join(dirname(__file__), '.env')
|
||||
load_dotenv(dotenv_path)
|
||||
|
||||
dunikey = os.getenv('DUNIKEY')
|
||||
node = os.getenv('NODE')
|
||||
if not node:
|
||||
sys.stderr.write("Please fill a Duniter node in .env file\n")
|
||||
sys.exit(1)
|
||||
|
||||
# Parse arguments
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-p', '--pubkey', help="Clé publique du compte visé")
|
||||
parser.add_argument('--mempool', action='store_true', help="Utilise les sources en Mempool")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Create transaction and send it
|
||||
balance = Balance(dunikey, node, args.pubkey, args.mempool)
|
||||
result = balance.balance()
|
|
@ -0,0 +1,78 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys, re, os.path, json, ast
|
||||
from termcolor import colored
|
||||
from 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 Balance:
|
||||
|
||||
def __init__(self, dunikey, node, pubkey, useMempool=False):
|
||||
self.dunikey = dunikey
|
||||
self.pubkey = pubkey if pubkey else get_privkey(dunikey, "pubsec").pubkey
|
||||
self.useMempool = useMempool
|
||||
if not re.match(PUBKEY_REGEX, self.pubkey) or len(self.pubkey) > 45:
|
||||
sys.stderr.write("La clé publique n'est pas au bon format.\n")
|
||||
sys.exit(1)
|
||||
|
||||
# Define Duniter GVA node
|
||||
transport = AIOHTTPTransport(url=node)
|
||||
self.client = Client(transport=transport, fetch_schema_from_transport=True)
|
||||
|
||||
def sendDoc(self):
|
||||
# Build TX generation document
|
||||
queryBuild = gql(
|
||||
"""
|
||||
query ($pubkey: String!){
|
||||
transactionsHistory(pubkey: $pubkey) {
|
||||
received {
|
||||
outputs
|
||||
}
|
||||
sent {
|
||||
outputs
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
paramsBuild = {
|
||||
"pubkey": self.pubkey
|
||||
}
|
||||
|
||||
# Send TX document
|
||||
try:
|
||||
self.historyDoc = 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 de l'historique:\n" + message + "\n")
|
||||
sys.exit(1)
|
||||
|
||||
res = self.historyDoc['transactionsHistory']['received']
|
||||
amount=[]
|
||||
for i in res:
|
||||
for output in i['outputs']:
|
||||
if re.search(self.pubkey, output):
|
||||
amount.append(int(output.split(':')[0]))
|
||||
receivedTotal = sum(amount)
|
||||
|
||||
res = self.historyDoc['transactionsHistory']['sent']
|
||||
amount=[]
|
||||
for i in res:
|
||||
for output in i['outputs']:
|
||||
if not re.search(self.pubkey, output):
|
||||
amount.append(int(output.split(':')[0]))
|
||||
sentTotal = sum(amount)
|
||||
|
||||
|
||||
# TODO: Get all UD from pubkey
|
||||
|
||||
|
||||
total = (receivedTotal-sentTotal)/100
|
||||
print(total)
|
||||
|
||||
def balance(self):
|
||||
self.sendDoc()
|
||||
|
Reference in New Issue