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.

204 lines
3.2 KiB

  1. package api
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // maxLimit is the max permited items to be returned in paginated responses
  9. const maxLimit uint = 2049
  10. // dfltLast indicates how paginated endpoints use the query param last if not provided
  11. const dfltLast = false
  12. // dfltLimit indicates the limit of returned items in paginated responses if the query param limit is not provided
  13. const dfltLimit uint = 20
  14. // 2^32 -1
  15. const maxUint32 = 4294967295
  16. func postAccountCreationAuth(c *gin.Context) {
  17. }
  18. func getAccountCreationAuth(c *gin.Context) {
  19. }
  20. func postPoolTx(c *gin.Context) {
  21. }
  22. func getPoolTx(c *gin.Context) {
  23. }
  24. func getAccounts(c *gin.Context) {
  25. }
  26. func getAccount(c *gin.Context) {
  27. }
  28. func getExits(c *gin.Context) {
  29. }
  30. func getExit(c *gin.Context) {
  31. }
  32. func getHistoryTxs(c *gin.Context) {
  33. // Get query parameters
  34. // TokenID
  35. tokenID, err := parseQueryUint("tokenId", nil, 0, maxUint32, c)
  36. if err != nil {
  37. retBadReq(err, c)
  38. return
  39. }
  40. // Hez Eth addr
  41. addr, err := parseQueryHezEthAddr(c)
  42. if err != nil {
  43. retBadReq(err, c)
  44. return
  45. }
  46. // BJJ
  47. bjj, err := parseQueryBJJ(c)
  48. if err != nil {
  49. retBadReq(err, c)
  50. return
  51. }
  52. if addr != nil && bjj != nil {
  53. retBadReq(errors.New("bjj and hermezEthereumAddress params are incompatible"), c)
  54. return
  55. }
  56. // Idx
  57. idx, err := parseIdx(c)
  58. if err != nil {
  59. retBadReq(err, c)
  60. return
  61. }
  62. if idx != nil && (addr != nil || bjj != nil || tokenID != nil) {
  63. retBadReq(errors.New("accountIndex is incompatible with BJJ, hermezEthereumAddress and tokenId"), c)
  64. return
  65. }
  66. // BatchNum
  67. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  68. if err != nil {
  69. retBadReq(err, c)
  70. return
  71. }
  72. // TxType
  73. txType, err := parseQueryTxType(c)
  74. if err != nil {
  75. retBadReq(err, c)
  76. return
  77. }
  78. // Pagination
  79. offset, last, limit, err := parsePagination(c)
  80. if err != nil {
  81. retBadReq(err, c)
  82. return
  83. }
  84. // Fetch txs from historyDB
  85. txs, totalItems, err := h.GetHistoryTxs(
  86. addr, bjj, tokenID, idx, batchNum, txType, offset, limit, *last,
  87. )
  88. if err != nil {
  89. retSQLErr(err, c)
  90. return
  91. }
  92. // Build succesfull response
  93. apiTxs := historyTxsToAPI(txs)
  94. lastRet := int(*offset) + len(apiTxs) - 1
  95. if *last {
  96. lastRet = totalItems - 1
  97. }
  98. c.JSON(http.StatusOK, &historyTxsAPI{
  99. Txs: apiTxs,
  100. Pagination: pagination{
  101. TotalItems: totalItems,
  102. LastReturnedItem: lastRet,
  103. },
  104. })
  105. }
  106. func getHistoryTx(c *gin.Context) {
  107. }
  108. func getBatches(c *gin.Context) {
  109. }
  110. func getBatch(c *gin.Context) {
  111. }
  112. func getFullBatch(c *gin.Context) {
  113. }
  114. func getSlots(c *gin.Context) {
  115. }
  116. func getBids(c *gin.Context) {
  117. }
  118. func getNextForgers(c *gin.Context) {
  119. }
  120. func getState(c *gin.Context) {
  121. }
  122. func getConfig(c *gin.Context) {
  123. }
  124. func getTokens(c *gin.Context) {
  125. }
  126. func getToken(c *gin.Context) {
  127. }
  128. func getRecommendedFee(c *gin.Context) {
  129. }
  130. func getCoordinators(c *gin.Context) {
  131. }
  132. func getCoordinator(c *gin.Context) {
  133. }
  134. func retSQLErr(err error, c *gin.Context) {
  135. if err == sql.ErrNoRows {
  136. c.JSON(http.StatusNotFound, errorMsg{
  137. Message: err.Error(),
  138. })
  139. } else {
  140. c.JSON(http.StatusInternalServerError, errorMsg{
  141. Message: err.Error(),
  142. })
  143. }
  144. }
  145. func retBadReq(err error, c *gin.Context) {
  146. c.JSON(http.StatusBadRequest, errorMsg{
  147. Message: err.Error(),
  148. })
  149. }