arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/api.go
api/api.goRaw
 1package api
 2
 3import (
 4	"errors"
 5
 6	ethCommon "github.com/ethereum/go-ethereum/common"
 7	"github.com/gin-gonic/gin"
 8	"github.com/hermeznetwork/hermez-node/db/historydb"
 9	"github.com/hermeznetwork/hermez-node/db/l2db"
10	"github.com/hermeznetwork/hermez-node/metric"
11	"github.com/hermeznetwork/tracerr"
12)
13
14// API serves HTTP requests to allow external interaction with the Hermez node
15type API struct {
16	h             *historydb.HistoryDB
17	cg            *configAPI
18	l2            *l2db.L2DB
19	chainID       uint16
20	hermezAddress ethCommon.Address
21}
22
23// NewAPI sets the endpoints and the appropriate handlers, but doesn't start the server
24func NewAPI(
25	coordinatorEndpoints, explorerEndpoints bool,
26	server *gin.Engine,
27	hdb *historydb.HistoryDB,
28	l2db *l2db.L2DB,
29) (*API, error) {
30	// Check input
31	// TODO: is stateDB only needed for explorer endpoints or for both?
32	if coordinatorEndpoints && l2db == nil {
33		return nil, tracerr.Wrap(errors.New("cannot serve Coordinator endpoints without L2DB"))
34	}
35	if explorerEndpoints && hdb == nil {
36		return nil, tracerr.Wrap(errors.New("cannot serve Explorer endpoints without HistoryDB"))
37	}
38	consts, err := hdb.GetConstants()
39	if err != nil {
40		return nil, err
41	}
42	a := &API{
43		h: hdb,
44		cg: &configAPI{
45			RollupConstants:   *newRollupConstants(consts.Rollup),
46			AuctionConstants:  consts.Auction,
47			WDelayerConstants: consts.WDelayer,
48		},
49		l2:            l2db,
50		chainID:       consts.ChainID,
51		hermezAddress: consts.HermezAddress,
52	}
53
54	middleware, err := metric.PrometheusMiddleware()
55	if err != nil {
56		return nil, err
57	}
58	server.Use(middleware)
59
60	v1 := server.Group("/v1")
61
62	// Add coordinator endpoints
63	if coordinatorEndpoints {
64		// Account
65		v1.POST("/account-creation-authorization", a.postAccountCreationAuth)
66		v1.GET("/account-creation-authorization/:hezEthereumAddress", a.getAccountCreationAuth)
67		// Transaction
68		v1.POST("/transactions-pool", a.postPoolTx)
69		v1.GET("/transactions-pool/:id", a.getPoolTx)
70	}
71
72	// Add explorer endpoints
73	if explorerEndpoints {
74		// Account
75		v1.GET("/accounts", a.getAccounts)
76		v1.GET("/accounts/:accountIndex", a.getAccount)
77		v1.GET("/exits", a.getExits)
78		v1.GET("/exits/:batchNum/:accountIndex", a.getExit)
79		// Transaction
80		v1.GET("/transactions-history", a.getHistoryTxs)
81		v1.GET("/transactions-history/:id", a.getHistoryTx)
82		// Status
83		v1.GET("/batches", a.getBatches)
84		v1.GET("/batches/:batchNum", a.getBatch)
85		v1.GET("/full-batches/:batchNum", a.getFullBatch)
86		v1.GET("/slots", a.getSlots)
87		v1.GET("/slots/:slotNum", a.getSlot)
88		v1.GET("/bids", a.getBids)
89		v1.GET("/state", a.getState)
90		v1.GET("/config", a.getConfig)
91		v1.GET("/tokens", a.getTokens)
92		v1.GET("/tokens/:id", a.getToken)
93		v1.GET("/coordinators", a.getCoordinators)
94	}
95
96	return a, nil
97}