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.

176 lines
3.4 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 postAccountCreationAuth(c *gin.Context) {
  24. // Parse body
  25. var apiAuth accountCreationAuthAPI
  26. if err := c.ShouldBindJSON(&apiAuth); err != nil {
  27. retBadReq(err, c)
  28. return
  29. }
  30. // API to common + verify signature
  31. dbAuth, err := accountCreationAuthAPIToCommon(&apiAuth)
  32. if err != nil {
  33. retBadReq(err, c)
  34. return
  35. }
  36. // Insert to DB
  37. if err := l2.AddAccountCreationAuth(dbAuth); err != nil {
  38. retSQLErr(err, c)
  39. return
  40. }
  41. // Return OK
  42. c.Status(http.StatusOK)
  43. }
  44. func getAccountCreationAuth(c *gin.Context) {
  45. // Get hezEthereumAddress
  46. addr, err := parseParamHezEthAddr(c)
  47. if err != nil {
  48. retBadReq(err, c)
  49. return
  50. }
  51. // Fetch auth from l2DB
  52. dbAuth, err := l2.GetAccountCreationAuth(*addr)
  53. if err != nil {
  54. retSQLErr(err, c)
  55. return
  56. }
  57. apiAuth := accountCreationAuthToAPI(dbAuth)
  58. // Build succesfull response
  59. c.JSON(http.StatusOK, apiAuth)
  60. }
  61. func getAccounts(c *gin.Context) {
  62. }
  63. func getAccount(c *gin.Context) {
  64. }
  65. func getExits(c *gin.Context) {
  66. // Get query parameters
  67. // Account filters
  68. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  69. if err != nil {
  70. retBadReq(err, c)
  71. return
  72. }
  73. // BatchNum
  74. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  75. if err != nil {
  76. retBadReq(err, c)
  77. return
  78. }
  79. // Pagination
  80. fromItem, order, limit, err := parsePagination(c)
  81. if err != nil {
  82. retBadReq(err, c)
  83. return
  84. }
  85. // Fetch exits from historyDB
  86. exits, pagination, err := h.GetExits(
  87. addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
  88. )
  89. if err != nil {
  90. retSQLErr(err, c)
  91. return
  92. }
  93. // Build succesfull response
  94. apiExits := historyExitsToAPI(exits)
  95. c.JSON(http.StatusOK, &exitsAPI{
  96. Exits: apiExits,
  97. Pagination: pagination,
  98. })
  99. }
  100. func getExit(c *gin.Context) {
  101. // Get batchNum and accountIndex
  102. batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
  103. if err != nil {
  104. retBadReq(err, c)
  105. return
  106. }
  107. idx, err := parseParamIdx(c)
  108. if err != nil {
  109. retBadReq(err, c)
  110. return
  111. }
  112. // Fetch tx from historyDB
  113. exit, err := h.GetExit(batchNum, idx)
  114. if err != nil {
  115. retSQLErr(err, c)
  116. return
  117. }
  118. apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
  119. // Build succesfull response
  120. c.JSON(http.StatusOK, apiExits[0])
  121. }
  122. func getSlots(c *gin.Context) {
  123. }
  124. func getNextForgers(c *gin.Context) {
  125. }
  126. func getState(c *gin.Context) {
  127. }
  128. func getConfig(c *gin.Context) {
  129. c.JSON(http.StatusOK, cg)
  130. }
  131. func getRecommendedFee(c *gin.Context) {
  132. }
  133. func retSQLErr(err error, c *gin.Context) {
  134. if err == sql.ErrNoRows {
  135. c.JSON(http.StatusNotFound, errorMsg{
  136. Message: err.Error(),
  137. })
  138. } else {
  139. c.JSON(http.StatusInternalServerError, errorMsg{
  140. Message: err.Error(),
  141. })
  142. }
  143. }
  144. func retBadReq(err error, c *gin.Context) {
  145. c.JSON(http.StatusBadRequest, errorMsg{
  146. Message: err.Error(),
  147. })
  148. }