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.

239 lines
4.0 KiB

  1. package api
  2. import (
  3. "database/sql"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/hermeznetwork/hermez-node/db/historydb"
  7. )
  8. const (
  9. // maxLimit is the max permited items to be returned in paginated responses
  10. maxLimit uint = 2049
  11. // dfltOrder indicates how paginated endpoints are ordered if not specified
  12. dfltOrder = historydb.OrderAsc
  13. // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
  14. dfltLimit uint = 20
  15. // 2^32 -1
  16. maxUint32 = 4294967295
  17. )
  18. func postAccountCreationAuth(c *gin.Context) {
  19. }
  20. func getAccountCreationAuth(c *gin.Context) {
  21. }
  22. func postPoolTx(c *gin.Context) {
  23. }
  24. func getPoolTx(c *gin.Context) {
  25. }
  26. func getAccounts(c *gin.Context) {
  27. }
  28. func getAccount(c *gin.Context) {
  29. }
  30. func getExits(c *gin.Context) {
  31. // Get query parameters
  32. // Account filters
  33. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  34. if err != nil {
  35. retBadReq(err, c)
  36. return
  37. }
  38. // BatchNum
  39. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  40. if err != nil {
  41. retBadReq(err, c)
  42. return
  43. }
  44. // Pagination
  45. fromItem, order, limit, err := parsePagination(c)
  46. if err != nil {
  47. retBadReq(err, c)
  48. return
  49. }
  50. // Fetch exits from historyDB
  51. exits, pagination, err := h.GetExits(
  52. addr, bjj, tokenID, idx, batchNum, fromItem, limit, order,
  53. )
  54. if err != nil {
  55. retSQLErr(err, c)
  56. return
  57. }
  58. // Build succesfull response
  59. apiExits := historyExitsToAPI(exits)
  60. c.JSON(http.StatusOK, &exitsAPI{
  61. Exits: apiExits,
  62. Pagination: pagination,
  63. })
  64. }
  65. func getExit(c *gin.Context) {
  66. // Get batchNum and accountIndex
  67. batchNum, err := parseParamUint("batchNum", nil, 0, maxUint32, c)
  68. if err != nil {
  69. retBadReq(err, c)
  70. return
  71. }
  72. idx, err := parseParamIdx(c)
  73. if err != nil {
  74. retBadReq(err, c)
  75. return
  76. }
  77. // Fetch tx from historyDB
  78. exit, err := h.GetExit(batchNum, idx)
  79. if err != nil {
  80. retSQLErr(err, c)
  81. return
  82. }
  83. apiExits := historyExitsToAPI([]historydb.HistoryExit{*exit})
  84. // Build succesfull response
  85. c.JSON(http.StatusOK, apiExits[0])
  86. }
  87. func getHistoryTxs(c *gin.Context) {
  88. // Get query parameters
  89. tokenID, addr, bjj, idx, err := parseAccountFilters(c)
  90. if err != nil {
  91. retBadReq(err, c)
  92. return
  93. }
  94. // BatchNum
  95. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  96. if err != nil {
  97. retBadReq(err, c)
  98. return
  99. }
  100. // TxType
  101. txType, err := parseQueryTxType(c)
  102. if err != nil {
  103. retBadReq(err, c)
  104. return
  105. }
  106. // Pagination
  107. fromItem, order, limit, err := parsePagination(c)
  108. if err != nil {
  109. retBadReq(err, c)
  110. return
  111. }
  112. // Fetch txs from historyDB
  113. txs, pagination, err := h.GetHistoryTxs(
  114. addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
  115. )
  116. if err != nil {
  117. retSQLErr(err, c)
  118. return
  119. }
  120. // Build succesfull response
  121. apiTxs := historyTxsToAPI(txs)
  122. c.JSON(http.StatusOK, &historyTxsAPI{
  123. Txs: apiTxs,
  124. Pagination: pagination,
  125. })
  126. }
  127. func getHistoryTx(c *gin.Context) {
  128. // Get TxID
  129. txID, err := parseParamTxID(c)
  130. if err != nil {
  131. retBadReq(err, c)
  132. return
  133. }
  134. // Fetch tx from historyDB
  135. tx, err := h.GetHistoryTx(txID)
  136. if err != nil {
  137. retSQLErr(err, c)
  138. return
  139. }
  140. apiTxs := historyTxsToAPI([]historydb.HistoryTx{*tx})
  141. // Build succesfull response
  142. c.JSON(http.StatusOK, apiTxs[0])
  143. }
  144. func getBatches(c *gin.Context) {
  145. }
  146. func getBatch(c *gin.Context) {
  147. }
  148. func getFullBatch(c *gin.Context) {
  149. }
  150. func getSlots(c *gin.Context) {
  151. }
  152. func getBids(c *gin.Context) {
  153. }
  154. func getNextForgers(c *gin.Context) {
  155. }
  156. func getState(c *gin.Context) {
  157. }
  158. func getConfig(c *gin.Context) {
  159. }
  160. func getTokens(c *gin.Context) {
  161. }
  162. func getToken(c *gin.Context) {
  163. }
  164. func getRecommendedFee(c *gin.Context) {
  165. }
  166. func getCoordinators(c *gin.Context) {
  167. }
  168. func getCoordinator(c *gin.Context) {
  169. }
  170. func retSQLErr(err error, c *gin.Context) {
  171. if err == sql.ErrNoRows {
  172. c.JSON(http.StatusNotFound, errorMsg{
  173. Message: err.Error(),
  174. })
  175. } else {
  176. c.JSON(http.StatusInternalServerError, errorMsg{
  177. Message: err.Error(),
  178. })
  179. }
  180. }
  181. func retBadReq(err error, c *gin.Context) {
  182. c.JSON(http.StatusBadRequest, errorMsg{
  183. Message: err.Error(),
  184. })
  185. }