Hivejs: How to get filtered AccountHistory? Wie bekommt man eine gefilterte AccountHistory? #5

in #hive2 years ago

EN

In the last post I showed you how to cast a vote on a post or even comment. All votes you cast are stored forever in the blockchain and of course all other actions like transfers, posted posts and comments. And all this data can be retrieved via getAccountHistory.

In the linked documentation you can also find a small example, but this one requires node.js, which I don't currently use. So I rewrote this to work without it. This will only work in modern browsers, but I think we all should use them by now.

Bootstrap

This time I included Bootstrap in addition to hivejs and jQuery because I wanted the output to look a bit fancier and with Bootstrap this is much easier and I don't have to worry about making the table look fancy too.

ChainTypes

First of all I included once the ChainTypes that you can find here, this file would be used by the example available in the documentation, but that doesn't work for me.

For me here are more important the first ones like vote, comment, transfer, custom_json. For most of them you can guess what they stand for, but for all of them it doesn't make sense to me yet.

In line 15 I inserted an empty select element , that I fill with the ChainTypes.operations, so you can select them in the dropdown.

This is done in lines 127-134, which I won't go into now because it has nothing to do with hivejs itself. If you don't understand the lines, you should look into jQuery a bit.

parameters

  • account the account whose history you want to read.
  • from start point, from where you want to start, -1 = from last entry
  • limit value between 1 and 1000 otherwise you will get an error like this
    to avoid request timeouts you should choose a value smaller than 1000.
  • operation_filter_low and operation_filter_high here you pass the operations you want to get.

limit

Please do not expect exactly as many results as you specify here, it is the maximum you will get. As it seems, from the start value (from) at most the last 2000 transactions are checked and at most the number you specify at limit is returned, but if in the last 2000 transactions there are less than limit results, you will get only those.

So if you want to have "limit" entries in any case, you may have to specify a new starting value.

I have built in a check, in line 167, if comments is selected, the limit is set to 5 and otherwise it is at 100. I had initially here a value of 1000 always and when retrieving the comments, I have even twice crashed my PC. It is no longer the youngest, but actually still quite fast. So be careful what and how much you try to get. If you select votes 1000 should be no problem.

operation_filter_low and operation_filter_high

In my tests, changing the high parameter had no effect. Only what was passed as the low parameter changed anything in the result. By my dropdown example only one operation is queried at a time.
If you want to query more than one at a time, you can use the commented out area in lines 154-163. Line 157 contains vote,comment and transfer. To do this, comment out lines 146-152 and the dropdown will be ignored.

makeBitMaskFilter.

To create the filter, makeBitMaskFilter is used, which I rewrote in lines 136-141 to work without Node.js, but this might be buggy in older browsers. However, I now assume that everyone uses a new one.

I use dropdown selection in line 146/15 for both walletOperationsBitmasklow and walletOperationsBitmaskhigh, as I said I haven't noticed any difference so far when I used something else for walletOperationsBitmaskhigh.

Output

Provided there is a return, I output it in a table, with the ID, operation type and its data as columns.

This is pure output of the data, what you can do with it I will show in an example yet, probably in the next post. There I will show you what I have already done with it.

Note that I'm outputting unfiltered data from the blockchain here, which is actually not a good idea, because it could also contain "bad code". Although the portals filter some stuff out before they send the posts etc. to the blockchain, but with hivejs and other libraries you can write anything to the blockchain!

You can find the whole script here

Translated with www.DeepL.com/Translator (free version)

DE

Im letzten Beitrag habe ich euch gezeigt, wie man ein Vote auf einen Beitrag oder auch Kommentar abgeben kann. Alle Votes, die ihr abgebt, sind für immer in der Blockchain gespeichert und natürlich auch alle anderen Aktionen wie Transfers, gepostete Beiträge und Kommentare. Und all diese Daten kann man über getAccountHistory abrufen.

In der verlinkten Dokumentation findet ihr auch ein kleines Beispiel, dieses benötigt aber node.js, was ich aktuell nicht nutze. Daher habe ich das umgeschrieben, damit es auch ohne geht. Dies funktioniert aber dann nur in modernen Browsern, aber ich denke mal, die sollten wir alle inzwischen nutzen.

Bootstrap

Diesmal habe ich neben hivejs und jQuery auch noch Bootstrap eingebunden, da ich die Ausgabe etwas schicker haben wollte und mit Bootstrap das deutlich einfacher ist und ich mich nicht auch darum kümmern muss, dass die Tabelle schick aussieht.

ChainTypes

Zunächst habe ich einmal die ChainTypes eingebunden, die ihr hier findet, diese Datei würde durch das in der Dokumentation vorhandene Beispiel genutzt werden, was aber bei mir nicht geht.

Für mich sind hier eher die ersten wichtig wie z.B. vote, comment, transfer, custom_json. Bei den meisten kann man erahnen, wofür diese stehen, aber bei allen erschließt es sich mir noch nicht.

In Zeile 15 habe ich ein leeres Select-Element eingefügt , dass ich mit den ChainTypes.operations befülle, damit man diese in der Dropdown auswählen kann.

Dies erfolgt in den Zeilen 127-134, auf die ich jetzt nicht näher eingehe, da das nichts mit hivejs selber zu tun hat. Wer die Zeilen nicht versteht, der sollte sich mit jQuery etwas beschäftigen.

Parameter

  • account den Account, dessen History ihr einlesen wollt.
  • from Startpunkt, von dem ihr anfangen wollt, -1 = vom letzten Eintrag
  • limit Wert zwischen 1 und 1000 ansonsten gibt es einen Fehler wie diesen
    um Request Timeouts zu vermeiden, sollte ein kleinerer Wert als 1000 gewählt werden.
  • operation_filter_low und operation_filter_high hier werden die Operationen übergeben, die man erhalten möchte.

limit

Bitte erwartet nicht genau so viele Ergebnisse, wie ihr hier angebt, es ist das Maximum, was ihr erhaltet. Wie es scheint, werden vom Startwert (from) maximal die letzten 2000 Transktionen geprüft und maximal die Anzahl, die ihr bei limit angebt zurückgegeben, wenn in den letzten 2000 Transaktionen jedoch weniger als limit Ergebnisse vorhanden sind, erhaltet ihr nur diese.

Wenn ihr also auf jeden Fall "limit"-Einträge haben wollt, dann müsst ihr ggf. einen neuen Startwert angeben.

Ich habe in Zeile 167 eine Prüfung eingebaut, wird comments ausgewählt, so wird das limit auf 5 gesetzt und ansonsten ist es bei 100. Ich hatte Anfangs hier ein Wert von 1000 immer und beim Abrufen der comments, habe ich sogar zweimal meinen PC zum Absturz gebracht. Ist ja nicht mehr der jüngste, aber eigentlich dennoch noch recht flott. Also seit vorsichtig, was und wie viel ihr versucht auszugeben. Bei Votes sollten 1000 kein Problem sein.

operation_filter_low und operation_filter_high

Bei meinen Tests hatte es keine Auswirkungen, den high-Parameter zu ändern. Lediglich, was als low-Parameter übergeben wurde änderte etwas an dem Ergebnis etwas. Durch mein Dropdown-Beispiel wird immer nur eine Operation abgefragt.
Möchtet ihr mehrere auf einmal abfragen, so könnt ihr den auskommentierten Bereich in den Zeile 154-163 nutzen. In Zeile 157 sind dort vote,comment und transfer angegeben. Dafür die Zeile 146-152 auskommentieren und die Dropdown wird nicht mehr beachtet.

makeBitMaskFilter

Für die Erstellung des Filters wird makeBitMaskFilter verwendet, welches ich in Zeile 136-141 so umgeschrieben habe, damit es auch ohne Node.js funktioniert, dies könnte aber in älteren Browsern fehlerhaft sein. Ich gehe aber nun mal davon aus, dass jeder einen neuen verwendet.

Ich verwende in Zeile 146/15 sowohl für walletOperationsBitmasklow als auch für walletOperationsBitmaskhigh die Dropdownauswahl, wie gesagt habe ich bisher keinen Unterschied festgestellt, wenn ich für walletOperationsBitmaskhigh etwas anderes verwendet habe.

Ausgabe

Sofern es eine Rückgabe gibt, gebe ich diese in einer Tabelle aus, mit der ID, der Operationsart und dessen Daten als Spalten.

Es handelt sich hierbei um reine Ausgabe der Daten, was man damit machen kann, werde ich in einem Beispiel noch zeigen, vermutlich im nächsten Beitrag. Da zeige ich euch, was ich damit schon gemacht habe.

Beachtet, dass ich hier ungefiltert Daten von der Blockchain ausgebe, was eigentlich keine gute Idee ist, denn diese könnte auch "bösen Code" enthalten. Zwar filtern die Portale einiges aus, bevor diese die Beiträge etc. an die Blockchain schicken, aber mit hivejs und anderen Bibliotheken kann man alles in die Blockchain schreiben!

Das ganze Script findet ihr hier

Sort:  

Congratulations @hive-coding! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You received more than 300 upvotes.
Your next target is to reach 400 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Be ready for the 11th edition of the Hive Power Up Month!
Trick or Treat - Share your scariest story and win your Halloween badge
Hive Power Up Day - November 1st 2022
Support the HiveBuzz project. Vote for our proposal!

🍕 PIZZA !
@hive-coding! The Hive.Pizza team manually upvoted your post.

Send $PIZZA tips in Discord via tip.cc!

Yay! 🤗
Your content has been boosted with Ecency Points, by @mein-senf-dazu.
Use Ecency daily to boost your growth on platform!

Support Ecency
Vote for new Proposal
Delegate HP and earn more