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.

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