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/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	NodeConfig        NodeConfig                    `json:"nodeConfig"`
 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	l2            *l2db.L2DB
 38	status        Status
 39	chainID       uint16
 40	hermezAddress ethCommon.Address
 41}
 42
 43// NewAPI sets the endpoints and the appropriate handlers, but doesn't start the server
 44func NewAPI(
 45	coordinatorEndpoints, explorerEndpoints bool,
 46	server *gin.Engine,
 47	hdb *historydb.HistoryDB,
 48	l2db *l2db.L2DB,
 49	config *Config,
 50	nodeConfig *NodeConfig,
 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		l2: l2db,
 69		status: Status{
 70			NodeConfig: *nodeConfig,
 71		},
 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}