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 postAccountCreationAuth(c *gin.Context) {
32 // Parse body
33 var apiAuth accountCreationAuthAPI
34 if err := c.ShouldBindJSON(&apiAuth); err != nil {
35 retBadReq(err, c)
36 return
37 }
38 // API to common + verify signature
39 dbAuth, err := accountCreationAuthAPIToCommon(&apiAuth)
40 if err != nil {
41 retBadReq(err, c)
42 return
43 }
44 // Insert to DB
45 if err := l2.AddAccountCreationAuth(dbAuth); err != nil {
46 retSQLErr(err, c)
47 return
48 }
49 // Return OK
50 c.Status(http.StatusOK)
51}
52
53func getAccountCreationAuth(c *gin.Context) {
54 // Get hezEthereumAddress
55 addr, err := parseParamHezEthAddr(c)
56 if err != nil {
57 retBadReq(err, c)
58 return
59 }
60 // Fetch auth from l2DB
61 dbAuth, err := l2.GetAccountCreationAuth(*addr)
62 if err != nil {
63 retSQLErr(err, c)
64 return
65 }
66 apiAuth := accountCreationAuthToAPI(dbAuth)
67 // Build succesfull response
68 c.JSON(http.StatusOK, apiAuth)
69}
70
71func getAccounts(c *gin.Context) {
72
73}
74
75func getAccount(c *gin.Context) {
76
77}
78
79func getExits(c *gin.Context) {
80 // Get query parameters
81 // Account filters
82 tokenID, addr, bjj, idx, err := parseAccountFilters(c)
83 if err != nil {
84 retBadReq(err, c)
85 return
86 }
87 // BatchNum
88 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
89 if err != nil {
90 retBadReq(err, c)
91 return
92 }
93 // Pagination
94 fromItem, order, limit, err := parsePagination(c)
95 if err != nil {
96 retBadReq(err, c)
97 return
98 }
99
100 // Fetch exits from historyDB
101 exits, pagination, err := h.GetExits(
102 addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
103 )
104 if err != nil {
105 retSQLErr(err, c)
106 return
107 }
108
109 // Build succesfull response
110 apiExits := historyExitsToAPI(exits)
111 c.JSON(http.StatusOK, &exitsAPI{
112 Exits: apiExits,
113 Pagination: pagination,
114 })
115}
116
117func getExit(c *gin.Context) {
118 // Get batchNum and accountIndex
119 batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
120 if err != nil {
121 retBadReq(err, c)
122 return
123 }
124 idx, err := parseParamIdx(c)
125 if err != nil {
126 retBadReq(err, c)
127 return
128 }
129 // Fetch tx from historyDB
130 exit, err := h.GetExit(batchNum, idx)
131 if err != nil {
132 retSQLErr(err, c)
133 return
134 }
135 apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
136 // Build succesfull response
137 c.JSON(http.StatusOK, apiExits[0])
138}
139
140func getSlots(c *gin.Context) {
141
142}
143
144func getNextForgers(c *gin.Context) {
145
146}
147
148func getState(c *gin.Context) {
149
150}
151
152func getConfig(c *gin.Context) {
153 c.JSON(http.StatusOK, cg)
154}
155
156func getRecommendedFee(c *gin.Context) {
157
158}
159
160func retSQLErr(err error, c *gin.Context) {
161 if err == sql.ErrNoRows {
162 c.JSON(http.StatusNotFound, errorMsg{
163 Message: err.Error(),
164 })
165 } else {
166 c.JSON(http.StatusInternalServerError, errorMsg{
167 Message: err.Error(),
168 })
169 }
170}
171
172func retBadReq(err error, c *gin.Context) {
173 c.JSON(http.StatusBadRequest, errorMsg{
174 Message: err.Error(),
175 })
176}