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.

89 lines
2.0 KiB

4 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
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. "github.com/hermeznetwork/hermez-node/db/statedb"
  8. "github.com/hermeznetwork/tracerr"
  9. )
  10. func (a *API) getAccount(c *gin.Context) {
  11. // Get Addr
  12. idx, err := parseParamIdx(c)
  13. if err != nil {
  14. retBadReq(err, c)
  15. return
  16. }
  17. apiAccount, err := a.h.GetAccountAPI(*idx)
  18. if err != nil {
  19. retSQLErr(err, c)
  20. return
  21. }
  22. // Get balance from stateDB
  23. account, err := a.s.LastGetAccount(*idx)
  24. if err != nil {
  25. retSQLErr(err, c)
  26. return
  27. }
  28. apiAccount.Balance = apitypes.NewBigIntStr(account.Balance)
  29. apiAccount.Nonce = account.Nonce
  30. c.JSON(http.StatusOK, apiAccount)
  31. }
  32. func (a *API) getAccounts(c *gin.Context) {
  33. // Account filters
  34. tokenIDs, addr, bjj, err := parseAccountFilters(c)
  35. if err != nil {
  36. retBadReq(err, c)
  37. return
  38. }
  39. // Pagination
  40. fromItem, order, limit, err := parsePagination(c)
  41. if err != nil {
  42. retBadReq(err, c)
  43. return
  44. }
  45. // Fetch Accounts from historyDB
  46. apiAccounts, pendingItems, err := a.h.GetAccountsAPI(tokenIDs, addr, bjj, fromItem, limit, order)
  47. if err != nil {
  48. retSQLErr(err, c)
  49. return
  50. }
  51. // Get balances from stateDB
  52. if err := a.s.LastRead(func(sdb *statedb.Last) error {
  53. for x, apiAccount := range apiAccounts {
  54. idx, err := stringToIdx(string(apiAccount.Idx), "Account Idx")
  55. if err != nil {
  56. return tracerr.Wrap(err)
  57. }
  58. account, err := sdb.GetAccount(*idx)
  59. if err != nil {
  60. return tracerr.Wrap(err)
  61. }
  62. apiAccounts[x].Balance = apitypes.NewBigIntStr(account.Balance)
  63. apiAccounts[x].Nonce = account.Nonce
  64. }
  65. return nil
  66. }); err != nil {
  67. retSQLErr(err, c)
  68. return
  69. }
  70. // Build succesfull response
  71. type accountResponse struct {
  72. Accounts []historydb.AccountAPI `json:"accounts"`
  73. PendingItems uint64 `json:"pendingItems"`
  74. }
  75. c.JSON(http.StatusOK, &accountResponse{
  76. Accounts: apiAccounts,
  77. PendingItems: pendingItems,
  78. })
  79. }