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	Network           Network                       `json:"network"`
 25	Metrics           historydb.Metrics             `json:"metrics"`
 26	Rollup            historydb.RollupVariablesAPI  `json:"rollup"`
 27	Auction           historydb.AuctionVariablesAPI `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	l2            *l2db.L2DB
 37	status        Status
 38	chainID       uint16
 39	hermezAddress ethCommon.Address
 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	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		l2:            l2db,
 67		status:        Status{},
 68		chainID:       config.ChainID,
 69		hermezAddress: config.HermezAddress,
 70	}
 71
 72	// Add coordinator endpoints
 73	if coordinatorEndpoints {
 74		// Account
 75		server.POST("/account-creation-authorization", a.postAccountCreationAuth)
 76		server.GET("/account-creation-authorization/:hezEthereumAddress", a.getAccountCreationAuth)
 77		// Transaction
 78		server.POST("/transactions-pool", a.postPoolTx)
 79		server.GET("/transactions-pool/:id", a.getPoolTx)
 80	}
 81
 82	// Add explorer endpoints
 83	if explorerEndpoints {
 84		// Account
 85		server.GET("/accounts", a.getAccounts)
 86		server.GET("/accounts/:accountIndex", a.getAccount)
 87		server.GET("/exits", a.getExits)
 88		server.GET("/exits/:batchNum/:accountIndex", a.getExit)
 89		// Transaction
 90		server.GET("/transactions-history", a.getHistoryTxs)
 91		server.GET("/transactions-history/:id", a.getHistoryTx)
 92		// Status
 93		server.GET("/batches", a.getBatches)
 94		server.GET("/batches/:batchNum", a.getBatch)
 95		server.GET("/full-batches/:batchNum", a.getFullBatch)
 96		server.GET("/slots", a.getSlots)
 97		server.GET("/slots/:slotNum", a.getSlot)
 98		server.GET("/bids", a.getBids)
 99		server.GET("/state", a.getState)
100		server.GET("/config", a.getConfig)
101		server.GET("/tokens", a.getTokens)
102		server.GET("/tokens/:id", a.getToken)
103		server.GET("/coordinators", a.getCoordinators)
104	}
105
106	return a, nil
107}