mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 16:25:21 +00:00
f0fa2e1253
* module files * proto types * types and generated proto types * keeper * client scaffold * add savings module to app * remove placeholder types file * implement rest and add to module * update proto types * validation for supported denoms * generate updates proto types * update comments * update comments * remove unused imports from proto files * regenerate proto files * update proto types * client * deposit type and generated proto types * deposit keeper methods + tests * update savings module file * update app.go + test common * query proto types * query types + generated proto types * keeper logic for deposits queries * cli/rest querier updates * remove abci * remove refs to other modules * remove endblocker call * genesis init test for module account * update genesis test with params * add get/set params test * fix up keeper test * use params getter * simplify if/else statement * remove querier.go and rest/query * update query deposit description * remove legacy querier * register querier * revisions
106 lines
2.6 KiB
Go
106 lines
2.6 KiB
Go
package keeper
|
|
|
|
import (
|
|
"context"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
|
|
|
"github.com/kava-labs/kava/x/savings/types"
|
|
)
|
|
|
|
type queryServer struct {
|
|
keeper Keeper
|
|
}
|
|
|
|
// NewQueryServerImpl creates a new server for handling gRPC queries.
|
|
func NewQueryServerImpl(k Keeper) types.QueryServer {
|
|
return &queryServer{keeper: k}
|
|
}
|
|
|
|
var _ types.QueryServer = queryServer{}
|
|
|
|
// Params implements the gRPC service handler for querying x/savings parameters.
|
|
func (s queryServer) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
}
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(c)
|
|
params := s.keeper.GetParams(sdkCtx)
|
|
|
|
return &types.QueryParamsResponse{Params: params}, nil
|
|
}
|
|
|
|
func (s queryServer) Deposits(ctx context.Context, req *types.QueryDepositsRequest) (*types.QueryDepositsResponse, error) {
|
|
if req == nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
}
|
|
|
|
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
|
|
|
hasDenom := len(req.Denom) > 0
|
|
hasOwner := len(req.Owner) > 0
|
|
|
|
var owner sdk.AccAddress
|
|
var err error
|
|
if hasOwner {
|
|
owner, err = sdk.AccAddressFromBech32(req.Owner)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, err.Error())
|
|
}
|
|
}
|
|
|
|
var deposits types.Deposits
|
|
switch {
|
|
case hasOwner && hasDenom:
|
|
deposit, found := s.keeper.GetDeposit(sdkCtx, owner)
|
|
if found {
|
|
for _, coin := range deposit.Amount {
|
|
if coin.Denom == req.Denom {
|
|
deposits = append(deposits, deposit)
|
|
}
|
|
}
|
|
}
|
|
case hasOwner:
|
|
deposit, found := s.keeper.GetDeposit(sdkCtx, owner)
|
|
if found {
|
|
deposits = append(deposits, deposit)
|
|
}
|
|
case hasDenom:
|
|
s.keeper.IterateDeposits(sdkCtx, func(deposit types.Deposit) (stop bool) {
|
|
if deposit.Amount.AmountOf(req.Denom).IsPositive() {
|
|
deposits = append(deposits, deposit)
|
|
}
|
|
return false
|
|
})
|
|
default:
|
|
s.keeper.IterateDeposits(sdkCtx, func(deposit types.Deposit) (stop bool) {
|
|
deposits = append(deposits, deposit)
|
|
return false
|
|
})
|
|
}
|
|
|
|
page, limit, err := query.ParsePagination(req.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
start, end := client.Paginate(len(deposits), page, limit, 100)
|
|
if start < 0 || end < 0 {
|
|
deposits = types.Deposits{}
|
|
} else {
|
|
deposits = deposits[start:end]
|
|
}
|
|
|
|
return &types.QueryDepositsResponse{
|
|
Deposits: deposits,
|
|
Pagination: nil,
|
|
}, nil
|
|
}
|