arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/coordinator.go
api/coordinator.goRaw
 1package api
 2
 3import (
 4	"net/http"
 5
 6	"github.com/gin-gonic/gin"
 7	"github.com/hermeznetwork/hermez-node/db/historydb"
 8)
 9
10func (a *API) getCoordinator(c *gin.Context) {
11	// Get bidderAddr
12	const name = "bidderAddr"
13	bidderAddr, err := parseParamEthAddr(name, c)
14
15	if err != nil {
16		retBadReq(err, c)
17		return
18	} else if bidderAddr == nil {
19		retBadReq(ErrNillBidderAddr, c)
20		return
21	}
22
23	coordinator, err := a.h.GetCoordinatorAPI(*bidderAddr)
24	if err != nil {
25		retSQLErr(err, c)
26		return
27	}
28
29	c.JSON(http.StatusOK, coordinator)
30}
31
32func (a *API) getCoordinators(c *gin.Context) {
33	// Pagination
34	fromItem, order, limit, err := parsePagination(c)
35	if err != nil {
36		retBadReq(err, c)
37		return
38	}
39
40	// Fetch coordinators from historyDB
41	coordinators, pendingItems, err := a.h.GetCoordinatorsAPI(fromItem, limit, order)
42	if err != nil {
43		retSQLErr(err, c)
44		return
45	}
46
47	// Build succesfull response
48	type coordinatorsResponse struct {
49		Coordinators []historydb.CoordinatorAPI `json:"coordinators"`
50		PendingItems uint64                     `json:"pendingItems"`
51	}
52	c.JSON(http.StatusOK, &coordinatorsResponse{
53		Coordinators: coordinators,
54		PendingItems: pendingItems,
55	})
56}