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