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.

71 lines
1.4 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. // Pagination
  27. fromItem, order, limit, err := parsePagination(c)
  28. if err != nil {
  29. retBadReq(err, c)
  30. return
  31. }
  32. // Fetch txs from historyDB
  33. txs, pendingItems, err := a.h.GetHistoryTxs(
  34. addr, bjj, tokenID, idx, batchNum, txType, fromItem, limit, order,
  35. )
  36. if err != nil {
  37. retSQLErr(err, c)
  38. return
  39. }
  40. // Build succesfull response
  41. type txsResponse struct {
  42. Txs []historydb.TxAPI `json:"transactions"`
  43. PendingItems uint64 `json:"pendingItems"`
  44. }
  45. c.JSON(http.StatusOK, &txsResponse{
  46. Txs: txs,
  47. PendingItems: pendingItems,
  48. })
  49. }
  50. func (a *API) getHistoryTx(c *gin.Context) {
  51. // Get TxID
  52. txID, err := parseParamTxID(c)
  53. if err != nil {
  54. retBadReq(err, c)
  55. return
  56. }
  57. // Fetch tx from historyDB
  58. tx, err := a.h.GetHistoryTx(txID)
  59. if err != nil {
  60. retSQLErr(err, c)
  61. return
  62. }
  63. // Build succesfull response
  64. c.JSON(http.StatusOK, tx)
  65. }