planetmint-go/x/dao/keeper/query_distributions.go
Jürgen Eckel 41b5bf545f
Eckelj/distributions query (#491)
* added distributions query
* added test cases

---------

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
Signed-off-by: Julian Strobl <jmastr@mailbox.org>
Co-authored-by: Julian Strobl <jmastr@mailbox.org>
2024-11-28 17:15:16 +01:00

36 lines
1.2 KiB
Go

package keeper
import (
"context"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/planetmint/planetmint-go/errormsg"
"github.com/planetmint/planetmint-go/x/dao/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) Distributions(goCtx context.Context, req *types.QueryDistributionsRequest) (*types.QueryDistributionsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, errormsg.InvalidRequest)
}
ctx := sdk.UnwrapSDKContext(goCtx)
distributions := make([]types.DistributionOrder, 0)
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.DistributionKey))
pageRes, err := query.Paginate(store, req.Pagination, func(_ []byte, value []byte) (err error) {
var distribution types.DistributionOrder
err = distribution.Unmarshal(value)
distributions = append(distributions, distribution)
return
})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
}
return &types.QueryDistributionsResponse{Distributions: distributions, Pagination: pageRes}, nil
}