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.

81 lines
1.7 KiB

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