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.

82 lines
1.7 KiB

4 years ago
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/hermeznetwork/hermez-node/apitypes"
  6. "github.com/hermeznetwork/hermez-node/db"
  7. "github.com/hermeznetwork/hermez-node/db/historydb"
  8. )
  9. func getAccount(c *gin.Context) {
  10. // Get Addr
  11. idx, err := parseParamIdx(c)
  12. if err != nil {
  13. retBadReq(err, c)
  14. return
  15. }
  16. apiAccount, err := h.GetAccountAPI(*idx)
  17. if err != nil {
  18. retSQLErr(err, c)
  19. return
  20. }
  21. // Get balance from stateDB
  22. account, err := s.GetAccount(*idx)
  23. if err != nil {
  24. retSQLErr(err, c)
  25. return
  26. }
  27. apiAccount.Balance = apitypes.NewBigIntStr(account.Balance)
  28. c.JSON(http.StatusOK, apiAccount)
  29. }
  30. func getAccounts(c *gin.Context) {
  31. // Account filters
  32. tokenIDs, addr, bjj, err := parseAccountFilters(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 Accounts from historyDB
  44. apiAccounts, pagination, err := h.GetAccountsAPI(tokenIDs, addr, bjj, fromItem, limit, order)
  45. if err != nil {
  46. retSQLErr(err, c)
  47. return
  48. }
  49. // Get balances from stateDB
  50. for x, apiAccount := range apiAccounts {
  51. idx, err := stringToIdx(string(apiAccount.Idx), "Account Idx")
  52. if err != nil {
  53. retSQLErr(err, c)
  54. return
  55. }
  56. account, err := s.GetAccount(*idx)
  57. if err != nil {
  58. retSQLErr(err, c)
  59. return
  60. }
  61. apiAccounts[x].Balance = apitypes.NewBigIntStr(account.Balance)
  62. }
  63. // Build succesfull response
  64. type accountResponse struct {
  65. Accounts []historydb.AccountAPI `json:"accounts"`
  66. Pagination *db.Pagination `json:"pagination"`
  67. }
  68. c.JSON(http.StatusOK, &accountResponse{
  69. Accounts: apiAccounts,
  70. Pagination: pagination,
  71. })
  72. }