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