Skip to content

Commit

Permalink
refactor: supplier query client
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 11, 2024
1 parent a19f686 commit e91c0c7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
3 changes: 3 additions & 0 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
)

// MsgCreateClaim is an interface satisfying proof.MsgCreateClaim concrete type
Expand Down Expand Up @@ -280,6 +281,8 @@ type ApplicationQueryClient interface {
// SupplierQueryClient defines an interface that enables the querying of the
// on-chain supplier information
type SupplierQueryClient interface {
ParamsQuerier[*suppliertypes.Params]

// GetSupplier queries the chain for the details of the supplier provided
GetSupplier(ctx context.Context, supplierOperatorAddress string) (sharedtypes.Supplier, error)
}
Expand Down
39 changes: 31 additions & 8 deletions pkg/client/query/supplierquerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import (
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
)

var _ client.SupplierQueryClient = (*supplierQuerier)(nil)

// supplierQuerier is a wrapper around the suppliertypes.QueryClient that enables the
// querying of on-chain supplier information through a single exposed method
// which returns an sharedtypes.Supplier struct
type supplierQuerier struct {
client.ParamsQuerier[*suppliertypes.Params]

clientConn grpc.ClientConn
supplierQuerier suppliertypes.QueryClient
}
Expand All @@ -24,28 +28,47 @@ type supplierQuerier struct {
//
// Required dependencies:
// - grpc.ClientConn
func NewSupplierQuerier(deps depinject.Config) (client.SupplierQueryClient, error) {
supq := &supplierQuerier{}
func NewSupplierQuerier(
deps depinject.Config,
paramsQuerierOpts ...ParamsQuerierOptionFn,
) (client.SupplierQueryClient, error) {
paramsQuerierCfg := DefaultParamsQuerierConfig()
for _, opt := range paramsQuerierOpts {
opt(paramsQuerierCfg)
}

paramsQuerier, err := NewCachedParamsQuerier[*suppliertypes.Params, suppliertypes.SupplierQueryClient](
deps, suppliertypes.NewSupplierQueryClient,
WithModuleInfo[*suppliertypes.Params](suppliertypes.ModuleName, suppliertypes.ErrSupplierParamInvalid),
WithParamsCacheOptions(paramsQuerierCfg.CacheOpts...),
)
if err != nil {
return nil, err
}

sq := &supplierQuerier{
ParamsQuerier: paramsQuerier,
}

if err := depinject.Inject(
if err = depinject.Inject(
deps,
&supq.clientConn,
&sq.clientConn,
); err != nil {
return nil, err
}

supq.supplierQuerier = suppliertypes.NewQueryClient(supq.clientConn)
sq.supplierQuerier = suppliertypes.NewQueryClient(sq.clientConn)

return supq, nil
return sq, nil
}

// GetSupplier returns an suppliertypes.Supplier struct for a given address
func (supq *supplierQuerier) GetSupplier(
func (sq *supplierQuerier) GetSupplier(
ctx context.Context,
operatorAddress string,
) (sharedtypes.Supplier, error) {
req := &suppliertypes.QueryGetSupplierRequest{OperatorAddress: operatorAddress}
res, err := supq.supplierQuerier.Supplier(ctx, req)
res, err := sq.supplierQuerier.Supplier(ctx, req)
if err != nil {
return sharedtypes.Supplier{}, suppliertypes.ErrSupplierNotFound.Wrapf(
"address: %s [%v]",
Expand Down
31 changes: 31 additions & 0 deletions x/supplier/types/query_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package types

import (
"context"

gogogrpc "github.com/cosmos/gogoproto/grpc"
)

// TODO_IN_THIS_COMMIT: godoc...
type SupplierQueryClient interface {
QueryClient

GetParams(context.Context) (*Params, error)
}

// TODO_IN_THIS_COMMIT: godoc...
func NewSupplierQueryClient(conn gogogrpc.ClientConn) SupplierQueryClient {
return NewQueryClient(conn).(SupplierQueryClient)
}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
func (c *queryClient) GetParams(ctx context.Context) (*Params, error) {
res, err := c.Params(ctx, &QueryParamsRequest{})
if err != nil {
return nil, err
}

params := res.GetParams()
return &params, nil
}

0 comments on commit e91c0c7

Please sign in to comment.