Skip to content

Commit

Permalink
Merge branch 'feat/api-transfers-pagination' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Aug 6, 2024
2 parents 820e19e + ba9f03a commit 4b888e4
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 211 deletions.
45 changes: 15 additions & 30 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.vocdoni.io/dvote/log"
"go.vocdoni.io/dvote/types"
"go.vocdoni.io/dvote/util"
"go.vocdoni.io/dvote/vochain/indexer/indexertypes"
"go.vocdoni.io/dvote/vochain/state"
"go.vocdoni.io/proto/build/go/models"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -433,56 +432,42 @@ func (a *API) accountElectionsCountHandler(_ *apirest.APIdata, ctx *httprouter.H
//
// @Summary List account received and sent token transfers
// @Description Returns the token transfers for an account. A transfer is a token transference from one account to other (excepting the burn address).
// @Deprecated
// @Description (deprecated, in favor of /chain/transfers?accountId=xxx&page=xxx)
// @Tags Accounts
// @Accept json
// @Produce json
// @Param accountId path string true "Specific accountId"
// @Param accountId path string true "Specific accountId that sent or received the tokens"
// @Param page path number true "Page"
// @Success 200 {object} object{transfers=indexertypes.TokenTransfersAccount}
// @Success 200 {object} TransfersList
// @Router /accounts/{accountId}/transfers/page/{page} [get]
func (a *API) tokenTransfersListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
accountID, err := hex.DecodeString(util.TrimHex(ctx.URLParam(ParamAccountId)))
if err != nil || accountID == nil {
return ErrCantParseAccountID.Withf("%q", ctx.URLParam(ParamAccountId))
}
acc, err := a.vocapp.State.GetAccount(common.BytesToAddress(accountID), true)
if acc == nil {
return ErrAccountNotFound
}
if err != nil {
return err
}

page, err := parsePage(ctx.URLParam(ParamPage))
params, err := parseTransfersParams(
ctx.URLParam(ParamPage),
"",
ctx.URLParam(ParamAccountId),
"",
"",
)
if err != nil {
return err
}

transfers, err := a.indexer.GetTokenTransfersByAccount(accountID, int32(page*DefaultItemsPerPage), DefaultItemsPerPage)
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}
data, err := json.Marshal(
struct {
Transfers indexertypes.TokenTransfersAccount `json:"transfers"`
}{Transfers: transfers},
)
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
}
return ctx.Send(data, apirest.HTTPstatusOK)
return a.sendTransfersList(ctx, params)
}

// tokenFeesHandler
//
// @Summary List account token fees
// @Description Returns the token fees for an account. A spending is an amount of tokens burnt from one account for executing transactions.
// @Deprecated
// @Description (deprecated, in favor of /chain/transfers?accountId=xxx&page=xxx)
// @Tags Accounts
// @Accept json
// @Produce json
// @Param accountId path string true "Specific accountId"
// @Param page path number true "Page"
// @Success 200 {object} object{fees=[]indexertypes.TokenFeeMeta}
// @Success 200 {object} FeesList
// @Router /accounts/{accountId}/fees/page/{page} [get]
func (a *API) tokenFeesHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
params, err := parseFeesParams(
Expand Down
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const (
ParamHeight = "height"
ParamReference = "reference"
ParamType = "type"
ParamAccountIdFrom = "accountIdFrom"
ParamAccountIdTo = "accountIdTo"
)

var (
Expand Down
14 changes: 14 additions & 0 deletions api/api_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ type FeesParams struct {
AccountID string `json:"accountId,omitempty"`
}

// TransfersParams allows the client to filter transfers
type TransfersParams struct {
PaginationParams
AccountID string `json:"accountId,omitempty"`
AccountIDFrom string `json:"accountIdFrom,omitempty"`
AccountIDTo string `json:"accountIdTo,omitempty"`
}

// VoteParams allows the client to filter votes
type VoteParams struct {
PaginationParams
Expand Down Expand Up @@ -264,6 +272,12 @@ type FeesList struct {
Pagination *Pagination `json:"pagination"`
}

// TransfersList is used to return a paginated list to the client
type TransfersList struct {
Transfers []*indexertypes.TokenTransferMeta `json:"transfers"`
Pagination *Pagination `json:"pagination"`
}

type GenericTransactionWithInfo struct {
TxContent json.RawMessage `json:"tx"`
TxInfo indexertypes.Transaction `json:"txInfo"`
Expand Down
80 changes: 80 additions & 0 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ func (a *API) enableChainHandlers() error {
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/chain/transfers",
"GET",
apirest.MethodAccessTypePublic,
a.chainTransfersListHandler,
); err != nil {
return err
}
if err := a.Endpoint.RegisterMethod(
"/chain/export/indexer",
"GET",
Expand Down Expand Up @@ -1076,6 +1084,63 @@ func (a *API) sendFeesList(ctx *httprouter.HTTPContext, params *FeesParams) erro
return marshalAndSend(ctx, list)
}

// chainTransfersListHandler
//
// @Summary List all token transfers
// @Description Returns the token transfers list ordered by date.
// @Tags Chain
// @Accept json
// @Produce json
// @Param page query number false "Page"
// @Param limit query number false "Items per page"
// @Param accountId query string false "Specific accountId that sent or received the tokens"
// @Param accountIdFrom query string false "Specific accountId that sent the tokens"
// @Param accountIdTo query string false "Specific accountId that received the tokens"
// @Success 200 {object} TransfersList
// @Router /chain/transfers [get]
func (a *API) chainTransfersListHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
params, err := parseTransfersParams(
ctx.QueryParam(ParamPage),
ctx.QueryParam(ParamLimit),
ctx.QueryParam(ParamAccountId),
ctx.QueryParam(ParamAccountIdFrom),
ctx.QueryParam(ParamAccountIdTo),
)
if err != nil {
return err
}

return a.sendTransfersList(ctx, params)
}

// sendTransfersList produces a filtered, paginated TokenTransfersList,
// and sends it marshalled over ctx.Send
//
// Errors returned are always of type APIerror.
func (a *API) sendTransfersList(ctx *httprouter.HTTPContext, params *TransfersParams) error {
transfers, total, err := a.indexer.TokenTransfersList(
params.Limit,
params.Page*params.Limit,
params.AccountID,
params.AccountIDFrom,
params.AccountIDTo,
)
if err != nil {
return ErrIndexerQueryFailed.WithErr(err)
}

pagination, err := calculatePagination(params.Page, params.Limit, total)
if err != nil {
return err
}

list := &TransfersList{
Transfers: transfers,
Pagination: pagination,
}
return marshalAndSend(ctx, list)
}

// chainIndexerExportHandler
//
// @Summary Exports the indexer database
Expand Down Expand Up @@ -1122,6 +1187,21 @@ func parseFeesParams(paramPage, paramLimit, paramReference, paramType, paramAcco
}, nil
}

// parseTransfersParams returns an TransfersParams filled with the passed params
func parseTransfersParams(paramPage, paramLimit, paramAccountId, paramAccountIdFrom, paramAccountIdTo string) (*TransfersParams, error) {
pagination, err := parsePaginationParams(paramPage, paramLimit)
if err != nil {
return nil, err
}

return &TransfersParams{
PaginationParams: pagination,
AccountID: util.TrimHex(paramAccountId),
AccountIDFrom: util.TrimHex(paramAccountIdFrom),
AccountIDTo: util.TrimHex(paramAccountIdTo),
}, nil
}

// parseTransactionParams returns an TransactionParams filled with the passed params
func parseTransactionParams(paramPage, paramLimit, paramHeight, paramType string) (*TransactionParams, error) {
pagination, err := parsePaginationParams(paramPage, paramLimit)
Expand Down
78 changes: 34 additions & 44 deletions vochain/indexer/db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b888e4

Please sign in to comment.