arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/exits.go
api/exits.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 getExits(c *gin.Context) {
12	// Get query parameters
13	// Account filters
14	tokenID, addr, bjj, idx, err := parseAccountFilters(c)
15	if err != nil {
16		retBadReq(err, c)
17		return
18	}
19	// BatchNum
20	batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
21	if err != nil {
22		retBadReq(err, c)
23		return
24	}
25	// OnlyPendingWithdraws
26	onlyPendingWithdraws, err := parseQueryBool("onlyPendingWithdraws", nil, c)
27	if err != nil {
28		retBadReq(err, c)
29		return
30	}
31	// Pagination
32	fromItem, order, limit, err := parsePagination(c)
33	if err != nil {
34		retBadReq(err, c)
35		return
36	}
37
38	// Fetch exits from historyDB
39	exits, pagination, err := h.GetExitsAPI(
40		addr, bjj, tokenID, idx, batchNum, onlyPendingWithdraws, fromItem, limit, order,
41	)
42	if err != nil {
43		retSQLErr(err, c)
44		return
45	}
46
47	// Build succesfull response
48	type exitsResponse struct {
49		Exits      []historydb.ExitAPI `json:"exits"`
50		Pagination *db.Pagination      `json:"pagination"`
51	}
52	c.JSON(http.StatusOK, &exitsResponse{
53		Exits:      exits,
54		Pagination: pagination,
55	})
56}
57
58func getExit(c *gin.Context) {
59	// Get batchNum and accountIndex
60	batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
61	if err != nil {
62		retBadReq(err, c)
63		return
64	}
65	idx, err := parseParamIdx(c)
66	if err != nil {
67		retBadReq(err, c)
68		return
69	}
70	// Fetch tx from historyDB
71	exit, err := h.GetExitAPI(batchNum, idx)
72	if err != nil {
73		retSQLErr(err, c)
74		return
75	}
76	// Build succesfull response
77	c.JSON(http.StatusOK, exit)
78}