hermez-node/api/handlers.go
api/handlers.goRaw
1package api
2
3import (
4 "database/sql"
5 "errors"
6 "net/http"
7
8 "github.com/gin-gonic/gin"
9 "github.com/hermeznetwork/hermez-node/db/historydb"
10)
11
12const (
13 // maxLimit is the max permited items to be returned in paginated responses
14 maxLimit uint = 2049
15
16 // dfltOrder indicates how paginated endpoints are ordered if not specified
17 dfltOrder = historydb.OrderAsc
18
19 // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
20 dfltLimit uint = 20
21
22 // 2^32 -1
23 maxUint32 = 4294967295
24)
25
26var (
27 // ErrNillBidderAddr is used when a nil bidderAddr is received in the getCoordinator method
28 ErrNillBidderAddr = errors.New("biderAddr can not be nil")
29)
30
31func getAccounts(c *gin.Context) {
32
33}
34
35func getAccount(c *gin.Context) {
36
37}
38
39func getExits(c *gin.Context) {
40 // Get query parameters
41 // Account filters
42 tokenID, addr, bjj, idx, err := parseAccountFilters(c)
43 if err != nil {
44 retBadReq(err, c)
45 return
46 }
47 // BatchNum
48 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
49 if err != nil {
50 retBadReq(err, c)
51 return
52 }
53 // Pagination
54 fromItem, order, limit, err := parsePagination(c)
55 if err != nil {
56 retBadReq(err, c)
57 return
58 }
59
60 // Fetch exits from historyDB
61 exits, pagination, err := h.GetExits(
62 addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
63 )
64 if err != nil {
65 retSQLErr(err, c)
66 return
67 }
68
69 // Build succesfull response
70 apiExits := historyExitsToAPI(exits)
71 c.JSON(http.StatusOK, &exitsAPI{
72 Exits: apiExits,
73 Pagination: pagination,
74 })
75}
76
77func getExit(c *gin.Context) {
78 // Get batchNum and accountIndex
79 batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
80 if err != nil {
81 retBadReq(err, c)
82 return
83 }
84 idx, err := parseParamIdx(c)
85 if err != nil {
86 retBadReq(err, c)
87 return
88 }
89 // Fetch tx from historyDB
90 exit, err := h.GetExit(batchNum, idx)
91 if err != nil {
92 retSQLErr(err, c)
93 return
94 }
95 apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
96 // Build succesfull response
97 c.JSON(http.StatusOK, apiExits[0])
98}
99
100func getSlots(c *gin.Context) {
101
102}
103
104func getNextForgers(c *gin.Context) {
105
106}
107
108func getState(c *gin.Context) {
109
110}
111
112func getConfig(c *gin.Context) {
113 c.JSON(http.StatusOK, cg)
114}
115
116func getRecommendedFee(c *gin.Context) {
117
118}
119
120func retSQLErr(err error, c *gin.Context) {
121 if err == sql.ErrNoRows {
122 c.JSON(http.StatusNotFound, errorMsg{
123 Message: err.Error(),
124 })
125 } else {
126 c.JSON(http.StatusInternalServerError, errorMsg{
127 Message: err.Error(),
128 })
129 }
130}
131
132func retBadReq(err error, c *gin.Context) {
133 c.JSON(http.StatusBadRequest, errorMsg{
134 Message: err.Error(),
135 })
136}