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