arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
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}
 40
 41// NewAPI sets the endpoints and the appropriate handlers, but doesn't start the server
 42func NewAPI(
 43	coordinatorEndpoints, explorerEndpoints bool,
 44	server *gin.Engine,
 45	hdb *historydb.HistoryDB,
 46	sdb *statedb.StateDB,
 47	l2db *l2db.L2DB,
 48	config *Config,
 49) (*API, error) {
 50	// Check input
 51	// TODO: is stateDB only needed for explorer endpoints or for both?
 52	if coordinatorEndpoints && l2db == nil {
 53		return nil, tracerr.Wrap(errors.New("cannot serve Coordinator endpoints without L2DB"))
 54	}
 55	if explorerEndpoints && hdb == nil {
 56		return nil, tracerr.Wrap(errors.New("cannot serve Explorer endpoints without HistoryDB"))
 57	}
 58
 59	a := &API{
 60		h: hdb,
 61		cg: &configAPI{
 62			RollupConstants:   *newRollupConstants(config.RollupConstants),
 63			AuctionConstants:  config.AuctionConstants,
 64			WDelayerConstants: config.WDelayerConstants,
 65		},
 66		s:      sdb,
 67		l2:     l2db,
 68		status: Status{},
 69	}
 70
 71	// Add coordinator endpoints
 72	if coordinatorEndpoints {
 73		// Account
 74		server.POST("/account-creation-authorization", a.postAccountCreationAuth)
 75		server.GET("/account-creation-authorization/:hermezEthereumAddress", a.getAccountCreationAuth)
 76		// Transaction
 77		server.POST("/transactions-pool", a.postPoolTx)
 78		server.GET("/transactions-pool/:id", a.getPoolTx)
 79	}
 80
 81	// Add explorer endpoints
 82	if explorerEndpoints {
 83		// Account
 84		server.GET("/accounts", a.getAccounts)
 85		server.GET("/accounts/:accountIndex", a.getAccount)
 86		server.GET("/exits", a.getExits)
 87		server.GET("/exits/:batchNum/:accountIndex", a.getExit)
 88		// Transaction
 89		server.GET("/transactions-history", a.getHistoryTxs)
 90		server.GET("/transactions-history/:id", a.getHistoryTx)
 91		// Status
 92		server.GET("/batches", a.getBatches)
 93		server.GET("/batches/:batchNum", a.getBatch)
 94		server.GET("/full-batches/:batchNum", a.getFullBatch)
 95		server.GET("/slots", a.getSlots)
 96		server.GET("/slots/:slotNum", a.getSlot)
 97		server.GET("/bids", a.getBids)
 98		server.GET("/state", a.getState)
 99		server.GET("/config", a.getConfig)
100		server.GET("/tokens", a.getTokens)
101		server.GET("/tokens/:id", a.getToken)
102		server.GET("/coordinators", a.getCoordinators)
103		server.GET("/coordinators/:bidderAddr", a.getCoordinator)
104	}
105
106	return a, nil
107}