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/db/historydb"
9)
10
11const (
12 // maxLimit is the max permited items to be returned in paginated responses
13 maxLimit uint = 2049
14
15 // dfltOrder indicates how paginated endpoints are ordered if not specified
16 dfltOrder = historydb.OrderAsc
17
18 // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
19 dfltLimit uint = 20
20
21 // 2^32 -1
22 maxUint32 = 4294967295
23)
24
25func postAccountCreationAuth(c *gin.Context) {
26
27}
28
29func getAccountCreationAuth(c *gin.Context) {
30
31}
32
33func postPoolTx(c *gin.Context) {
34
35}
36
37func getPoolTx(c *gin.Context) {
38
39}
40
41func getAccounts(c *gin.Context) {
42
43}
44
45func getAccount(c *gin.Context) {
46
47}
48
49func getExits(c *gin.Context) {
50 // Get query parameters
51 // Account filters
52 tokenID, addr, bjj, idx, err := parseAccountFilters(c)
53 if err != nil {
54 retBadReq(err, c)
55 return
56 }
57 // BatchNum
58 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
59 if err != nil {
60 retBadReq(err, c)
61 return
62 }
63 // Pagination
64 fromItem, order, limit, err := parsePagination(c)
65 if err != nil {
66 retBadReq(err, c)
67 return
68 }
69
70 // Fetch exits from historyDB
71 exits, pagination, err := h.GetExits(
72 addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
73 )
74 if err != nil {
75 retSQLErr(err, c)
76 return
77 }
78
79 // Build succesfull response
80 apiExits := historyExitsToAPI(exits)
81 c.JSON(http.StatusOK, &exitsAPI{
82 Exits: apiExits,
83 Pagination: pagination,
84 })
85}
86
87func getExit(c *gin.Context) {
88 // Get batchNum and accountIndex
89 batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
90 if err != nil {
91 retBadReq(err, c)
92 return
93 }
94 idx, err := parseParamIdx(c)
95 if err != nil {
96 retBadReq(err, c)
97 return
98 }
99 // Fetch tx from historyDB
100 exit, err := h.GetExit(batchNum, idx)
101 if err != nil {
102 retSQLErr(err, c)
103 return
104 }
105 apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
106 // Build succesfull response
107 c.JSON(http.StatusOK, apiExits[0])
108}
109
110func getHistoryTxs(c *gin.Context) {
111 // Get query parameters
112 tokenID, addr, bjj, idx, err := parseAccountFilters(c)
113 if err != nil {
114 retBadReq(err, c)
115 return
116 }
117 // BatchNum
118 batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
119 if err != nil {
120 retBadReq(err, c)
121 return
122 }
123 // TxType
124 txType, err := parseQueryTxType(c)
125 if err != nil {
126 retBadReq(err, c)
127 return
128 }
129 // Pagination
130 fromItem, order, limit, err := parsePagination(c)
131 if err != nil {
132 retBadReq(err, c)
133 return
134 }
135
136 // Fetch txs from historyDB
137 txs, pagination, err := h.GetHistoryTxs(
138 addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
139 )
140 if err != nil {
141 retSQLErr(err, c)
142 return
143 }
144
145 // Build succesfull response
146 apiTxs := historyTxsToAPI(txs)
147 c.JSON(http.StatusOK, &historyTxsAPI{
148 Txs: apiTxs,
149 Pagination: pagination,
150 })
151}
152
153func getHistoryTx(c *gin.Context) {
154 // Get TxID
155 txID, err := parseParamTxID(c)
156 if err != nil {
157 retBadReq(err, c)
158 return
159 }
160 // Fetch tx from historyDB
161 tx, err := h.GetHistoryTx(txID)
162 if err != nil {
163 retSQLErr(err, c)
164 return
165 }
166 apiTxs := historyTxsToAPI([]historydb.HistoryTx{*tx})
167 // Build succesfull response
168 c.JSON(http.StatusOK, apiTxs[0])
169}
170
171func getBatches(c *gin.Context) {
172
173}
174
175func getBatch(c *gin.Context) {
176
177}
178
179func getFullBatch(c *gin.Context) {
180
181}
182
183func getSlots(c *gin.Context) {
184
185}
186
187func getBids(c *gin.Context) {
188
189}
190
191func getNextForgers(c *gin.Context) {
192
193}
194
195func getState(c *gin.Context) {
196
197}
198
199func getConfig(c *gin.Context) {
200
201}
202
203func getTokens(c *gin.Context) {
204
205}
206
207func getToken(c *gin.Context) {
208
209}
210
211func getRecommendedFee(c *gin.Context) {
212
213}
214
215func getCoordinators(c *gin.Context) {
216
217}
218
219func getCoordinator(c *gin.Context) {
220
221}
222
223func retSQLErr(err error, c *gin.Context) {
224 if err == sql.ErrNoRows {
225 c.JSON(http.StatusNotFound, errorMsg{
226 Message: err.Error(),
227 })
228 } else {
229 c.JSON(http.StatusInternalServerError, errorMsg{
230 Message: err.Error(),
231 })
232 }
233}
234
235func retBadReq(err error, c *gin.Context) {
236 c.JSON(http.StatusBadRequest, errorMsg{
237 Message: err.Error(),
238 })
239}