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