Skip to content

Commit 3726e80

Browse files
committed
fix: use query array for token ids and token types
1 parent 9f885c5 commit 3726e80

File tree

1 file changed

+32
-47
lines changed

1 file changed

+32
-47
lines changed

internal/handlers/token_handlers.go

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,7 @@ func GetTokenBalancesByType(c *gin.Context) {
4949
return
5050
}
5151

52-
tokenTypesStr := c.Param("type")
53-
if tokenTypesStr == "" {
54-
tokenTypesStr = c.Query("token_types")
55-
}
56-
57-
tokenTypesStr = strings.ToLower(tokenTypesStr)
58-
tokenTypes, err := parseTokenType(tokenTypesStr)
59-
52+
tokenTypes, err := getTokenTypesFromReq(c)
6053
if err != nil {
6154
api.BadRequestErrorHandler(c, err)
6255
return
@@ -73,21 +66,17 @@ func GetTokenBalancesByType(c *gin.Context) {
7366
return
7467
}
7568

76-
tokenIds := []*big.Int{}
77-
tokenIdsStr := strings.TrimSpace(c.Query("token_ids"))
78-
if tokenIdsStr != "" {
79-
tokenIds, err = parseTokenIds(tokenIdsStr)
80-
if err != nil {
81-
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token ids '%s'", tokenIdsStr))
82-
return
83-
}
69+
tokenIds, err := getTokenIdsFromReq(c)
70+
if err != nil {
71+
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token ids '%s'", err))
72+
return
8473
}
8574

8675
hideZeroBalances := c.Query("hide_zero_balances") != "false"
8776

8877
columns := []string{"address", "sum(balance) as balance"}
8978
groupBy := []string{"address"}
90-
if !strings.Contains(tokenTypesStr, "erc20") {
79+
if !strings.Contains(strings.Join(tokenTypes, ","), "erc20") {
9180
columns = []string{"address", "token_id", "sum(balance) as balance"}
9281
groupBy = []string{"address", "token_id"}
9382
}
@@ -153,35 +142,37 @@ func serializeBalance(balance common.TokenBalance) BalanceModel {
153142
}
154143
}
155144

156-
func parseTokenType(input string) ([]string, error) {
157-
tokenTypes := []string{input}
158-
if strings.Contains(input, ",") {
159-
tokenTypes = strings.Split(input, ",")
145+
func getTokenTypesFromReq(c *gin.Context) ([]string, error) {
146+
tokenTypeParam := c.Param("type")
147+
var tokenTypes []string
148+
if tokenTypeParam != "" {
149+
tokenTypes = []string{tokenTypeParam}
150+
} else {
151+
tokenTypes = c.QueryArray("token_type")
160152
}
161-
for _, tokenType := range tokenTypes {
153+
154+
for i, tokenType := range tokenTypes {
155+
tokenType = strings.ToLower(tokenType)
162156
if tokenType != "erc721" && tokenType != "erc1155" && tokenType != "erc20" {
163157
return []string{}, fmt.Errorf("invalid token type: %s", tokenType)
164158
}
159+
tokenTypes[i] = tokenType
165160
}
166161
return tokenTypes, nil
167162
}
168163

169-
func parseTokenIds(input string) ([]*big.Int, error) {
170-
tokenIdsStr := []string{input}
171-
if strings.Contains(input, ",") {
172-
tokenIdsStr = strings.Split(input, ",")
173-
}
174-
tokenIdsBn := make([]*big.Int, len(tokenIdsStr))
175-
176-
for i, strNum := range tokenIdsStr {
177-
strNum = strings.TrimSpace(strNum) // Remove potential whitespace
178-
if strNum == "" {
179-
continue // Skip empty strings
164+
func getTokenIdsFromReq(c *gin.Context) ([]*big.Int, error) {
165+
tokenIds := c.QueryArray("token_id")
166+
tokenIdsBn := make([]*big.Int, len(tokenIds))
167+
for i, tokenId := range tokenIds {
168+
tokenId = strings.TrimSpace(tokenId) // Remove potential whitespace
169+
if tokenId == "" {
170+
return nil, fmt.Errorf("invalid token id: %s", tokenId)
180171
}
181172
num := new(big.Int)
182-
_, ok := num.SetString(strNum, 10) // Base 10
173+
_, ok := num.SetString(tokenId, 10) // Base 10
183174
if !ok {
184-
return nil, fmt.Errorf("invalid token id: %s", strNum)
175+
return nil, fmt.Errorf("invalid token id: %s", tokenId)
185176
}
186177
tokenIdsBn[i] = num
187178
}
@@ -218,8 +209,7 @@ func GetTokenHoldersByType(c *gin.Context) {
218209
return
219210
}
220211

221-
tokenTypesStr := strings.ToLower(c.Query("token_types"))
222-
tokenTypes, err := parseTokenType(tokenTypesStr)
212+
tokenTypes, err := getTokenTypesFromReq(c)
223213
if err != nil {
224214
api.BadRequestErrorHandler(c, err)
225215
return
@@ -229,21 +219,16 @@ func GetTokenHoldersByType(c *gin.Context) {
229219
columns := []string{"owner", "sum(balance) as balance"}
230220
groupBy := []string{"owner"}
231221

232-
if !strings.Contains(tokenTypesStr, "erc20") {
222+
if !strings.Contains(strings.Join(tokenTypes, ","), "erc20") {
233223
columns = []string{"owner", "token_id", "sum(balance) as balance"}
234224
groupBy = []string{"owner", "token_id"}
235225
}
236226

237-
tokenIds := []*big.Int{}
238-
tokenIdsStr := strings.TrimSpace(c.Query("token_ids"))
239-
if tokenIdsStr != "" {
240-
tokenIds, err = parseTokenIds(c.Query("token_ids"))
241-
if err != nil {
242-
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token ids '%s'", tokenIdsStr))
243-
return
244-
}
227+
tokenIds, err := getTokenIdsFromReq(c)
228+
if err != nil {
229+
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token ids '%s'", err))
230+
return
245231
}
246-
247232
qf := storage.BalancesQueryFilter{
248233
ChainId: chainId,
249234
TokenTypes: tokenTypes,

0 commit comments

Comments
 (0)