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