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.

72 lines
1.4 KiB

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