2024-02-15 07:08:32 +00:00
|
|
|
package keeper
|
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2024-02-15 07:08:32 +00:00
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
|
|
|
|
registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ registrytypes.QueryServer = queryServer{}
|
2024-02-15 07:08:32 +00:00
|
|
|
|
|
|
|
type queryServer struct {
|
|
|
|
k Keeper
|
|
|
|
}
|
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
// NewQueryServerImpl returns an implementation of the module QueryServer.
|
|
|
|
func NewQueryServerImpl(k Keeper) registrytypes.QueryServer {
|
|
|
|
return queryServer{k}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs queryServer) Params(c context.Context, _ *registrytypes.QueryParamsRequest) (*registrytypes.QueryParamsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
|
|
|
params, err := qs.k.GetParams(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istrytypes.QueryParamsResponse{Params: params}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs queryServer) Records(c context.Context, req *registrytypes.QueryRecordsRequest) (*registrytypes.QueryRecordsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
|
|
|
attributes := req.GetAttributes()
|
|
|
|
all := req.GetAll()
|
|
|
|
|
|
|
|
var records []registrytypes.Record
|
|
|
|
var err error
|
|
|
|
if len(attributes) > 0 {
|
|
|
|
records, err = qs.k.RecordsFromAttributes(ctx, attributes, all)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
records, err = qs.k.ListRecords(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istrytypes.QueryRecordsResponse{Records: records}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 11:25:15 +00:00
|
|
|
func (qs queryServer) GetRecord(c context.Context, req *registrytypes.QueryGetRecordRequest) (*registrytypes.QueryGetRecordResponse, error) {
|
2024-02-16 06:40:42 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
id := req.GetId()
|
|
|
|
|
|
|
|
if has, err := qs.k.HasRecord(ctx, req.Id); !has {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.")
|
|
|
|
}
|
|
|
|
|
2024-02-26 05:42:36 +00:00
|
|
|
record, err := qs.k.GetRecordById(ctx, id)
|
2024-02-16 06:40:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-07 11:25:15 +00:00
|
|
|
return ®istrytypes.QueryGetRecordResponse{Record: record}, nil
|
2024-02-16 06:40:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 11:25:15 +00:00
|
|
|
func (qs queryServer) GetRecordsByBondId(c context.Context, req *registrytypes.QueryGetRecordsByBondIdRequest) (*registrytypes.QueryGetRecordsByBondIdResponse, error) {
|
2024-02-16 06:40:42 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-02-26 05:42:36 +00:00
|
|
|
|
|
|
|
records, err := qs.k.GetRecordsByBondId(ctx, req.GetId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-07 11:25:15 +00:00
|
|
|
return ®istrytypes.QueryGetRecordsByBondIdResponse{Records: records}, nil
|
2024-02-16 06:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (qs queryServer) GetRegistryModuleBalance(c context.Context,
|
|
|
|
_ *registrytypes.QueryGetRegistryModuleBalanceRequest,
|
|
|
|
) (*registrytypes.QueryGetRegistryModuleBalanceResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
balances := qs.k.GetModuleBalances(ctx)
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
return ®istrytypes.QueryGetRegistryModuleBalanceResponse{
|
|
|
|
Balances: balances,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs queryServer) NameRecords(c context.Context, _ *registrytypes.QueryNameRecordsRequest) (*registrytypes.QueryNameRecordsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-02-26 05:42:36 +00:00
|
|
|
|
|
|
|
nameRecords, err := qs.k.ListNameRecords(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
return ®istrytypes.QueryNameRecordsResponse{Names: nameRecords}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qs queryServer) Whois(c context.Context, request *registrytypes.QueryWhoisRequest) (*registrytypes.QueryWhoisResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-02-26 05:42:36 +00:00
|
|
|
|
|
|
|
nameAuthority, err := qs.k.GetNameAuthority(ctx, request.GetName())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
return ®istrytypes.QueryWhoisResponse{NameAuthority: nameAuthority}, nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
func (qs queryServer) LookupLrn(c context.Context, req *registrytypes.QueryLookupLrnRequest) (*registrytypes.QueryLookupLrnResponse, error) {
|
2024-02-16 06:40:42 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-02-28 04:53:23 +00:00
|
|
|
lrn := req.GetLrn()
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
lrnExists, err := qs.k.HasNameRecord(ctx, lrn)
|
2024-02-26 05:42:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-28 04:53:23 +00:00
|
|
|
if !lrnExists {
|
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "LRN not found.")
|
2024-02-16 06:40:42 +00:00
|
|
|
}
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
nameRecord, err := qs.k.LookupNameRecord(ctx, lrn)
|
2024-02-16 06:40:42 +00:00
|
|
|
if nameRecord == nil {
|
2024-02-26 05:42:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
|
|
|
|
}
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
return ®istrytypes.QueryLookupLrnResponse{Name: nameRecord}, nil
|
2024-02-16 06:40:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
func (qs queryServer) ResolveLrn(c context.Context, req *registrytypes.QueryResolveLrnRequest) (*registrytypes.QueryResolveLrnResponse, error) {
|
2024-02-16 06:40:42 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
lrn := req.GetLrn()
|
|
|
|
record, err := qs.k.ResolveLRN(ctx, lrn)
|
2024-02-16 06:40:42 +00:00
|
|
|
if record == nil {
|
2024-02-26 05:42:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-16 06:40:42 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
|
|
|
|
}
|
2024-02-26 05:42:36 +00:00
|
|
|
|
2024-02-28 04:53:23 +00:00
|
|
|
return ®istrytypes.QueryResolveLrnResponse{Record: record}, nil
|
2024-02-16 06:40:42 +00:00
|
|
|
}
|