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.

253 lines
4.4 KiB

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