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
202}
203
204func getTokens(c *gin.Context) {
205
206}
207
208func getToken(c *gin.Context) {
209	// Get TokenID
210	tokenIDUint, err := parseParamUint("id", nil, 0, maxUint32, c)
211	if err != nil {
212		retBadReq(err, c)
213		return
214	}
215	tokenID := common.TokenID(*tokenIDUint)
216	// Fetch token from historyDB
217	token, err := h.GetToken(tokenID)
218	if err != nil {
219		retSQLErr(err, c)
220		return
221	}
222	c.JSON(http.StatusOK, token)
223}
224
225func getRecommendedFee(c *gin.Context) {
226
227}
228
229func getCoordinators(c *gin.Context) {
230
231}
232
233func getCoordinator(c *gin.Context) {
234
235}
236
237func retSQLErr(err error, c *gin.Context) {
238	if err == sql.ErrNoRows {
239		c.JSON(http.StatusNotFound, errorMsg{
240			Message: err.Error(),
241		})
242	} else {
243		c.JSON(http.StatusInternalServerError, errorMsg{
244			Message: err.Error(),
245		})
246	}
247}
248
249func retBadReq(err error, c *gin.Context) {
250	c.JSON(http.StatusBadRequest, errorMsg{
251		Message: err.Error(),
252	})
253}