Skip to content

Commit 7ac6d68

Browse files
committed
fix: add token ids filter
1 parent 5b8e1cf commit 7ac6d68

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

internal/handlers/token_handlers.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package handlers
22

33
import (
44
"fmt"
5+
"math/big"
56
"strings"
67

78
"github.com/gin-gonic/gin"
@@ -131,6 +132,25 @@ func serializeBalance(balance common.TokenBalance) BalanceModel {
131132
}
132133
}
133134

135+
func parseTokenIds(input string) ([]*big.Int, error) {
136+
tokenIdsStr := strings.Split(input, ",")
137+
tokenIdsBn := make([]*big.Int, len(tokenIdsStr))
138+
139+
for i, strNum := range tokenIdsStr {
140+
strNum = strings.TrimSpace(strNum) // Remove potential whitespace
141+
if strNum == "" {
142+
continue // Skip empty strings
143+
}
144+
num := new(big.Int)
145+
_, ok := num.SetString(strNum, 10) // Base 10
146+
if !ok {
147+
return nil, fmt.Errorf("invalid token id: %s", strNum)
148+
}
149+
tokenIdsBn[i] = num
150+
}
151+
return tokenIdsBn, nil
152+
}
153+
134154
// @Summary Get holders of a token
135155
// @Description Retrieve holders of a token
136156
// @Tags holders
@@ -175,11 +195,22 @@ func GetTokenHoldersByType(c *gin.Context) {
175195
groupBy = []string{"owner", "token_id"}
176196
}
177197

198+
tokenIds := []*big.Int{}
199+
tokenIdsStr := strings.TrimSpace(c.Query("token_ids"))
200+
if tokenIdsStr != "" {
201+
tokenIds, err = parseTokenIds(c.Query("token_ids"))
202+
if err != nil {
203+
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token ids '%s'", tokenIdsStr))
204+
return
205+
}
206+
}
207+
178208
qf := storage.BalancesQueryFilter{
179209
ChainId: chainId,
180210
TokenType: tokenType,
181211
TokenAddress: address,
182212
ZeroBalance: hideZeroBalances,
213+
TokenIds: tokenIds,
183214
GroupBy: groupBy,
184215
SortBy: c.Query("sort_by"),
185216
SortOrder: c.Query("sort_order"),

internal/storage/clickhouse.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,16 @@ func (c *ClickHouseConnector) GetTokenBalances(qf BalancesQueryFilter, fields ..
14091409
query += fmt.Sprintf(" AND address = '%s'", qf.TokenAddress)
14101410
}
14111411

1412+
if len(qf.TokenIds) > 0 {
1413+
tokenIdsStr := ""
1414+
tokenIdsLen := len(qf.TokenIds)
1415+
for i := 0; i < tokenIdsLen-1; i++ {
1416+
tokenIdsStr += fmt.Sprintf("%s,", qf.TokenIds[i].String())
1417+
}
1418+
tokenIdsStr += qf.TokenIds[tokenIdsLen-1].String()
1419+
query += fmt.Sprintf(" AND token_id in (%s)", tokenIdsStr)
1420+
}
1421+
14121422
isBalanceAggregated := false
14131423
for _, field := range fields {
14141424
if strings.Contains(field, "balance") && strings.TrimSpace(field) != "balance" {

internal/storage/connector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type BalancesQueryFilter struct {
3030
TokenType string
3131
TokenAddress string
3232
Owner string
33+
TokenIds []*big.Int
3334
ZeroBalance bool
3435
GroupBy []string
3536
SortBy string

0 commit comments

Comments
 (0)