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