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.

79 lines
1.6 KiB

4 years ago
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/hermeznetwork/hermez-node/db/historydb"
  6. )
  7. func (a *API) getHistoryTxs(c *gin.Context) {
  8. // Get query parameters
  9. tokenID, addr, bjj, idx, err := parseExitFilters(c)
  10. if err != nil {
  11. retBadReq(err, c)
  12. return
  13. }
  14. // BatchNum
  15. batchNum, err := parseQueryUint("batchNum", nil, 0, maxUint32, c)
  16. if err != nil {
  17. retBadReq(err, c)
  18. return
  19. }
  20. // TxType
  21. txType, err := parseQueryTxType(c)
  22. if err != nil {
  23. retBadReq(err, c)
  24. return
  25. }
  26. // IncludePendingL1s
  27. includePendingL1s := new(bool)
  28. *includePendingL1s = false
  29. includePendingL1s, err = parseQueryBool("includePendingL1s", includePendingL1s, c)
  30. if err != nil {
  31. retBadReq(err, c)
  32. return
  33. }
  34. // Pagination
  35. fromItem, order, limit, err := parsePagination(c)
  36. if err != nil {
  37. retBadReq(err, c)
  38. return
  39. }
  40. // Fetch txs from historyDB
  41. txs, pendingItems, err := a.h.GetTxsAPI(
  42. addr, bjj, tokenID, idx, batchNum, txType, includePendingL1s, fromItem, limit, order,
  43. )
  44. if err != nil {
  45. retSQLErr(err, c)
  46. return
  47. }
  48. // Build successful response
  49. type txsResponse struct {
  50. Txs []historydb.TxAPI `json:"transactions"`
  51. PendingItems uint64 `json:"pendingItems"`
  52. }
  53. c.JSON(http.StatusOK, &txsResponse{
  54. Txs: txs,
  55. PendingItems: pendingItems,
  56. })
  57. }
  58. func (a *API) getHistoryTx(c *gin.Context) {
  59. // Get TxID
  60. txID, err := parseParamTxID(c)
  61. if err != nil {
  62. retBadReq(err, c)
  63. return
  64. }
  65. // Fetch tx from historyDB
  66. tx, err := a.h.GetTxAPI(txID)
  67. if err != nil {
  68. retSQLErr(err, c)
  69. return
  70. }
  71. // Build successful response
  72. c.JSON(http.StatusOK, tx)
  73. }