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.

83 lines
1.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 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. apiAccount.Nonce = account.Nonce
  28. c.JSON(http.StatusOK, apiAccount)
  29. }
  30. func (a *API) 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, pendingItems, err := a.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 := a.s.GetAccount(*idx)
  57. if err != nil {
  58. retSQLErr(err, c)
  59. return
  60. }
  61. apiAccounts[x].Balance = apitypes.NewBigIntStr(account.Balance)
  62. apiAccounts[x].Nonce = account.Nonce
  63. }
  64. // Build succesfull response
  65. type accountResponse struct {
  66. Accounts []historydb.AccountAPI `json:"accounts"`
  67. PendingItems uint64 `json:"pendingItems"`
  68. }
  69. c.JSON(http.StatusOK, &accountResponse{
  70. Accounts: apiAccounts,
  71. PendingItems: pendingItems,
  72. })
  73. }