You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

  1. package api
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "github.com/hermeznetwork/hermez-node/common"
  6. "github.com/hermeznetwork/hermez-node/db/historydb"
  7. "github.com/hermeznetwork/hermez-node/db/l2db"
  8. "github.com/hermeznetwork/hermez-node/db/statedb"
  9. )
  10. // Status define status of the network
  11. type Status struct {
  12. Network Network `json:"network"`
  13. Metrics historydb.Metrics `json:"metrics"`
  14. Rollup common.RollupVariables `json:"rollup"`
  15. Auction common.AuctionVariables `json:"auction"`
  16. WithdrawalDelayer common.WDelayerVariables `json:"withdrawalDelayer"`
  17. RecommendedFee common.RecommendedFee `json:"recommendedFee"`
  18. }
  19. // API serves HTTP requests to allow external interaction with the Hermez node
  20. type API struct {
  21. h *historydb.HistoryDB
  22. cg *configAPI
  23. s *statedb.StateDB
  24. l2 *l2db.L2DB
  25. status Status
  26. }
  27. // NewAPI sets the endpoints and the appropriate handlers, but doesn't start the server
  28. func NewAPI(
  29. coordinatorEndpoints, explorerEndpoints bool,
  30. server *gin.Engine,
  31. hdb *historydb.HistoryDB,
  32. sdb *statedb.StateDB,
  33. l2db *l2db.L2DB,
  34. config *configAPI,
  35. ) (*API, error) {
  36. // Check input
  37. // TODO: is stateDB only needed for explorer endpoints or for both?
  38. if coordinatorEndpoints && l2db == nil {
  39. return nil, errors.New("cannot serve Coordinator endpoints without L2DB")
  40. }
  41. if explorerEndpoints && hdb == nil {
  42. return nil, errors.New("cannot serve Explorer endpoints without HistoryDB")
  43. }
  44. a := &API{
  45. h: hdb,
  46. cg: config,
  47. s: sdb,
  48. l2: l2db,
  49. }
  50. // Add coordinator endpoints
  51. if coordinatorEndpoints {
  52. // Account
  53. server.POST("/account-creation-authorization", a.postAccountCreationAuth)
  54. server.GET("/account-creation-authorization/:hermezEthereumAddress", a.getAccountCreationAuth)
  55. // Transaction
  56. server.POST("/transactions-pool", a.postPoolTx)
  57. server.GET("/transactions-pool/:id", a.getPoolTx)
  58. }
  59. // Add explorer endpoints
  60. if explorerEndpoints {
  61. // Account
  62. server.GET("/accounts", a.getAccounts)
  63. server.GET("/accounts/:accountIndex", a.getAccount)
  64. server.GET("/exits", a.getExits)
  65. server.GET("/exits/:batchNum/:accountIndex", a.getExit)
  66. // Transaction
  67. server.GET("/transactions-history", a.getHistoryTxs)
  68. server.GET("/transactions-history/:id", a.getHistoryTx)
  69. // Status
  70. server.GET("/batches", a.getBatches)
  71. server.GET("/batches/:batchNum", a.getBatch)
  72. server.GET("/full-batches/:batchNum", a.getFullBatch)
  73. server.GET("/slots", a.getSlots)
  74. server.GET("/slots/:slotNum", a.getSlot)
  75. server.GET("/bids", a.getBids)
  76. server.GET("/state", a.getState)
  77. server.GET("/config", a.getConfig)
  78. server.GET("/tokens", a.getTokens)
  79. server.GET("/tokens/:id", a.getToken)
  80. server.GET("/coordinators", a.getCoordinators)
  81. server.GET("/coordinators/:bidderAddr", a.getCoordinator)
  82. }
  83. return a, nil
  84. }