hermez-node/api/handlers.go
api/handlers.goRaw
1package api
2
3import (
4 "database/sql"
5 "net/http"
6
7 "github.com/gin-gonic/gin"
8 "github.com/hermeznetwork/hermez-node/common"
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
26func postAccountCreationAuth(c *gin.Context) {
27
28}
29
30func getAccountCreationAuth(c *gin.Context) {
31}
32
33func postPoolTx(c *gin.Context) {
34 // Parse body
35 var receivedTx receivedPoolTx
36 if err := c.ShouldBindJSON(&receivedTx); err != nil {
37 retBadReq(err, c)
38 return
39 }
40 // Transform from received to insert format and validate
41 writeTx, err := receivedTx.toDBWritePoolL2Tx()
42 if err != nil {
43 retBadReq(err, c)
44 return
45 }
46 // Insert to DB
47 if err := l2.AddTx(writeTx); err != nil {
48 retSQLErr(err, c)
49 return
50 }
51 // Return TxID
52 c.JSON(http.StatusOK, writeTx.TxID.String())
53}
54
55func getPoolTx(c *gin.Context) {
56 // Get TxID
57 txID, err := parseParamTxID(c)
58 if err != nil {
59 retBadReq(err, c)
60 return
61 }
62 // Fetch tx from l2DB
63 dbTx, err := l2.GetTx(txID)
64 if err != nil {
65 retSQLErr(err, c)
66 return
67 }
68 apiTx := poolL2TxReadToSend(dbTx)
69 // Build succesfull response
70 c.JSON(http.StatusOK, apiTx)
71}
72
73func getAccounts(c *gin.Context) {
74
75}
76
77func getAccount(c *gin.Context) {
78
79}
80
81func getExits(c *gin.Context) {
82 // Get query parameters
83 // Account filters
84 tokenID, addr, bjj, idx, err := parseAccountFilters(c)
85 if err != nil {
86 retBadReq(err, c)
87 return
88 }
89 // BatchNum
90 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
91 if err != nil {
92 retBadReq(err, c)
93 return
94 }
95 // Pagination
96 fromItem, order, limit, err := parsePagination(c)
97 if err != nil {
98 retBadReq(err, c)
99 return
100 }
101
102 // Fetch exits from historyDB
103 exits, pagination, err := h.GetExits(
104 addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
105 )
106 if err != nil {
107 retSQLErr(err, c)
108 return
109 }
110
111 // Build succesfull response
112 apiExits := historyExitsToAPI(exits)
113 c.JSON(http.StatusOK, &exitsAPI{
114 Exits: apiExits,
115 Pagination: pagination,
116 })
117}
118
119func getExit(c *gin.Context) {
120 // Get batchNum and accountIndex
121 batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
122 if err != nil {
123 retBadReq(err, c)
124 return
125 }
126 idx, err := parseParamIdx(c)
127 if err != nil {
128 retBadReq(err, c)
129 return
130 }
131 // Fetch tx from historyDB
132 exit, err := h.GetExit(batchNum, idx)
133 if err != nil {
134 retSQLErr(err, c)
135 return
136 }
137 apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
138 // Build succesfull response
139 c.JSON(http.StatusOK, apiExits[0])
140}
141
142func getHistoryTxs(c *gin.Context) {
143 // Get query parameters
144 tokenID, addr, bjj, idx, err := parseAccountFilters(c)
145 if err != nil {
146 retBadReq(err, c)
147 return
148 }
149 // BatchNum
150 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
151 if err != nil {
152 retBadReq(err, c)
153 return
154 }
155 // TxType
156 txType, err := parseQueryTxType(c)
157 if err != nil {
158 retBadReq(err, c)
159 return
160 }
161 // Pagination
162 fromItem, order, limit, err := parsePagination(c)
163 if err != nil {
164 retBadReq(err, c)
165 return
166 }
167
168 // Fetch txs from historyDB
169 txs, pagination, err := h.GetHistoryTxs(
170 addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
171 )
172 if err != nil {
173 retSQLErr(err, c)
174 return
175 }
176
177 // Build succesfull response
178 apiTxs := historyTxsToAPI(txs)
179 c.JSON(http.StatusOK, &historyTxsAPI{
180 Txs: apiTxs,
181 Pagination: pagination,
182 })
183}
184
185func getHistoryTx(c *gin.Context) {
186 // Get TxID
187 txID, err := parseParamTxID(c)
188 if err != nil {
189 retBadReq(err, c)
190 return
191 }
192 // Fetch tx from historyDB
193 tx, err := h.GetHistoryTx(txID)
194 if err != nil {
195 retSQLErr(err, c)
196 return
197 }
198 apiTxs := historyTxsToAPI([]historydb.HistoryTx{*tx})
199 // Build succesfull response
200 c.JSON(http.StatusOK, apiTxs[0])
201}
202
203func getBatches(c *gin.Context) {
204
205}
206
207func getBatch(c *gin.Context) {
208
209}
210
211func getFullBatch(c *gin.Context) {
212
213}
214
215func getSlots(c *gin.Context) {
216
217}
218
219func getBids(c *gin.Context) {
220
221}
222
223func getNextForgers(c *gin.Context) {
224
225}
226
227func getState(c *gin.Context) {
228
229}
230
231func getConfig(c *gin.Context) {
232 c.JSON(http.StatusOK, cg)
233}
234
235func getTokens(c *gin.Context) {
236 // Account filters
237 tokenIDs, symbols, name, err := parseTokenFilters(c)
238 if err != nil {
239 retBadReq(err, c)
240 return
241 }
242 // Pagination
243 fromItem, order, limit, err := parsePagination(c)
244 if err != nil {
245 retBadReq(err, c)
246 return
247 }
248 // Fetch exits from historyDB
249 tokens, pagination, err := h.GetTokens(
250 tokenIDs, symbols, name, fromItem, limit, order,
251 )
252 if err != nil {
253 retSQLErr(err, c)
254 return
255 }
256
257 // Build succesfull response
258 apiTokens := tokensToAPI(tokens)
259 c.JSON(http.StatusOK, &tokensAPI{
260 Tokens: apiTokens,
261 Pagination: pagination,
262 })
263}
264
265func getToken(c *gin.Context) {
266 // Get TokenID
267 tokenIDUint, err := parseParamUint("id", nil, 0, maxUint32, c)
268 if err != nil {
269 retBadReq(err, c)
270 return
271 }
272 tokenID := common.TokenID(*tokenIDUint)
273 // Fetch token from historyDB
274 token, err := h.GetToken(tokenID)
275 if err != nil {
276 retSQLErr(err, c)
277 return
278 }
279 apiToken := tokensToAPI([]historydb.TokenRead{*token})
280 c.JSON(http.StatusOK, apiToken[0])
281}
282
283func getRecommendedFee(c *gin.Context) {
284
285}
286
287func getCoordinators(c *gin.Context) {
288
289}
290
291func getCoordinator(c *gin.Context) {
292
293}
294
295func retSQLErr(err error, c *gin.Context) {
296 if err == sql.ErrNoRows {
297 c.JSON(http.StatusNotFound, errorMsg{
298 Message: err.Error(),
299 })
300 } else {
301 c.JSON(http.StatusInternalServerError, errorMsg{
302 Message: err.Error(),
303 })
304 }
305}
306
307func retBadReq(err error, c *gin.Context) {
308 c.JSON(http.StatusBadRequest, errorMsg{
309 Message: err.Error(),
310 })
311}