Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CORE-696] - Add query endpoint for x/rewards GetRewardShare. #673

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add query endpoint for x/rewards GetRewardShare.
Crystal Lemire committed Oct 19, 2023
commit 58bff90966b54e98a5d9d2dcefba4eedf46e4d09
17 changes: 17 additions & 0 deletions proto/dydxprotocol/rewards/query.proto
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
syntax = "proto3";
package dydxprotocol.rewards;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "dydxprotocol/rewards/params.proto";
import "dydxprotocol/rewards/reward_share.proto";

option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/rewards/types";

@@ -13,6 +15,11 @@ service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/dydxprotocol/v4/rewards/params";
}

// Queries a reward share by address.
rpc RewardShare(QueryRewardShareRequest) returns (QueryRewardShareResponse) {
option (google.api.http).get = "/dydxprotocol/v4/rewards/shares/{address}";
}
}

// QueryParamsRequest is a request type for the Params RPC method.
@@ -22,3 +29,13 @@ message QueryParamsRequest {}
message QueryParamsResponse {
Params params = 1 [ (gogoproto.nullable) = false ];
}

// QueryRewardShareRequest is a request type for the RewardShare RPC method.
message QueryRewardShareRequest {
string address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

// QueryRewardShareResponse is a response type for the RewardsShare RPC method.
message QueryRewardShareResponse {
RewardShare reward_share = 1 [ (gogoproto.nullable) = false ];
}
40 changes: 40 additions & 0 deletions protocol/x/rewards/client/cli/query_reward_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/dydxprotocol/v4-chain/protocol/x/rewards/types"
"github.com/spf13/cobra"
)

func CmdQueryRewardShare() *cobra.Command {
cmd := &cobra.Command{
Use: "reward-share [address]",
Short: "shows the reward share for the specified address",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

argAddress := args[0]

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.RewardShare(cmd.Context(), &types.QueryRewardShareRequest{
Address: argAddress,
})

if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
34 changes: 34 additions & 0 deletions protocol/x/rewards/client/cli/query_reward_share_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cli_test

import (
"fmt"
tmcli "github.com/cometbft/cometbft/libs/cli"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/x/rewards/client/cli"
"github.com/dydxprotocol/v4-chain/protocol/x/rewards/types"
"github.com/stretchr/testify/require"
"testing"
)

func TestQueryRewardShare(t *testing.T) {
net, ctx := setupNetwork(t)

out, err := clitestutil.ExecTestCLICmd(
ctx,
cli.CmdQueryRewardShare(),
[]string{
constants.AliceAccAddress.String(),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
)

require.NoError(t, err)
var resp types.QueryRewardShareResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.Equal(t, types.RewardShare{
Address: constants.AliceAccAddress.String(),
Weight: dtypes.NewInt(0),
}, resp.RewardShare)
}
14 changes: 14 additions & 0 deletions protocol/x/rewards/keeper/grpc_query.go
Original file line number Diff line number Diff line change
@@ -19,3 +19,17 @@ func (k Keeper) Params(goCtx context.Context, req *types.QueryParamsRequest) (*t

return &types.QueryParamsResponse{Params: k.GetParams(ctx)}, nil
}

func (k Keeper) RewardShare(
goCtx context.Context,
req *types.QueryRewardShareRequest,
) (*types.QueryRewardShareResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

return &types.QueryRewardShareResponse{
RewardShare: k.GetRewardShare(ctx, req.Address),
}, nil
}
45 changes: 45 additions & 0 deletions protocol/x/rewards/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"testing"

testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
@@ -44,3 +45,47 @@ func TestQueryParams(t *testing.T) {
})
}
}

func TestQueryRewardShare(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.RewardsKeeper

rewardShare := types.RewardShare{
Address: TestAddress1,
Weight: dtypes.NewInt(12_345_678),
}

err := k.SetRewardShare(ctx, rewardShare)
require.NoError(t, err)

for name, tc := range map[string]struct {
req *types.QueryRewardShareRequest
res *types.QueryRewardShareResponse
err error
}{
"Success": {
req: &types.QueryRewardShareRequest{
Address: rewardShare.Address,
},
res: &types.QueryRewardShareResponse{
RewardShare: rewardShare,
},
},
"Nil": {
req: nil,
res: nil,
err: status.Error(codes.InvalidArgument, "invalid request"),
},
} {
t.Run(name, func(t *testing.T) {
res, err := k.RewardShare(ctx, tc.req)
if tc.err != nil {
require.ErrorIs(t, err, tc.err)
} else {
require.NoError(t, err)
require.Equal(t, tc.res, res)
}
})
}
}
424 changes: 408 additions & 16 deletions protocol/x/rewards/types/query.pb.go

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions protocol/x/rewards/types/query.pb.gw.go