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.

78 lines
2.3 KiB

  1. package api
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. "github.com/hermeznetwork/hermez-node/db/historydb"
  6. "github.com/hermeznetwork/hermez-node/db/l2db"
  7. "github.com/hermeznetwork/hermez-node/db/statedb"
  8. )
  9. var h *historydb.HistoryDB
  10. var cg *configAPI
  11. var s *statedb.StateDB
  12. var l2 *l2db.L2DB
  13. // SetAPIEndpoints sets the endpoints and the appropriate handlers, but doesn't start the server
  14. func SetAPIEndpoints(
  15. coordinatorEndpoints, explorerEndpoints bool,
  16. server *gin.Engine,
  17. hdb *historydb.HistoryDB,
  18. sdb *statedb.StateDB,
  19. l2db *l2db.L2DB,
  20. config *configAPI,
  21. ) error {
  22. // Check input
  23. // TODO: is stateDB only needed for explorer endpoints or for both?
  24. if coordinatorEndpoints && l2db == nil {
  25. return errors.New("cannot serve Coordinator endpoints without L2DB")
  26. }
  27. if explorerEndpoints && hdb == nil {
  28. return errors.New("cannot serve Explorer endpoints without HistoryDB")
  29. }
  30. h = hdb
  31. cg = config
  32. s = sdb
  33. l2 = l2db
  34. // Add coordinator endpoints
  35. if coordinatorEndpoints {
  36. // Account
  37. server.POST("/account-creation-authorization", postAccountCreationAuth)
  38. server.GET("/account-creation-authorization/:hermezEthereumAddress", getAccountCreationAuth)
  39. // Transaction
  40. server.POST("/transactions-pool", postPoolTx)
  41. server.GET("/transactions-pool/:id", getPoolTx)
  42. }
  43. // Add explorer endpoints
  44. if explorerEndpoints {
  45. // Account
  46. server.GET("/accounts", getAccounts)
  47. server.GET("/accounts/:hermezEthereumAddress/:accountIndex", getAccount)
  48. server.GET("/exits", getExits)
  49. server.GET("/exits/:batchNum/:accountIndex", getExit)
  50. // Transaction
  51. server.GET("/transactions-history", getHistoryTxs)
  52. server.GET("/transactions-history/:id", getHistoryTx)
  53. // Status
  54. server.GET("/batches", getBatches)
  55. server.GET("/batches/:batchNum", getBatch)
  56. server.GET("/full-batches/:batchNum", getFullBatch)
  57. server.GET("/slots", getSlots)
  58. server.GET("/slots/:slotNum", getSlot)
  59. server.GET("/bids", getBids)
  60. server.GET("/next-forgers", getNextForgers)
  61. server.GET("/state", getState)
  62. server.GET("/config", getConfig)
  63. server.GET("/tokens", getTokens)
  64. server.GET("/tokens/:id", getToken)
  65. server.GET("/recommendedFee", getRecommendedFee)
  66. server.GET("/coordinators", getCoordinators)
  67. server.GET("/coordinators/:bidderAddr", getCoordinator)
  68. }
  69. return nil
  70. }