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.

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