Skip to content

Commit d532f55

Browse files
authored
feat: include token type to response (#172)
### TL;DR Added `token_type` field to the `HolderModel` struct and included it in the serialization function. We need it to properly detect token types for NFTs handlers, specifically for further metadata resolutions. ### What changed? - Added a new `TokenType` field to the `HolderModel` struct with JSON tag `token_type` and ClickHouse tag `token_type` - Updated the `serializeHolder` function to populate the new `TokenType` field from the `holder.TokenType` value ### How to test? 1. Make a request to an endpoint that returns token holder data 2. Verify that the response now includes the `token_type` field in the JSON output 3. Check that the value matches the expected token type for the given token ### Why make this change? This change enhances the token holder API response by including the token type information, which allows clients to distinguish between different token standards (e.g., ERC-20, ERC-721, ERC-1155) without having to make additional queries. This provides more complete information about tokens in a single response.
2 parents b500ad4 + a4c676b commit d532f55

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

internal/handlers/token_handlers.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ type BalanceModel struct {
1717
TokenAddress string `json:"token_address" ch:"address"`
1818
TokenId string `json:"token_id" ch:"token_id"`
1919
Balance string `json:"balance" ch:"balance"`
20+
TokenType string `json:"token_type" ch:"token_type"`
2021
}
2122

2223
type HolderModel struct {
2324
HolderAddress string `json:"holder_address" ch:"owner"`
2425
TokenId string `json:"token_id" ch:"token_id"`
2526
Balance string `json:"balance" ch:"balance"`
27+
TokenType string `json:"token_type" ch:"token_type"`
2628
}
2729

2830
// @Summary Get token balances of an address by type
@@ -77,8 +79,8 @@ func GetTokenBalancesByType(c *gin.Context) {
7779
columns := []string{"address", "sum(balance) as balance"}
7880
groupBy := []string{"address"}
7981
if !strings.Contains(strings.Join(tokenTypes, ","), "erc20") {
80-
columns = []string{"address", "token_id", "sum(balance) as balance"}
81-
groupBy = []string{"address", "token_id"}
82+
columns = []string{"address", "token_id", "sum(balance) as balance", "token_type"}
83+
groupBy = []string{"address", "token_id", "token_type"}
8284
}
8385

8486
qf := storage.BalancesQueryFilter{
@@ -139,6 +141,7 @@ func serializeBalance(balance common.TokenBalance) BalanceModel {
139141
}
140142
return ""
141143
}(),
144+
TokenType: balance.TokenType,
142145
}
143146
}
144147

@@ -220,8 +223,8 @@ func GetTokenHoldersByType(c *gin.Context) {
220223
groupBy := []string{"owner"}
221224

222225
if !strings.Contains(strings.Join(tokenTypes, ","), "erc20") {
223-
columns = []string{"owner", "token_id", "sum(balance) as balance"}
224-
groupBy = []string{"owner", "token_id"}
226+
columns = []string{"owner", "token_id", "sum(balance) as balance", "token_type"}
227+
groupBy = []string{"owner", "token_id", "token_type"}
225228
}
226229

227230
tokenIds, err := getTokenIdsFromReq(c)
@@ -286,5 +289,6 @@ func serializeHolder(holder common.TokenBalance) HolderModel {
286289
}
287290
return ""
288291
}(),
292+
TokenType: holder.TokenType,
289293
}
290294
}

0 commit comments

Comments
 (0)