-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add get Runes info batch api #73
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
269b246
fix: make existing handlers use new total holders usecase
gazenw 612a895
fix: error msg
gazenw 2a4f586
feat: add get token info batch
gazenw d54506e
feat: add includeHoldersCount in get tokens api
gazenw 5ed622e
refactor: extract response mapping
gazenw 1774d7d
fix: rename new field and add holdersCount to extend
gazenw 23e0b20
fix: query params array
gazenw 8157300
fix: error msg
gazenw ccd0e21
fix: struct tags
gazenw 12981d6
fix: remove error
gazenw deacd9c
feat: add default value to additional fields
gazenw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package httphandler | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/gaze-network/indexer-network/common/errs" | ||
"github.com/gaze-network/indexer-network/modules/runes/runes" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/samber/lo" | ||
) | ||
|
||
type getTokenInfoBatchRequest struct { | ||
Ids []string `json:"ids"` | ||
BlockHeight uint64 `json:"blockHeight"` | ||
AdditionalFields []string `json:"additionalFields"` | ||
} | ||
|
||
const getTokenInfoBatchMaxQueries = 100 | ||
|
||
func (r *getTokenInfoBatchRequest) Validate() error { | ||
var errList []error | ||
|
||
if len(r.Ids) == 0 { | ||
errList = append(errList, errors.New("ids cannot be empty")) | ||
} | ||
if len(r.Ids) > getTokenInfoBatchMaxQueries { | ||
errList = append(errList, errors.Errorf("cannot query more than %d ids", getTokenInfoBatchMaxQueries)) | ||
} | ||
for i := range r.Ids { | ||
id, err := url.QueryUnescape(r.Ids[i]) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
r.Ids[i] = id | ||
if !isRuneIdOrRuneName(r.Ids[i]) { | ||
errList = append(errList, errors.Errorf("ids[%d]: id '%s' is not valid rune id or rune name", i, r.Ids[i])) | ||
} | ||
} | ||
|
||
return errs.WithPublicMessage(errors.Join(errList...), "validation error") | ||
} | ||
|
||
type getTokenInfoBatchResult struct { | ||
List []*getTokenInfoResult `json:"list"` | ||
} | ||
type getTokenInfoBatchResponse = HttpResponse[getTokenInfoBatchResult] | ||
|
||
func (h *HttpHandler) GetTokenInfoBatch(ctx *fiber.Ctx) (err error) { | ||
var req getTokenInfoBatchRequest | ||
if err := ctx.BodyParser(&req); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
if err := req.Validate(); err != nil { | ||
return errors.WithStack(err) | ||
} | ||
|
||
blockHeight := req.BlockHeight | ||
if blockHeight == 0 { | ||
blockHeader, err := h.usecase.GetLatestBlock(ctx.UserContext()) | ||
if err != nil { | ||
if errors.Is(err, errs.NotFound) { | ||
return errs.NewPublicError("latest block not found") | ||
} | ||
return errors.Wrap(err, "error during GetLatestBlock") | ||
} | ||
blockHeight = uint64(blockHeader.Height) | ||
} | ||
|
||
runeIds := make([]runes.RuneId, 0) | ||
for i, id := range req.Ids { | ||
runeId, ok := h.resolveRuneId(ctx.UserContext(), id) | ||
if !ok { | ||
return errs.NewPublicError(fmt.Sprintf("unable to resolve rune id \"%s\" from \"ids[%d]\"", id, i)) | ||
} | ||
runeIds = append(runeIds, runeId) | ||
} | ||
|
||
runeEntries, err := h.usecase.GetRuneEntryByRuneIdAndHeightBatch(ctx.UserContext(), runeIds, blockHeight) | ||
if err != nil { | ||
return errors.Wrap(err, "error during GetRuneEntryByRuneIdAndHeightBatch") | ||
} | ||
holdersCounts := make(map[runes.RuneId]int64) | ||
if lo.Contains(req.AdditionalFields, "holdersCount") { | ||
holdersCounts, err = h.usecase.GetTotalHoldersByRuneIds(ctx.UserContext(), runeIds, blockHeight) | ||
if err != nil { | ||
return errors.Wrap(err, "error during GetBalancesByRuneId") | ||
} | ||
} | ||
|
||
results := make([]*getTokenInfoResult, 0, len(runeIds)) | ||
|
||
for _, runeId := range runeIds { | ||
runeEntry, ok := runeEntries[runeId] | ||
if !ok { | ||
return errs.NewPublicError(fmt.Sprintf("rune not found: %s", runeId)) | ||
} | ||
var holdersCount *int64 | ||
if lo.Contains(req.AdditionalFields, "holdersCount") { | ||
holdersCount = lo.ToPtr(holdersCounts[runeId]) | ||
} | ||
|
||
result, err := createTokenInfoResult(runeEntry, holdersCount) | ||
if err != nil { | ||
return errors.Wrap(err, "error during createTokenInfoResult") | ||
} | ||
results = append(results, result) | ||
} | ||
|
||
resp := getTokenInfoBatchResponse{ | ||
Result: &getTokenInfoBatchResult{ | ||
List: results, | ||
}, | ||
} | ||
|
||
return errors.WithStack(ctx.JSON(resp)) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use
%q
for quotation