arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/api.go
api/api.goRaw
 1package api
 2
 3import (
 4	"errors"
 5
 6	"github.com/gin-gonic/gin"
 7	"github.com/hermeznetwork/hermez-node/db/historydb"
 8	"github.com/hermeznetwork/hermez-node/db/l2db"
 9	"github.com/hermeznetwork/hermez-node/db/statedb"
10)
11
12var h *historydb.HistoryDB
13var cg *configAPI
14
15var s *statedb.StateDB
16var l2 *l2db.L2DB
17
18// SetAPIEndpoints sets the endpoints and the appropriate handlers, but doesn't start the server
19func SetAPIEndpoints(
20	coordinatorEndpoints, explorerEndpoints bool,
21	server *gin.Engine,
22	hdb *historydb.HistoryDB,
23	sdb *statedb.StateDB,
24	l2db *l2db.L2DB,
25	config *configAPI,
26) error {
27	// Check input
28	// TODO: is stateDB only needed for explorer endpoints or for both?
29	if coordinatorEndpoints && l2db == nil {
30		return errors.New("cannot serve Coordinator endpoints without L2DB")
31	}
32	if explorerEndpoints && hdb == nil {
33		return errors.New("cannot serve Explorer endpoints without HistoryDB")
34	}
35
36	h = hdb
37	cg = config
38	s = sdb
39	l2 = l2db
40
41	// Add coordinator endpoints
42	if coordinatorEndpoints {
43		// Account
44		server.POST("/account-creation-authorization", postAccountCreationAuth)
45		server.GET("/account-creation-authorization/:hermezEthereumAddress", getAccountCreationAuth)
46		// Transaction
47		server.POST("/transactions-pool", postPoolTx)
48		server.GET("/transactions-pool/:id", getPoolTx)
49	}
50
51	// Add explorer endpoints
52	if explorerEndpoints {
53		// Account
54		server.GET("/accounts", getAccounts)
55		server.GET("/accounts/:hermezEthereumAddress/:accountIndex", getAccount)
56		server.GET("/exits", getExits)
57		server.GET("/exits/:batchNum/:accountIndex", getExit)
58		// Transaction
59		server.GET("/transactions-history", getHistoryTxs)
60		server.GET("/transactions-history/:id", getHistoryTx)
61		// Status
62		server.GET("/batches", getBatches)
63		server.GET("/batches/:batchNum", getBatch)
64		server.GET("/full-batches/:batchNum", getFullBatch)
65		server.GET("/slots", getSlots)
66		server.GET("/bids", getBids)
67		server.GET("/next-forgers", getNextForgers)
68		server.GET("/state", getState)
69		server.GET("/config", getConfig)
70		server.GET("/tokens", getTokens)
71		server.GET("/tokens/:id", getToken)
72		server.GET("/recommendedFee", getRecommendedFee)
73		server.GET("/coordinators", getCoordinators)
74		server.GET("/coordinators/:bidderAddr", getCoordinator)
75	}
76
77	return nil
78}