hermez-node/api/txshistory.go
api/txshistory.goRaw
1package api
2
3import (
4 "net/http"
5
6 "github.com/gin-gonic/gin"
7 "github.com/hermeznetwork/hermez-node/db/historydb"
8)
9
10func (a *API) getHistoryTxs(c *gin.Context) {
11 // Get query parameters
12 tokenID, addr, bjj, idx, err := parseExitFilters(c)
13 if err != nil {
14 retBadReq(err, c)
15 return
16 }
17 // BatchNum
18 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
19 if err != nil {
20 retBadReq(err, c)
21 return
22 }
23 // TxType
24 txType, err := parseQueryTxType(c)
25 if err != nil {
26 retBadReq(err, c)
27 return
28 }
29 // Pagination
30 fromItem, order, limit, err := parsePagination(c)
31 if err != nil {
32 retBadReq(err, c)
33 return
34 }
35
36 // Fetch txs from historyDB
37 txs, pendingItems, err := a.h.GetHistoryTxs(
38 addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
39 )
40 if err != nil {
41 retSQLErr(err, c)
42 return
43 }
44
45 // Build succesfull response
46 type txsResponse struct {
47 Txs []historydb.TxAPI `json:"transactions"`
48 PendingItems uint64 `json:"pendingItems"`
49 }
50 c.JSON(http.StatusOK, &txsResponse{
51 Txs: txs,
52 PendingItems: pendingItems,
53 })
54}
55
56func (a *API) getHistoryTx(c *gin.Context) {
57 // Get TxID
58 txID, err := parseParamTxID(c)
59 if err != nil {
60 retBadReq(err, c)
61 return
62 }
63 // Fetch tx from historyDB
64 tx, err := a.h.GetHistoryTx(txID)
65 if err != nil {
66 retSQLErr(err, c)
67 return
68 }
69 // Build succesfull response
70 c.JSON(http.StatusOK, tx)
71}