Skip to content

Commit

Permalink
Tokens count endpoint (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Mar 30, 2021
1 parent 91b709c commit 3c9654d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ type getTransfersRequest struct {
TokenID *int64 `form:"token_id" binding:"omitempty,min=0"`
}

type getTokenHolders struct {
type byTokenIDRequest struct {
TokenID *int64 `form:"token_id" binding:"min=0"`
}

Expand Down
33 changes: 32 additions & 1 deletion cmd/api/handlers/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,37 @@ func (ctx *Context) GetContractTokens(c *gin.Context) {
c.JSON(http.StatusOK, metadata)
}

// GetContractTokensCount godoc
// @Summary Get contract`s tokens count
// @Description Get contract`s tokens count
// @Tags contract
// @ID get-contract-token-count
// @Param network path string true "Network"
// @Param address path string true "KT address" minlength(36) maxlength(36)
// @Accept json
// @Produce json
// @Success 200 {object} CountResponse
// @Failure 400 {object} Error
// @Failure 500 {object} Error
// @Router /v1/contract/{network}/{address}/tokens/count [get]
func (ctx *Context) GetContractTokensCount(c *gin.Context) {
var req getContractRequest
if err := c.BindUri(&req); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
count, err := ctx.TokenMetadata.Count([]tokenmetadata.GetContext{{
Contract: req.Address,
Network: req.Network,
TokenID: -1,
}})
if ctx.handleError(c, err, 0) {
return
}
c.JSON(http.StatusOK, CountResponse{
count,
})
}

func (ctx *Context) getTokensWithSupply(getCtx tokenmetadata.GetContext, size, offset int64) ([]Token, error) {
metadata, err := ctx.TokenMetadata.Get([]tokenmetadata.GetContext{getCtx}, size, offset)
if err != nil {
Expand Down Expand Up @@ -372,7 +403,7 @@ func (ctx *Context) GetTokenHolders(c *gin.Context) {
if err := c.BindUri(&req); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
var reqArgs getTokenHolders
var reqArgs byTokenIDRequest
if err := c.BindQuery(&reqArgs); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
Expand Down
1 change: 1 addition & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (api *app) makeRouter() {
tokens := contract.Group("tokens")
{
tokens.GET("", api.Context.GetContractTokens)
tokens.GET("count", api.Context.GetContractTokensCount)
tokens.GET("holders", api.Context.GetTokenHolders)
}

Expand Down
12 changes: 9 additions & 3 deletions internal/elastic/tokenmetadata/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/baking-bad/bcdhub/internal/models/tokenmetadata"
)

func buildGetTokenMetadataContext(ctx ...tokenmetadata.GetContext) core.Base {
func buildGetTokenMetadataContext(ctx []tokenmetadata.GetContext, withSize bool) core.Base {
filters := make([]core.Item, 0)

for _, c := range ctx {
Expand Down Expand Up @@ -34,10 +34,16 @@ func buildGetTokenMetadataContext(ctx ...tokenmetadata.GetContext) core.Base {
))
}

return core.NewQuery().Query(
query := core.NewQuery().Query(
core.Bool(
core.Should(filters...),
core.MinimumShouldMatch(1),
),
).Sort("level", "desc").All()
)

if withSize {
query.Sort("level", "desc").All()
}

return query
}
10 changes: 8 additions & 2 deletions internal/elastic/tokenmetadata/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tokenmetadata

import (
"github.com/baking-bad/bcdhub/internal/elastic/consts"
"github.com/baking-bad/bcdhub/internal/models"

"github.com/baking-bad/bcdhub/internal/elastic/core"
"github.com/baking-bad/bcdhub/internal/models/tokenmetadata"
Expand All @@ -19,7 +20,7 @@ func NewStorage(es *core.Elastic) *Storage {

// Get -
func (storage *Storage) Get(ctx []tokenmetadata.GetContext, size, offset int64) (tokens []tokenmetadata.TokenMetadata, err error) {
query := buildGetTokenMetadataContext(ctx...)
query := buildGetTokenMetadataContext(ctx, true)
scrollCtx := core.NewScrollContext(storage.es, query, size, consts.DefaultScrollSize)
scrollCtx.Offset = offset
err = scrollCtx.Get(&tokens)
Expand All @@ -28,7 +29,7 @@ func (storage *Storage) Get(ctx []tokenmetadata.GetContext, size, offset int64)

// Get -
func (storage *Storage) GetAll(ctx ...tokenmetadata.GetContext) (tokens []tokenmetadata.TokenMetadata, err error) {
query := buildGetTokenMetadataContext(ctx...)
query := buildGetTokenMetadataContext(ctx, true)
err = storage.es.GetAllByQuery(query, &tokens)
return
}
Expand All @@ -49,3 +50,8 @@ func (storage *Storage) GetWithExtras() ([]tokenmetadata.TokenMetadata, error) {

return withExtras, nil
}

// Count -
func (storage *Storage) Count(ctx []tokenmetadata.GetContext) (int64, error) {
return storage.es.CountItems([]string{models.DocTokenMetadata}, buildGetTokenMetadataContext(ctx, false))
}
15 changes: 15 additions & 0 deletions internal/models/mock/tokenmetadata/mock.go

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

1 change: 1 addition & 0 deletions internal/models/tokenmetadata/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ type Repository interface {
Get(ctx []GetContext, size, offset int64) ([]TokenMetadata, error)
GetAll(ctx ...GetContext) ([]TokenMetadata, error)
GetWithExtras() ([]TokenMetadata, error)
Count(ctx []GetContext) (int64, error)
}
5 changes: 5 additions & 0 deletions internal/reindexer/tokenmetadata/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ func (storage *Storage) GetAll(ctx ...tokenmetadata.GetContext) (tokens []tokenm
func (storage *Storage) GetWithExtras() ([]tokenmetadata.TokenMetadata, error) {
return nil, nil
}

// Count -
func (storage *Storage) Count(ctx []tokenmetadata.GetContext) (int64, error) {
return 0, nil
}

0 comments on commit 3c9654d

Please sign in to comment.