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