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.

136 lines
2.6 KiB

  1. package api
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. )
  9. const (
  10. // maxLimit is the max permited items to be returned in paginated responses
  11. maxLimit uint = 2049
  12. // dfltOrder indicates how paginated endpoints are ordered if not specified
  13. dfltOrder = historydb.OrderAsc
  14. // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
  15. dfltLimit uint = 20
  16. // 2^32 -1
  17. maxUint32 = 4294967295
  18. )
  19. var (
  20. // ErrNillBidderAddr is used when a nil bidderAddr is received in the getCoordinator method
  21. ErrNillBidderAddr = errors.New("biderAddr can not be nil")
  22. )
  23. func getAccounts(c *gin.Context) {
  24. }
  25. func getAccount(c *gin.Context) {
  26. }
  27. func getExits(c *gin.Context) {
  28. // Get query parameters
  29. // Account filters
  30. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  31. if err != nil {
  32. retBadReq(err, c)
  33. return
  34. }
  35. // BatchNum
  36. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  37. if err != nil {
  38. retBadReq(err, c)
  39. return
  40. }
  41. // Pagination
  42. fromItem, order, limit, err := parsePagination(c)
  43. if err != nil {
  44. retBadReq(err, c)
  45. return
  46. }
  47. // Fetch exits from historyDB
  48. exits, pagination, err := h.GetExits(
  49. addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
  50. )
  51. if err != nil {
  52. retSQLErr(err, c)
  53. return
  54. }
  55. // Build succesfull response
  56. apiExits := historyExitsToAPI(exits)
  57. c.JSON(http.StatusOK, &exitsAPI{
  58. Exits: apiExits,
  59. Pagination: pagination,
  60. })
  61. }
  62. func getExit(c *gin.Context) {
  63. // Get batchNum and accountIndex
  64. batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
  65. if err != nil {
  66. retBadReq(err, c)
  67. return
  68. }
  69. idx, err := parseParamIdx(c)
  70. if err != nil {
  71. retBadReq(err, c)
  72. return
  73. }
  74. // Fetch tx from historyDB
  75. exit, err := h.GetExit(batchNum, idx)
  76. if err != nil {
  77. retSQLErr(err, c)
  78. return
  79. }
  80. apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
  81. // Build succesfull response
  82. c.JSON(http.StatusOK, apiExits[0])
  83. }
  84. func getSlots(c *gin.Context) {
  85. }
  86. func getNextForgers(c *gin.Context) {
  87. }
  88. func getState(c *gin.Context) {
  89. }
  90. func getConfig(c *gin.Context) {
  91. c.JSON(http.StatusOK, cg)
  92. }
  93. func getRecommendedFee(c *gin.Context) {
  94. }
  95. func retSQLErr(err error, c *gin.Context) {
  96. if err == sql.ErrNoRows {
  97. c.JSON(http.StatusNotFound, errorMsg{
  98. Message: err.Error(),
  99. })
  100. } else {
  101. c.JSON(http.StatusInternalServerError, errorMsg{
  102. Message: err.Error(),
  103. })
  104. }
  105. }
  106. func retBadReq(err error, c *gin.Context) {
  107. c.JSON(http.StatusBadRequest, errorMsg{
  108. Message: err.Error(),
  109. })
  110. }