From e45cd1efe1289f7edd9d1c30279dc82eb962da24 Mon Sep 17 00:00:00 2001 From: Michael Zaikin Date: Sat, 27 Mar 2021 22:16:15 +0300 Subject: [PATCH] Add optional token_id parameter to the contract tokens endpoint (#570) --- cmd/api/handlers/dapp.go | 2 +- cmd/api/handlers/requests.go | 1 + cmd/api/handlers/tokens.go | 12 ++++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/api/handlers/dapp.go b/cmd/api/handlers/dapp.go index 5e83c639f..2b2ca9146 100644 --- a/cmd/api/handlers/dapp.go +++ b/cmd/api/handlers/dapp.go @@ -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 } diff --git a/cmd/api/handlers/requests.go b/cmd/api/handlers/requests.go index 65b3df642..e8453e3bb 100644 --- a/cmd/api/handlers/requests.go +++ b/cmd/api/handlers/requests.go @@ -370,6 +370,7 @@ type minMaxLevel struct { type tokenRequest struct { pageableRequest minMaxLevel + TokenID *int64 `form:"token_id" binding:"omitempty"` } type tokenBalanceRequest struct { diff --git a/cmd/api/handlers/tokens.go b/cmd/api/handlers/tokens.go index 8033e7d32..61b3c0716 100644 --- a/cmd/api/handlers/tokens.go +++ b/cmd/api/handlers/tokens.go @@ -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, @@ -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 }