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.

66 lines
1.4 KiB

  1. package api
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/hermeznetwork/hermez-node/common"
  7. "github.com/hermeznetwork/hermez-node/db"
  8. "github.com/hermeznetwork/hermez-node/db/historydb"
  9. )
  10. func (a *API) getToken(c *gin.Context) {
  11. // Get TokenID
  12. tokenIDUint, err := parseParamUint("id", nil, 0, maxUint32, c)
  13. if err != nil {
  14. retBadReq(err, c)
  15. return
  16. }
  17. if tokenIDUint == nil { // tokenID is required
  18. retBadReq(errors.New("Invalid tokenID"), c)
  19. return
  20. }
  21. tokenID := common.TokenID(*tokenIDUint)
  22. // Fetch token from historyDB
  23. token, err := a.h.GetToken(tokenID)
  24. if err != nil {
  25. retSQLErr(err, c)
  26. return
  27. }
  28. c.JSON(http.StatusOK, token)
  29. }
  30. func (a *API) getTokens(c *gin.Context) {
  31. // Account filters
  32. tokenIDs, symbols, name, err := parseTokenFilters(c)
  33. if err != nil {
  34. retBadReq(err, c)
  35. return
  36. }
  37. // Pagination
  38. fromItem, order, limit, err := parsePagination(c)
  39. if err != nil {
  40. retBadReq(err, c)
  41. return
  42. }
  43. // Fetch exits from historyDB
  44. tokens, pagination, err := a.h.GetTokens(
  45. tokenIDs, symbols, name, fromItem, limit, order,
  46. )
  47. if err != nil {
  48. retSQLErr(err, c)
  49. return
  50. }
  51. // Build succesfull response
  52. type tokensResponse struct {
  53. Tokens []historydb.TokenWithUSD `json:"tokens"`
  54. Pagination *db.Pagination `json:"pagination"`
  55. }
  56. c.JSON(http.StatusOK, &tokensResponse{
  57. Tokens: tokens,
  58. Pagination: pagination,
  59. })
  60. }