Skip to content

Commit

Permalink
[OTE-780] Add client code for affiliates (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Sep 16, 2024
1 parent 8522246 commit 03454e4
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { AffiliateTiers, AffiliateTiersSDKType } from "./affiliates";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** GenesisState defines generis state of `x/affiliates` */

export interface GenesisState {}
export interface GenesisState {
/** The list of affiliate tiers */
affiliateTiers?: AffiliateTiers;
}
/** GenesisState defines generis state of `x/affiliates` */

export interface GenesisStateSDKType {}
export interface GenesisStateSDKType {
/** The list of affiliate tiers */
affiliate_tiers?: AffiliateTiersSDKType;
}

function createBaseGenesisState(): GenesisState {
return {};
return {
affiliateTiers: undefined
};
}

export const GenesisState = {
encode(_: GenesisState, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
encode(message: GenesisState, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.affiliateTiers !== undefined) {
AffiliateTiers.encode(message.affiliateTiers, writer.uint32(10).fork()).ldelim();
}

return writer;
},

Expand All @@ -25,6 +38,10 @@ export const GenesisState = {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.affiliateTiers = AffiliateTiers.decode(reader, reader.uint32());
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -34,8 +51,9 @@ export const GenesisState = {
return message;
},

fromPartial(_: DeepPartial<GenesisState>): GenesisState {
fromPartial(object: DeepPartial<GenesisState>): GenesisState {
const message = createBaseGenesisState();
message.affiliateTiers = object.affiliateTiers !== undefined && object.affiliateTiers !== null ? AffiliateTiers.fromPartial(object.affiliateTiers) : undefined;
return message;
}

Expand Down
7 changes: 6 additions & 1 deletion proto/dydxprotocol/affiliates/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
syntax = "proto3";
package dydxprotocol.affiliates;
import "gogoproto/gogo.proto";
import "dydxprotocol/affiliates/affiliates.proto";

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

// GenesisState defines generis state of `x/affiliates`
message GenesisState {}
message GenesisState {
// The list of affiliate tiers
AffiliateTiers affiliate_tiers = 1 [ (gogoproto.nullable) = false ];
}
6 changes: 5 additions & 1 deletion protocol/app/testdata/default_genesis_state.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
}
]
},
"affiliates": {},
"affiliates": {
"affiliate_tiers": {
"tiers": []
}
},
"auth": {
"params": {
"max_memo_characters": "256",
Expand Down
8 changes: 6 additions & 2 deletions protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"app_hash": null,
"app_name": "dydxprotocold",
"app_state": {
"affiliates": {},
"affiliates": {
"affiliate_tiers": {
"tiers": []
}
},
"assets": {
"assets": [
{
Expand Down Expand Up @@ -3973,7 +3977,7 @@
]
}
},
"app_version": "5.2.1-19-g04197f43",
"app_version": "5.2.1-103-g5c95dd72",
"chain_id": "dydx-sample-1",
"consensus": {
"params": {
Expand Down
72 changes: 72 additions & 0 deletions protocol/x/affiliates/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -20,5 +21,76 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(
GetCmdQueryAffiliateTiers(),
GetCmdQueryAffiliateInfo(),
GetCmdQueryReferredBy(),
)
return cmd
}

func GetCmdQueryAffiliateTiers() *cobra.Command {
cmd := &cobra.Command{
Use: "affiliate-tiers",
Short: "Query affiliate tiers",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.AllAffiliateTiers(context.Background(), &types.AllAffiliateTiersRequest{})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}

func GetCmdQueryAffiliateInfo() *cobra.Command {
cmd := &cobra.Command{
Use: "affiliate-info [affiliate-address]",
Short: "Query affiliate info",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.AffiliateInfo(context.Background(), &types.AffiliateInfoRequest{
Address: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}

func GetCmdQueryReferredBy() *cobra.Command {
cmd := &cobra.Command{
Use: "referred-by [address]",
Short: "Query the referee that referred the given addresss",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.ReferredBy(context.Background(), &types.ReferredByRequest{
Address: args[0],
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}
74 changes: 74 additions & 0 deletions protocol/x/affiliates/client/cli/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cli_test

import (
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/client"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"

"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/dydxprotocol/v4-chain/protocol/testutil/network"
"github.com/dydxprotocol/v4-chain/protocol/x/affiliates/client/cli"
"github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types"
)

// Prevent strconv unused error
var _ = strconv.IntSize

func setupNetwork(t *testing.T) (*network.Network, client.Context) {
t.Helper()
cfg := network.DefaultConfig(nil)

// Init state.
state := types.GenesisState{}
require.NoError(t, cfg.Codec.UnmarshalJSON(cfg.GenesisState[types.ModuleName], &state))

// Modify default genesis state
state = *types.DefaultGenesis()

// Add test affiliate tiers
state.AffiliateTiers = types.DefaultAffiliateTiers

buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
net := network.New(t, cfg)
ctx := net.Validators[0].ClientCtx

return net, ctx
}

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

out, err := clitestutil.ExecTestCLICmd(ctx, cli.GetCmdQueryAffiliateTiers(), []string{})
require.NoError(t, err)

var resp types.AllAffiliateTiersResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.Equal(t, types.DefaultAffiliateTiers, resp.Tiers)
}

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

testAddress := constants.AliceAccAddress.String()
out, err := clitestutil.ExecTestCLICmd(ctx, cli.GetCmdQueryAffiliateInfo(), []string{testAddress})
require.NoError(t, err)

var resp types.AffiliateInfoResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
}

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

testAddress := constants.AliceAccAddress.String()
out, err := clitestutil.ExecTestCLICmd(ctx, cli.GetCmdQueryReferredBy(), []string{testAddress})
require.NoError(t, err)

var resp types.ReferredByResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
}
24 changes: 24 additions & 0 deletions protocol/x/affiliates/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types"
)

Expand All @@ -18,5 +20,27 @@ func GetTxCmd() *cobra.Command {
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(CmdRegisterAffiliate())
return cmd
}

func CmdRegisterAffiliate() *cobra.Command {
cmd := &cobra.Command{
Use: "register-affiliate [affiliate] [referee]",
Short: "Register an affiliate",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.MsgRegisterAffiliate{
Affiliate: args[0],
Referee: args[1],
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
13 changes: 12 additions & 1 deletion protocol/x/affiliates/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ import (

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
err := k.UpdateAffiliateTiers(ctx, genState.AffiliateTiers)
if err != nil {
panic(err)
}
}

// ExportGenesis returns the module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
return &types.GenesisState{}
affiliateTiers, err := k.GetAllAffiliateTiers(ctx)
if err != nil {
panic(err)
}

return &types.GenesisState{
AffiliateTiers: affiliateTiers,
}
}
6 changes: 1 addition & 5 deletions protocol/x/affiliates/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ func (k Keeper) ReferredBy(ctx context.Context,

affiliateAddr, exists := k.GetReferredBy(sdkCtx, req.GetAddress())
if !exists {
return &types.ReferredByResponse{}, errorsmod.Wrapf(
types.ErrAffiliateNotFound,
"affiliate not found for address: %s",
req.GetAddress(),
)
return &types.ReferredByResponse{}, nil
}

return &types.ReferredByResponse{
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/affiliates/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ func TestReferredBy(t *testing.T) {
AffiliateAddress: constants.BobAccAddress.String(),
},
},
"Affiliate not found": {
"Affiliate not registered": {
req: &types.ReferredByRequest{
Address: constants.DaveAccAddress.String(),
},
setup: func(ctx sdk.Context, k keeper.Keeper) {},
expected: nil,
expectError: types.ErrAffiliateNotFound,
expectError: nil,
},
}

Expand Down
6 changes: 5 additions & 1 deletion protocol/x/affiliates/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package types

// DefaultGenesis returns the default stats genesis state.
func DefaultGenesis() *GenesisState {
return &GenesisState{}
return &GenesisState{
AffiliateTiers: AffiliateTiers{
Tiers: []AffiliateTiers_Tier{},
},
}
}

// Validate performs basic genesis state validation returning an error upon any
Expand Down
Loading

0 comments on commit 03454e4

Please sign in to comment.