From 9a64979aa745e5a435d5ffd4246d811ee7277679 Mon Sep 17 00:00:00 2001 From: poka Date: Tue, 15 Dec 2020 21:19:36 +0100 Subject: [PATCH] Full history with pagination --- jaklis.py | 3 +- lib/gva.py | 4 +-- lib/gvaHistory.py | 70 +++++++++++++++++++++++++++++++++++++---------- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/jaklis.py b/jaklis.py index 6ae24fd..327e4d9 100755 --- a/jaklis.py +++ b/jaklis.py @@ -74,6 +74,7 @@ pay_cmd.add_argument('-m', '--mempool', action='store_true', help="Utilise les s pay_cmd.add_argument('-v', '--verbose', action='store_true', help="Affiche le résultat JSON de la transaction") history_cmd.add_argument('-p', '--pubkey', help="Clé publique du compte visé") +history_cmd.add_argument('-n', '--number',type=int, default=10, help="Affiche les NUMBER dernières transactions") history_cmd.add_argument('-j', '--json', action='store_true', help="Affiche le résultat en format JSON") history_cmd.add_argument('--nocolors', action='store_true', help="Affiche le résultat en noir et blanc") @@ -217,7 +218,7 @@ elif cmd in ("pay","history","balance"): if cmd == "pay": gva.pay(args.amount, args.comment, args.mempool, args.verbose) if cmd == "history": - gva.history(args.json, args.nocolors) + gva.history(args.json, args.nocolors, args.number) if cmd == "balance": gva.balance(args.mempool) diff --git a/lib/gva.py b/lib/gva.py index eaf22fd..0ec1ad6 100644 --- a/lib/gva.py +++ b/lib/gva.py @@ -42,9 +42,9 @@ class GvaApi(): gva.signDoc() return gva.sendTXDoc() - def history(self, isJSON=False, noColors=False): + def history(self, isJSON=False, noColors=False, number=10): gva = History(self.dunikey, self.node, self.destPubkey) - gva.sendDoc() + gva.sendDoc(number) transList = gva.parseHistory() if isJSON: diff --git a/lib/gvaHistory.py b/lib/gvaHistory.py index f858658..cfcad0f 100644 --- a/lib/gvaHistory.py +++ b/lib/gvaHistory.py @@ -23,14 +23,14 @@ class History: transport = AIOHTTPTransport(url=node) self.client = Client(transport=transport, fetch_schema_from_transport=True) - def sendDoc(self): + def sendDoc(self, number): # Build history generation document queryBuild = gql( """ - query ($pubkey: String!){ + query ($pubkey: String!, $number: Int!){ txsHistoryBc( pubkeyOrScript: $pubkey - pagination: { pageSize: 10, ord: DESC } + pagination: { pageSize: $number, ord: DESC } ) { both { pageInfo { @@ -57,6 +57,13 @@ class History: outputs writtenTime } + receiving { + currency + issuers + comment + outputs + writtenTime + } } balance(script: $pubkey) { amount @@ -75,7 +82,8 @@ class History: """ ) paramsBuild = { - "pubkey": self.pubkey + "pubkey": self.pubkey, + "number": number } # Send history document @@ -95,18 +103,51 @@ class History: self.UD = self.historyDoc['currentUd']['amount']/100 - resBc = self.historyDoc['txsHistoryBc']['both']['edges'][0] - for transaction in resBc: - direction = resBc['direction'] - transaction = resBc['node'] + # Parse transactions in blockchain + resBc = [] + resBc = self.historyDoc['txsHistoryBc']['both']['edges'] + for j, transaction in enumerate(resBc): + # print(transaction) + direction = resBc[j]['direction'] + transaction = resBc[j]['node'] output = transaction['outputs'][0] outPubkey = output.split("SIG(")[1].replace(')','') - if direction == 'RECEIVED' or self.pubkey != outPubkey: + # if direction == 'RECEIVED' or self.pubkey != outPubkey: + trans.append(i) + trans[i] = [] + trans[i].append(direction) + trans[i].append(transaction['writtenTime']) + if direction == 'SENT': + trans[i].append(outPubkey) + amount = int('-' + output.split(':')[0]) + else: + trans[i].append(transaction['issuers'][0]) + amount = int(output.split(':')[0]) + base = int(output.split(':')[1]) + applyBase = base-currentBase + amount = round(amount*pow(10,applyBase)/100, 2) + # if referential == 'DU': amount = round(amount/UD, 2) + trans[i].append(amount) + trans[i].append(round(amount/self.UD, 2)) + trans[i].append(transaction['comment']) + trans[i].append(base) + i += 1 + + # Parse transactions in mempool + for direction in self.historyDoc['txsHistoryMp']: + resBc = [] + resBc = self.historyDoc['txsHistoryMp'][direction] + for j, transaction in enumerate(resBc): + # print(transaction) + transaction = resBc[j] + output = transaction['outputs'][0] + outPubkey = output.split("SIG(")[1].replace(')','') + # if direction == 'RECEIVING' or self.pubkey != outPubkey: trans.append(i) trans[i] = [] trans[i].append(direction) - trans[i].append(transaction['writtenTime']) - if direction == 'SENT': + trans[i].append(int(time.time())) + if direction == 'SENDING': trans[i].append(outPubkey) amount = int('-' + output.split(':')[0]) else: @@ -157,13 +198,14 @@ class History: print(isBold + "|{: <19} | {: <12} | {: <7} | {: <7} | {: <30}".format(" Date"," De / À"," {0}".format(currency)," DU/{0}".format(currency.lower()),"Commentaire") + isBoldEnd) print('|', end='') for t in trans: - if t[0] == "received": color = "green" + if t[0] == "RECEIVED": color = "green" + elif t[0] == "SENT": color = "blue" elif t[0] == "receiving": color = "yellow" elif t[0] == "sending": color = "red" - else: color = "blue" + else: color = None if noColors: color = None - if t[0] in ('receiving','sending'): + if t[0] in ('RECEIVING','SENDING'): comment = '(EN ATTENTE) ' + t[5] else: comment = t[5]