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