Skip to content

Commit

Permalink
add new table accounts and queries: GetListAccounts, CountAccounts, C…
Browse files Browse the repository at this point in the history
…reateAccount

add new queries in token_transfers: GetTokenTransfersByToAccount, GetTokenTransfersByAccount, CountTokenTransfersByAccount
add new integration test: testAPIAccountTokentxns
update GetTransfers api
update indexer method onSetAccount
  • Loading branch information
mariajdab committed Oct 16, 2023
1 parent c75912c commit e440654
Show file tree
Hide file tree
Showing 15 changed files with 506 additions and 224 deletions.
42 changes: 40 additions & 2 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ func (a *API) enableAccountHandlers() error {
); err != nil {
return err
}
if err := a.endpoint.RegisterMethod(
"/accounts/{accountID}/transfers/count",
"GET",
apirest.MethodAccessTypePublic,
a.tokenTransfersCountHandler,
); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -415,13 +423,13 @@ func (a *API) tokenTransfersHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCont
}
}
page = page * MaxPageSize
transfers, err := a.indexer.GetTokenTransfersByFromAccount(accountID, int32(page), MaxPageSize)
transfers, err := a.indexer.GetTokenTransfersAccount(accountID, int32(page), MaxPageSize)
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}
data, err := json.Marshal(
struct {
Transfers []*indexertypes.TokenTransferMeta `json:"transfers"`
Transfers map[string][]*indexertypes.TokenTransferMeta `json:"transfers"`
}{Transfers: transfers},
)
if err != nil {
Expand Down Expand Up @@ -476,3 +484,33 @@ func (a *API) tokenFeesHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext)
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// /accounts/{accountID}/transfers/count
func (a *API) tokenTransfersCountHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
accountID, err := hex.DecodeString(util.TrimHex(ctx.URLParam("accountID")))
if err != nil || accountID == nil {
return ErrCantParseAccountID.Withf("%q", ctx.URLParam("accountID"))
}
acc, err := a.vocapp.State.GetAccount(common.BytesToAddress(accountID), true)
if acc == nil {
return ErrAccountNotFound
}
if err != nil {
return err
}

count, err := a.indexer.CountTokenTransfersAccount(accountID)
if err != nil {
return err
}
data, err := json.Marshal(
struct {
Count uint64 `json:"count"`
}{Count: count},
)
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
}

return ctx.Send(data, apirest.HTTPstatusOK)
}
28 changes: 23 additions & 5 deletions apiclient/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.vocdoni.io/dvote/data/ipfs"
"go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/types"
indexertypes "go.vocdoni.io/dvote/vochain/indexer/indexertypes"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
"go.vocdoni.io/proto/build/go/models"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -228,19 +228,21 @@ func (c *HTTPclient) AccountSetMetadata(metadata *api.AccountMetadata) (types.He
}

// GetTransfers returns the list of token transfers associated with an account
func (c *HTTPclient) GetTransfers(from common.Address, page int) ([]*indexertypes.TokenTransferMeta, error) {
func (c *HTTPclient) GetTransfers(from common.Address, page int) (map[string][]*indexertypes.TokenTransferMeta, error) {
resp, code, err := c.Request(HTTPGET, nil, "accounts", from.Hex(), "transfers", "page", strconv.Itoa(page))
if err != nil {
return nil, err
}
if code != apirest.HTTPstatusOK {
return nil, fmt.Errorf("%s: %d (%s)", errCodeNot200, code, resp)
}
var transfers []*indexertypes.TokenTransferMeta
if err := json.Unmarshal(resp, &transfers); err != nil {
trnsfs := new(struct {
Transfers map[string][]*indexertypes.TokenTransferMeta `json:"transfers"`
})
if err := json.Unmarshal(resp, &trnsfs); err != nil {
return nil, err
}
return transfers, nil
return trnsfs.Transfers, nil
}

// SetSIK function allows to update the Secret Identity Key for the current
Expand Down Expand Up @@ -382,3 +384,19 @@ func (c *HTTPclient) RegisterSIKForVote(electionId types.HexBytes, proof *Census
}
return hash, nil
}

// AccountCount returns the count of transactions
func (c *HTTPclient) AccountCount() (uint64, error) {
resp, code, err := c.Request(HTTPGET, nil, "accounts", "count")
if err != nil {
return 0, err
}
if code != apirest.HTTPstatusOK {
return 0, fmt.Errorf("%s: %d (%s)", errCodeNot200, code, resp)
}
txsCount := new(struct {
Count uint64 `json:"count"`
})

return txsCount.Count, json.Unmarshal(resp, txsCount)
}
3 changes: 3 additions & 0 deletions cmd/end2endtest/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func (t *E2ETokenTxs) Run() error {
return fmt.Errorf("error in testSendTokens: %w", err)
}

c, err := t.api.AccountCount()
fmt.Println("lulis", c, err)

return nil
}

Expand Down
Loading

0 comments on commit e440654

Please sign in to comment.