Skip to content

Commit

Permalink
airdrop - user cli txs (#1238)
Browse files Browse the repository at this point in the history
  • Loading branch information
sampocs authored Jul 12, 2024
1 parent 61d4169 commit 42b5ec4
Show file tree
Hide file tree
Showing 10 changed files with 714 additions and 137 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ setup-ics:
###############################################################################
### LocalNet ###
###############################################################################
start-local-node:
start-local-node: build
@bash scripts/start_local_node.sh

###############################################################################
Expand Down
21 changes: 16 additions & 5 deletions proto/stride/airdrop/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ service Msg {
// User facing messages:

// User transaction to claim all the pending daily airdrop rewards
rpc Claim(MsgClaim) returns (MsgClaimResponse);
rpc ClaimDaily(MsgClaimDaily) returns (MsgClaimDailyResponse);

// User transaction to claim half of their total amount now, and forfeit the
// other half to be clawed back
Expand All @@ -28,21 +28,27 @@ service Msg {
// TODO
}

// Claim
message MsgClaim {
// ClaimDaily
message MsgClaimDaily {
option (cosmos.msg.v1.signer) = "claimer";
option (amino.name) = "airdrop/MsgClaim";
option (amino.name) = "airdrop/MsgClaimDaily";

// Address of the claimer
string claimer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// Airdrop ID
string airdrop_id = 2;
}
message MsgClaimResponse {}
message MsgClaimDailyResponse {}

// ClaimEarly
message MsgClaimEarly {
option (cosmos.msg.v1.signer) = "claimer";
option (amino.name) = "airdrop/MsgClaimEarly";

// Address of the claimer
string claimer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// Airdrop ID
string airdrop_id = 2;
}
message MsgClaimEarlyResponse {}

Expand All @@ -51,6 +57,11 @@ message MsgClaimAndStake {
option (cosmos.msg.v1.signer) = "claimer";
option (amino.name) = "airdrop/MsgClaimAndStake";

// Address of the claimer
string claimer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// Airdrop ID
string airdrop_id = 2;
// Validator address to delegate to when staking
string validator_address = 3;
}
message MsgClaimAndStakeResponse {}
133 changes: 132 additions & 1 deletion x/airdrop/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package cli

import (
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"

"github.com/Stride-Labs/stride/v22/x/airdrop/types"
Expand All @@ -19,7 +23,134 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand()
cmd.AddCommand(
CmdClaimDaily(),
CmdClaimEarly(),
CmdClaimAndStake(),
)

return cmd
}

// User transaction to claim all the pending airdrop rewards up to the current day
func CmdClaimDaily() *cobra.Command {
cmd := &cobra.Command{
Use: "claim-daily [airdrop-id]",
Short: "Claims all the pending airdrop rewards up to the current day",
Long: strings.TrimSpace(
fmt.Sprintf(`Claims all pending airdrop rewards up to the current day.
This option is only available if the user has not already elected to claim and stake or claim early
Example:
$ %[1]s tx %[2]s claim-daily airdrop-1 --from user
`, version.AppName, types.ModuleName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
airdropId := args[0]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgClaimDaily(
clientCtx.GetFromAddress().String(),
airdropId,
)

if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// User transaction to claim half of their total amount now, and forfeit the other half to be clawed back
func CmdClaimEarly() *cobra.Command {
cmd := &cobra.Command{
Use: "claim-early [airdrop-id]",
Short: "Claims rewards immediately, but with a early claim penalty",
Long: strings.TrimSpace(
fmt.Sprintf(`Claims rewards immediately (including for future days), but with an early
claim penalty causing a portion of the total to be clawed back.
Example:
$ %[1]s tx %[2]s claim-early airdrop-1 --from user
`, version.AppName, types.ModuleName),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
airdropId := args[0]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgClaimEarly(
clientCtx.GetFromAddress().String(),
airdropId,
)

if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

// User transaction to claim and stake the full airdrop amount
// The rewards will be locked until the end of the distribution period, but will recieve rewards throughout this time
func CmdClaimAndStake() *cobra.Command {
cmd := &cobra.Command{
Use: "claim-and-stake [airdrop-id] [validator-address]",
Short: "Claims and stakes the total reward amount",
Long: strings.TrimSpace(
fmt.Sprintf(`Claims and stakes the full airdrop amount.
The rewards will be locked until the end of the distribution period, but will accrue rewards throughout this time
Example:
$ %[1]s tx %[2]s claim-early airdrop-1 stridevaloperX --from user
`, version.AppName, types.ModuleName),
),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
airdropId := args[0]
vaildatorAddress := args[1]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgClaimAndStake(
clientCtx.GetFromAddress().String(),
airdropId,
vaildatorAddress,
)

if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
5 changes: 3 additions & 2 deletions x/airdrop/keeper/airdrop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
func (s *KeeperTestSuite) addAirdrops() (airdrops []types.Airdrop) {
for i := 0; i <= 4; i++ {
airdrop := types.Airdrop{
Id: fmt.Sprintf("airdrop-%d", i),
EarlyClaimPenalty: sdk.ZeroDec(),
Id: fmt.Sprintf("airdrop-%d", i),
EarlyClaimPenalty: sdk.ZeroDec(),
ClaimAndStakeBonus: sdk.ZeroDec(),
}
airdrops = append(airdrops, airdrop)
s.App.AirdropKeeper.SetAirdrop(s.Ctx, airdrop)
Expand Down
2 changes: 1 addition & 1 deletion x/airdrop/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) Claim(ctx sdk.Context, claimer string) error {
func (k Keeper) ClaimDaily(ctx sdk.Context, claimer string) error {
// TODO[airdrop] implement logic

return nil
Expand Down
15 changes: 7 additions & 8 deletions x/airdrop/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {

var _ types.MsgServer = msgServer{}

// User transaction to claim all the pending daily airdrop rewards
func (ms msgServer) Claim(goCtx context.Context, msg *types.MsgClaim) (*types.MsgClaimResponse, error) {
// User transaction to claim all the pending airdrop rewards up to the current day
func (ms msgServer) ClaimDaily(goCtx context.Context, msg *types.MsgClaimDaily) (*types.MsgClaimDailyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := ms.Keeper.Claim(ctx, msg.Claimer)
err := ms.Keeper.ClaimDaily(ctx, msg.Claimer)
if err != nil {
return nil, err
}

return &types.MsgClaimResponse{}, nil
return &types.MsgClaimDailyResponse{}, nil
}

// User transaction to claim half of their total amount now, and forfeit the
// other half to be clawed back
// User transaction to claim half of their total amount now, and forfeit the other half to be clawed back
func (ms msgServer) ClaimEarly(goCtx context.Context, msg *types.MsgClaimEarly) (*types.MsgClaimEarlyResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand All @@ -45,8 +44,8 @@ func (ms msgServer) ClaimEarly(goCtx context.Context, msg *types.MsgClaimEarly)
return &types.MsgClaimEarlyResponse{}, nil
}

// User transaction to claim and automatically lock stake their whole airdrop
// amount now. The funds can be unstaked by the user once the airdrop is over
// User transaction to claim and stake the full airdrop amount
// The rewards will be locked until the end of the distribution period, but will recieve rewards throughout this time
func (ms msgServer) ClaimAndStake(goCtx context.Context, msg *types.MsgClaimAndStake) (*types.MsgClaimAndStakeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
4 changes: 2 additions & 2 deletions x/airdrop/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
)

func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgClaim{}, "airdrop/MsgClaim")
legacy.RegisterAminoMsg(cdc, &MsgClaimDaily{}, "airdrop/MsgClaimDaily")
legacy.RegisterAminoMsg(cdc, &MsgClaimEarly{}, "airdrop/MsgClaimEarly")
legacy.RegisterAminoMsg(cdc, &MsgClaimAndStake{}, "airdrop/MsgClaimAndStake")
// TODO[airdrop]: add admin messages
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgClaim{},
&MsgClaimDaily{},
&MsgClaimEarly{},
&MsgClaimAndStake{},
// TODO[airdrop]: add admin messages
Expand Down
Loading

0 comments on commit 42b5ec4

Please sign in to comment.