Skip to content

Commit 58daf2e

Browse files
committed
fix: add /balances with token type query param + make token type optional
1 parent 7ac6d68 commit 58daf2e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cmd/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ func RunApi(cmd *cobra.Command, args []string) {
8787
// token balance queries
8888
root.GET("/balances/:owner/:type", handlers.GetTokenBalancesByType)
8989

90+
root.GET("/balances/:owner", handlers.GetTokenBalancesByType)
91+
9092
// token holder queries
9193
root.GET("/holders/:address", handlers.GetTokenHoldersByType)
9294

internal/handlers/token_handlers.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ func GetTokenBalancesByType(c *gin.Context) {
4949
return
5050
}
5151
tokenType := c.Param("type")
52-
if tokenType != "erc20" && tokenType != "erc1155" && tokenType != "erc721" {
53-
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token type '%s'", tokenType))
54-
return
52+
if tokenType == "" {
53+
tokenType = c.Query("token_type")
5554
}
55+
5656
owner := strings.ToLower(c.Param("owner"))
5757
if !strings.HasPrefix(owner, "0x") {
5858
api.BadRequestErrorHandler(c, fmt.Errorf("invalid owner address '%s'", owner))
@@ -63,6 +63,17 @@ func GetTokenBalancesByType(c *gin.Context) {
6363
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token address '%s'", tokenAddress))
6464
return
6565
}
66+
67+
tokenIds := []*big.Int{}
68+
tokenIdsStr := strings.TrimSpace(c.Query("token_ids"))
69+
if tokenIdsStr != "" {
70+
tokenIds, err = parseTokenIds(c.Query("token_ids"))
71+
if err != nil {
72+
api.BadRequestErrorHandler(c, fmt.Errorf("invalid token ids '%s'", tokenIdsStr))
73+
return
74+
}
75+
}
76+
6677
hideZeroBalances := c.Query("hide_zero_balances") != "false"
6778

6879
columns := []string{"address", "sum(balance) as balance"}
@@ -78,6 +89,7 @@ func GetTokenBalancesByType(c *gin.Context) {
7889
TokenType: tokenType,
7990
TokenAddress: tokenAddress,
8091
ZeroBalance: hideZeroBalances,
92+
TokenIds: tokenIds,
8193
GroupBy: groupBy,
8294
SortBy: c.Query("sort_by"),
8395
SortOrder: c.Query("sort_order"),

0 commit comments

Comments
 (0)