arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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 *Config,
 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: &configAPI{
 59			RollupConstants:   *newRollupConstants(config.RollupConstants),
 60			AuctionConstants:  config.AuctionConstants,
 61			WDelayerConstants: config.WDelayerConstants,
 62		},
 63		s:  sdb,
 64		l2: l2db,
 65	}
 66
 67	// Add coordinator endpoints
 68	if coordinatorEndpoints {
 69		// Account
 70		server.POST("/account-creation-authorization", a.postAccountCreationAuth)
 71		server.GET("/account-creation-authorization/:hermezEthereumAddress", a.getAccountCreationAuth)
 72		// Transaction
 73		server.POST("/transactions-pool", a.postPoolTx)
 74		server.GET("/transactions-pool/:id", a.getPoolTx)
 75	}
 76
 77	// Add explorer endpoints
 78	if explorerEndpoints {
 79		// Account
 80		server.GET("/accounts", a.getAccounts)
 81		server.GET("/accounts/:accountIndex", a.getAccount)
 82		server.GET("/exits", a.getExits)
 83		server.GET("/exits/:batchNum/:accountIndex", a.getExit)
 84		// Transaction
 85		server.GET("/transactions-history", a.getHistoryTxs)
 86		server.GET("/transactions-history/:id", a.getHistoryTx)
 87		// Status
 88		server.GET("/batches", a.getBatches)
 89		server.GET("/batches/:batchNum", a.getBatch)
 90		server.GET("/full-batches/:batchNum", a.getFullBatch)
 91		server.GET("/slots", a.getSlots)
 92		server.GET("/slots/:slotNum", a.getSlot)
 93		server.GET("/bids", a.getBids)
 94		server.GET("/state", a.getState)
 95		server.GET("/config", a.getConfig)
 96		server.GET("/tokens", a.getTokens)
 97		server.GET("/tokens/:id", a.getToken)
 98		server.GET("/coordinators", a.getCoordinators)
 99		server.GET("/coordinators/:bidderAddr", a.getCoordinator)
100	}
101
102	return a, nil
103}