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