Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional token_id parameter to the contract tokens endpoint #570

Merged
merged 3 commits into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/api/handlers/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (ctx *Context) appendDAppInfo(dapp *tzip.DApp, withDetails bool) (DApp, err
ReleaseDate: contract.Timestamp.UTC(),
})

tokens, err := ctx.getTokens(consts.Mainnet, address.Address, 0, 0, 0, 0, "")
tokens, err := ctx.getTokens(consts.Mainnet, address.Address, 0, 0, 0, 0, "", -1)
if err != nil {
return result, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ type minMaxLevel struct {
type tokenRequest struct {
pageableRequest
minMaxLevel
TokenID *int64 `form:"token_id" binding:"omitempty"`
}

type tokenBalanceRequest struct {
Expand Down
12 changes: 8 additions & 4 deletions cmd/api/handlers/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,26 @@ func (ctx *Context) GetContractTokens(c *gin.Context) {
if err := c.BindQuery(&pageReq); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
if pageReq.TokenID == nil {
pageReq.TokenID = new(int64)
*pageReq.TokenID = -1
}
if pageReq.Size == 0 {
pageReq.Size = 10
}
tokens, err := ctx.getTokens(req.Network, req.Address, pageReq.Size, pageReq.Offset, pageReq.MinLevel, pageReq.MaxLevel, "")
tokens, err := ctx.getTokens(req.Network, req.Address, pageReq.Size, pageReq.Offset, pageReq.MinLevel, pageReq.MaxLevel, "", *pageReq.TokenID)
if ctx.handleError(c, err, 0) {
return
}
c.JSON(http.StatusOK, tokens)
}

func (ctx *Context) getTokens(network, contract string, size, offset, minLevel, maxLevel int64, creator string) ([]Token, error) {
func (ctx *Context) getTokens(network, contract string, size, offset, minLevel, maxLevel int64, creator string, tokenId int64) ([]Token, error) {
metadata, err := ctx.TokenMetadata.Get([]tokenmetadata.GetContext{
{
Contract: contract,
Network: network,
TokenID: -1,
TokenID: tokenId,
MinLevel: minLevel,
MaxLevel: maxLevel,
Creator: creator,
Expand Down Expand Up @@ -407,7 +411,7 @@ func (ctx *Context) GetTokenMetadata(c *gin.Context) {
if queryParams.Size == 0 {
queryParams.Size = 10
}
tokens, err := ctx.getTokens(req.Network, queryParams.Contract, queryParams.Size, queryParams.Offset, queryParams.MinLevel, queryParams.MaxLevel, queryParams.Creator)
tokens, err := ctx.getTokens(req.Network, queryParams.Contract, queryParams.Size, queryParams.Offset, queryParams.MinLevel, queryParams.MaxLevel, queryParams.Creator, -1)
if ctx.handleError(c, err, 0) {
return
}
Expand Down