arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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)
 10
 11// maxLimit is the max permited items to be returned in paginated responses
 12const maxLimit uint = 2049
 13
 14// dfltLast indicates how paginated endpoints use the query param last if not provided
 15const dfltLast = false
 16
 17// dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
 18const dfltLimit uint = 20
 19
 20// 2^32 -1
 21const maxUint32 = 4294967295
 22
 23func postAccountCreationAuth(c *gin.Context) {
 24
 25}
 26
 27func getAccountCreationAuth(c *gin.Context) {
 28
 29}
 30
 31func postPoolTx(c *gin.Context) {
 32
 33}
 34
 35func getPoolTx(c *gin.Context) {
 36
 37}
 38
 39func getAccounts(c *gin.Context) {
 40
 41}
 42
 43func getAccount(c *gin.Context) {
 44
 45}
 46
 47func getExits(c *gin.Context) {
 48
 49}
 50
 51func getExit(c *gin.Context) {
 52
 53}
 54
 55func getHistoryTxs(c *gin.Context) {
 56	// Get query parameters
 57	// TokenID
 58	tokenID, err := parseQueryUint("tokenId", nil, 0, maxUint32, c)
 59	if err != nil {
 60		retBadReq(err, c)
 61		return
 62	}
 63	// Hez Eth addr
 64	addr, err := parseQueryHezEthAddr(c)
 65	if err != nil {
 66		retBadReq(err, c)
 67		return
 68	}
 69	// BJJ
 70	bjj, err := parseQueryBJJ(c)
 71	if err != nil {
 72		retBadReq(err, c)
 73		return
 74	}
 75	if addr != nil && bjj != nil {
 76		retBadReq(errors.New("bjj and hermezEthereumAddress params are incompatible"), c)
 77		return
 78	}
 79	// Idx
 80	idx, err := parseIdx(c)
 81	if err != nil {
 82		retBadReq(err, c)
 83		return
 84	}
 85	if idx != nil && (addr != nil || bjj != nil || tokenID != nil) {
 86		retBadReq(errors.New("accountIndex is incompatible with BJJ, hermezEthereumAddress and tokenId"), 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	// TxType
 96	txType, err := parseQueryTxType(c)
 97	if err != nil {
 98		retBadReq(err, c)
 99		return
100	}
101	// Pagination
102	offset, last, limit, err := parsePagination(c)
103	if err != nil {
104		retBadReq(err, c)
105		return
106	}
107
108	// Fetch txs from historyDB
109	txs, totalItems, err := h.GetHistoryTxs(
110		addr, bjj, tokenID, idx, batchNum, txType, offset, limit, *last,
111	)
112	if err != nil {
113		retSQLErr(err, c)
114		return
115	}
116
117	// Build succesfull response
118	apiTxs := historyTxsToAPI(txs)
119	lastRet := int(*offset) + len(apiTxs) - 1
120	if *last {
121		lastRet = totalItems - 1
122	}
123	c.JSON(http.StatusOK, &historyTxsAPI{
124		Txs: apiTxs,
125		Pagination: pagination{
126			TotalItems:       totalItems,
127			LastReturnedItem: lastRet,
128		},
129	})
130}
131
132func getHistoryTx(c *gin.Context) {
133
134}
135
136func getBatches(c *gin.Context) {
137
138}
139
140func getBatch(c *gin.Context) {
141
142}
143
144func getFullBatch(c *gin.Context) {
145
146}
147
148func getSlots(c *gin.Context) {
149
150}
151
152func getBids(c *gin.Context) {
153
154}
155
156func getNextForgers(c *gin.Context) {
157
158}
159
160func getState(c *gin.Context) {
161
162}
163
164func getConfig(c *gin.Context) {
165
166}
167
168func getTokens(c *gin.Context) {
169
170}
171
172func getToken(c *gin.Context) {
173
174}
175
176func getRecommendedFee(c *gin.Context) {
177
178}
179
180func getCoordinators(c *gin.Context) {
181
182}
183
184func getCoordinator(c *gin.Context) {
185
186}
187
188func retSQLErr(err error, c *gin.Context) {
189	if err == sql.ErrNoRows {
190		c.JSON(http.StatusNotFound, errorMsg{
191			Message: err.Error(),
192		})
193	} else {
194		c.JSON(http.StatusInternalServerError, errorMsg{
195			Message: err.Error(),
196		})
197	}
198}
199
200func retBadReq(err error, c *gin.Context) {
201	c.JSON(http.StatusBadRequest, errorMsg{
202		Message: err.Error(),
203	})
204}