mirror of
https://github.com/alexgo-io/gaze-indexer.git
synced 2026-01-12 08:34:28 +08:00
* feat: implement pagination on get balance, get holders * feat: paginate get transactions * fix: remove debug * feat: implement pagination in get utxos * feat: sort response in get holders * feat: cap batch query * feat: add default limits to all endpoints * chore: rename endpoint funcs * fix: parse rune name spacers * feat(runes): add get token list api * fix(runes): use distinct to get token list * feat: remove unused code * fix: count holders distinct pkscript * feat: implement additional scopes * chore: comments * feat: implement search * refactor: switch to use paginationRequest * refactor: rename get token list to get tokens * fix: count total holders by rune ids * fix: rename file * fix: rename minting to ongoing * fix: get ongoing check rune is mintable * chore: disable gosec g115 * fix: pr --------- Co-authored-by: Gaze <gazenw@users.noreply.github.com>
148 lines
4.2 KiB
Go
148 lines
4.2 KiB
Go
package httphandler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"slices"
|
|
|
|
"github.com/cockroachdb/errors"
|
|
"github.com/gaze-network/indexer-network/common/errs"
|
|
"github.com/gaze-network/indexer-network/modules/runes/internal/entity"
|
|
"github.com/gaze-network/indexer-network/modules/runes/runes"
|
|
"github.com/gaze-network/uint128"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
type getHoldersRequest struct {
|
|
paginationRequest
|
|
Id string `params:"id"`
|
|
BlockHeight uint64 `query:"blockHeight"`
|
|
}
|
|
|
|
const (
|
|
getHoldersMaxLimit = 1000
|
|
)
|
|
|
|
func (r getHoldersRequest) Validate() error {
|
|
var errList []error
|
|
if !isRuneIdOrRuneName(r.Id) {
|
|
errList = append(errList, errors.New("'id' is not valid rune id or rune name"))
|
|
}
|
|
if r.Limit < 0 {
|
|
errList = append(errList, errors.New("'limit' must be non-negative"))
|
|
}
|
|
if r.Limit > getHoldersMaxLimit {
|
|
errList = append(errList, errors.Errorf("'limit' cannot exceed %d", getHoldersMaxLimit))
|
|
}
|
|
return errs.WithPublicMessage(errors.Join(errList...), "validation error")
|
|
}
|
|
|
|
type holdingBalance struct {
|
|
Address string `json:"address"`
|
|
PkScript string `json:"pkScript"`
|
|
Amount uint128.Uint128 `json:"amount"`
|
|
Percent float64 `json:"percent"`
|
|
}
|
|
|
|
type getHoldersResult struct {
|
|
BlockHeight uint64 `json:"blockHeight"`
|
|
TotalSupply uint128.Uint128 `json:"totalSupply"`
|
|
MintedAmount uint128.Uint128 `json:"mintedAmount"`
|
|
Decimals uint8 `json:"decimals"`
|
|
List []holdingBalance `json:"list"`
|
|
}
|
|
|
|
type getHoldersResponse = HttpResponse[getHoldersResult]
|
|
|
|
func (h *HttpHandler) GetHolders(ctx *fiber.Ctx) (err error) {
|
|
var req getHoldersRequest
|
|
if err := ctx.ParamsParser(&req); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
if err := ctx.QueryParser(&req); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
if err := req.Validate(); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
if err := req.ParseDefault(); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
blockHeight := req.BlockHeight
|
|
if blockHeight == 0 {
|
|
blockHeader, err := h.usecase.GetLatestBlock(ctx.UserContext())
|
|
if err != nil {
|
|
return errors.Wrap(err, "error during GetLatestBlock")
|
|
}
|
|
blockHeight = uint64(blockHeader.Height)
|
|
}
|
|
|
|
var runeId runes.RuneId
|
|
if req.Id != "" {
|
|
var ok bool
|
|
runeId, ok = h.resolveRuneId(ctx.UserContext(), req.Id)
|
|
if !ok {
|
|
return errs.NewPublicError("unable to resolve rune id from \"id\"")
|
|
}
|
|
}
|
|
|
|
runeEntry, err := h.usecase.GetRuneEntryByRuneIdAndHeight(ctx.UserContext(), runeId, blockHeight)
|
|
if err != nil {
|
|
if errors.Is(err, errs.NotFound) {
|
|
return errs.NewPublicError("rune not found")
|
|
}
|
|
return errors.Wrap(err, "error during GetRuneEntryByRuneIdAndHeight")
|
|
}
|
|
holdingBalances, err := h.usecase.GetBalancesByRuneId(ctx.UserContext(), runeId, blockHeight, req.Limit, req.Offset)
|
|
if err != nil {
|
|
if errors.Is(err, errs.NotFound) {
|
|
return errs.NewPublicError("balances not found")
|
|
}
|
|
return errors.Wrap(err, "error during GetBalancesByRuneId")
|
|
}
|
|
|
|
totalSupply, err := runeEntry.Supply()
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot get total supply of rune")
|
|
}
|
|
mintedAmount, err := runeEntry.MintedAmount()
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot get minted amount of rune")
|
|
}
|
|
|
|
list := make([]holdingBalance, 0, len(holdingBalances))
|
|
for _, balance := range holdingBalances {
|
|
address := addressFromPkScript(balance.PkScript, h.network)
|
|
amount := decimal.NewFromBigInt(balance.Amount.Big(), 0)
|
|
percent := amount.Div(decimal.NewFromBigInt(totalSupply.Big(), 0))
|
|
list = append(list, holdingBalance{
|
|
Address: address,
|
|
PkScript: hex.EncodeToString(balance.PkScript),
|
|
Amount: balance.Amount,
|
|
Percent: percent.InexactFloat64(),
|
|
})
|
|
}
|
|
|
|
// sort by amount descending, then pk script ascending
|
|
slices.SortFunc(holdingBalances, func(b1, b2 *entity.Balance) int {
|
|
if b1.Amount.Cmp(b2.Amount) == 0 {
|
|
return bytes.Compare(b1.PkScript, b2.PkScript)
|
|
}
|
|
return b2.Amount.Cmp(b1.Amount)
|
|
})
|
|
|
|
resp := getHoldersResponse{
|
|
Result: &getHoldersResult{
|
|
BlockHeight: blockHeight,
|
|
TotalSupply: totalSupply,
|
|
MintedAmount: mintedAmount,
|
|
Decimals: runeEntry.Divisibility,
|
|
List: list,
|
|
},
|
|
}
|
|
|
|
return errors.WithStack(ctx.JSON(resp))
|
|
}
|