Skip to content

Commit

Permalink
indexer/api: integrate token fees queries and endpoints
Browse files Browse the repository at this point in the history
1. /chain/fees/page/{page}
2. /chain/fees/reference/{reference}/page/{page}
3. /chain/fees/type/{type}/page/{page}
4. /accounts/{accountID}/fees/page/{page}

Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Sep 8, 2023
1 parent a546924 commit 8ce1665
Show file tree
Hide file tree
Showing 16 changed files with 688 additions and 45 deletions.
55 changes: 55 additions & 0 deletions api/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func (a *API) enableAccountHandlers() error {
); err != nil {
return err
}
if err := a.endpoint.RegisterMethod(
"/accounts/{accountID}/fees/page/{page}",
"GET",
apirest.MethodAccessTypePublic,
a.tokenFeesHandler,
); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -421,3 +429,50 @@ func (a *API) tokenTransfersHandler(_ *apirest.APIdata, ctx *httprouter.HTTPCont
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// 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.
// @Tags Accounts
// @Accept json
// @Produce json
// @Param accountID path string true "Specific accountID"
// @Param page path string true "Paginator page"
// @Success 200 {object} object{fees=[]indexertypes.TokenFeeMeta}
// @Router /accounts/{accountID}/fees/page/{page} [get]
func (a *API) tokenFeesHandler(_ *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
}
page := 0
if ctx.URLParam("page") != "" {
page, err = strconv.Atoi(ctx.URLParam("page"))
if err != nil {
return ErrCantParsePageNumber
}
}
page = page * MaxPageSize

fees, err := a.indexer.GetTokenFeesByFromAccount(accountID, int32(page), MaxPageSize)
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}
data, err := json.Marshal(
struct {
Fees []*indexertypes.TokenFeeMeta `json:"fees"`
}{Fees: fees},
)
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
}
return ctx.Send(data, apirest.HTTPstatusOK)
}
144 changes: 144 additions & 0 deletions api/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ func (a *API) enableChainHandlers() error {
); err != nil {
return err
}
if err := a.endpoint.RegisterMethod(
"/chain/fees/page/{page}",
"GET",
apirest.MethodAccessTypePublic,
a.chainListFeesHandler,
); err != nil {
return err
}
if err := a.endpoint.RegisterMethod(
"/chain/fees/reference/{reference}/page/{page}",
"GET",
apirest.MethodAccessTypePublic,
a.chainListFeesByReferenceHandler,
); err != nil {
return err
}
if err := a.endpoint.RegisterMethod(
"/chain/fees/type/{type}/page/{page}",
"GET",
apirest.MethodAccessTypePublic,
a.chainListFeesByTypeHandler,
); err != nil {
return err
}

return nil
}
Expand Down Expand Up @@ -846,3 +870,123 @@ func (a *API) chainTxCountHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContex

return ctx.Send(data, apirest.HTTPstatusOK)
}

// chainListFeesHandler
//
// @Summary List all token fees
// @Description Returns the token fees list ordered by date. A spending is an amount of tokens burnt from one account for executing transactions.
// @Tags Accounts
// @Accept json
// @Produce json
// @Param page path string true "Paginator page"
// @Success 200 {object} object{fees=[]indexertypes.TokenFeeMeta}
// @Router /chain/fees/page/{page} [get]
func (a *API) chainListFeesHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
var err error
page := 0
if ctx.URLParam("page") != "" {
page, err = strconv.Atoi(ctx.URLParam("page"))
if err != nil {
return ErrCantParsePageNumber
}
}
page = page * MaxPageSize

fees, err := a.indexer.GetTokenFees(int32(page), MaxPageSize)
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}
data, err := json.Marshal(
struct {
Fees []*indexertypes.TokenFeeMeta `json:"fees"`
}{Fees: fees},
)
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// chainListFeesByReferenceHandler
//
// @Summary List all token fees by reference
// @Description Returns the token fees list filtered by reference and ordered by date. A spending is an amount of tokens burnt from one account for executing transactions.
// @Tags Accounts
// @Accept json
// @Produce json
// @Param reference path string true "Reference filter"
// @Param page path string true "Paginator page"
// @Success 200 {object} object{fees=[]indexertypes.TokenFeeMeta}
// @Router /chain/fees/reference/{reference}/page/{page} [get]
func (a *API) chainListFeesByReferenceHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
var err error
page := 0
if ctx.URLParam("page") != "" {
page, err = strconv.Atoi(ctx.URLParam("page"))
if err != nil {
return ErrCantParsePageNumber
}
}
page = page * MaxPageSize

reference := ctx.URLParam("reference")
if reference == "" {
return ErrMissingParameter
}

fees, err := a.indexer.GetTokenFeesByReference(reference, int32(page), MaxPageSize)
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}
data, err := json.Marshal(
struct {
Fees []*indexertypes.TokenFeeMeta `json:"fees"`
}{Fees: fees},
)
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
}
return ctx.Send(data, apirest.HTTPstatusOK)
}

// chainListFeesByTypeHandler
//
// @Summary List all token fees by type
// @Description Returns the token fees list filtered by type and ordered by date. A spending is an amount of tokens burnt from one account for executing transactions.
// @Tags Accounts
// @Accept json
// @Produce json
// @Param type path string true "Type filter"
// @Param page path string true "Paginator page"
// @Success 200 {object} object{fees=[]indexertypes.TokenFeeMeta}
// @Router /chain/fees/type/{type}/page/{page} [get]
func (a *API) chainListFeesByTypeHandler(_ *apirest.APIdata, ctx *httprouter.HTTPContext) error {
var err error
page := 0
if ctx.URLParam("page") != "" {
page, err = strconv.Atoi(ctx.URLParam("page"))
if err != nil {
return ErrCantParsePageNumber
}
}
page = page * MaxPageSize

typeFilter := ctx.URLParam("type")
if typeFilter == "" {
return ErrMissingParameter
}

fees, err := a.indexer.GetTokenFeesByType(typeFilter, int32(page), MaxPageSize)
if err != nil {
return ErrCantFetchTokenTransfers.WithErr(err)
}
data, err := json.Marshal(
struct {
Fees []*indexertypes.TokenFeeMeta `json:"fees"`
}{Fees: fees},
)
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
}
return ctx.Send(data, apirest.HTTPstatusOK)
}
50 changes: 50 additions & 0 deletions vochain/indexer/db/db.go

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

6 changes: 3 additions & 3 deletions vochain/indexer/db/models.go

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

Loading

0 comments on commit 8ce1665

Please sign in to comment.