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