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