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