Skip to content

Commit

Permalink
Add client code for affiliates
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Sep 13, 2024
1 parent ac1e578 commit 7fdb5c9
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 16 deletions.
8 changes: 7 additions & 1 deletion proto/dydxprotocol/affiliates/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
syntax = "proto3";
package dydxprotocol.affiliates;
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.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 ];
}
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
}
10 changes: 9 additions & 1 deletion protocol/x/affiliates/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import (

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

// 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: 3 additions & 1 deletion protocol/x/affiliates/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package types

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

// Validate performs basic genesis state validation returning an error upon any
Expand Down
76 changes: 68 additions & 8 deletions protocol/x/affiliates/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7fdb5c9

Please sign in to comment.