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.

280 lines
5.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/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. c.JSON(http.StatusOK, cg)
  161. }
  162. func getTokens(c *gin.Context) {
  163. // Account filters
  164. tokenIDs, symbols, name, err := parseTokenFilters(c)
  165. if err != nil {
  166. retBadReq(err, c)
  167. return
  168. }
  169. // Pagination
  170. fromItem, order, limit, err := parsePagination(c)
  171. if err != nil {
  172. retBadReq(err, c)
  173. return
  174. }
  175. // Fetch exits from historyDB
  176. tokens, pagination, err := h.GetTokens(
  177. tokenIDs, symbols, name, fromItem, limit, order,
  178. )
  179. if err != nil {
  180. retSQLErr(err, c)
  181. return
  182. }
  183. // Build succesfull response
  184. apiTokens := tokensToAPI(tokens)
  185. c.JSON(http.StatusOK, &tokensAPI{
  186. Tokens: apiTokens,
  187. Pagination: pagination,
  188. })
  189. }
  190. func getToken(c *gin.Context) {
  191. // Get TokenID
  192. tokenIDUint, err := parseParamUint("id", nil, 0, maxUint32, c)
  193. if err != nil {
  194. retBadReq(err, c)
  195. return
  196. }
  197. tokenID := common.TokenID(*tokenIDUint)
  198. // Fetch token from historyDB
  199. token, err := h.GetToken(tokenID)
  200. if err != nil {
  201. retSQLErr(err, c)
  202. return
  203. }
  204. apiToken := tokensToAPI([]historydb.TokenRead{*token})
  205. c.JSON(http.StatusOK, apiToken[0])
  206. }
  207. func getRecommendedFee(c *gin.Context) {
  208. }
  209. func getCoordinators(c *gin.Context) {
  210. }
  211. func getCoordinator(c *gin.Context) {
  212. }
  213. func retSQLErr(err error, c *gin.Context) {
  214. if err == sql.ErrNoRows {
  215. c.JSON(http.StatusNotFound, errorMsg{
  216. Message: err.Error(),
  217. })
  218. } else {
  219. c.JSON(http.StatusInternalServerError, errorMsg{
  220. Message: err.Error(),
  221. })
  222. }
  223. }
  224. func retBadReq(err error, c *gin.Context) {
  225. c.JSON(http.StatusBadRequest, errorMsg{
  226. Message: err.Error(),
  227. })
  228. }