arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/api.go
api/api.goRaw
 1package api
 2
 3import (
 4	"errors"
 5	"fmt"
 6
 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/db/statedb"
11)
12
13var h *historydb.HistoryDB
14var s *statedb.StateDB // Not 100% sure if this is needed
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) 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	// tmp
39	fmt.Println(h, s, l2)
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.POST("/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/:forgerAddr", getCoordinator)
74	}
75
76	return nil
77}