Skip to content

Commit

Permalink
chore: update proto buf to do error handling in CLI side
Browse files Browse the repository at this point in the history
  • Loading branch information
kenta-elys committed Sep 27, 2023
1 parent 844732a commit d48c561
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 169 deletions.
6 changes: 3 additions & 3 deletions proto/elys/commitment/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ message MsgUpdateVestingInfo {
string baseDenom = 2;
string vestingDenom = 3;
string epochIdentifier = 4;
string numEpochs = 5;
string vestNowFactor = 6;
string numMaxVestings = 7;
int64 numEpochs = 5;
int64 vestNowFactor = 6;
int64 numMaxVestings = 7;
}

message MsgUpdateVestingInfoResponse {}
Expand Down
49 changes: 33 additions & 16 deletions x/commitment/client/cli/tx_update_vesting_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/elys-network/elys/x/commitment/types"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -43,32 +45,32 @@ func CmdUpdateVestingInfo() *cobra.Command {
return err
}

argBaseDenom, err := cmd.Flags().GetString("base-denom")
argBaseDenom, err := cmd.Flags().GetString(types.BaseDenom)
if err != nil {
return err
}

argVestingDenom, err := cmd.Flags().GetString("vesting-denom")
argVestingDenom, err := cmd.Flags().GetString(types.VestingDenom)
if err != nil {
return err
}

argEpochIdentifier, err := cmd.Flags().GetString("epoch-identifier")
argEpochIdentifier, err := cmd.Flags().GetString(types.EpochIdentifier)
if err != nil {
return err
}

argNumEpochs, err := cmd.Flags().GetString("num-epochs")
argNumEpochs, err := cmd.Flags().GetString(types.NumEpochs)
if err != nil {
return err
}

argVestNowFactor, err := cmd.Flags().GetString("vest-now-factor")
argVestNowFactor, err := cmd.Flags().GetString(types.VestNowFactor)
if err != nil {
return err
}

argNumMaxVestings, err := cmd.Flags().GetString("num-max-vestings")
argNumMaxVestings, err := cmd.Flags().GetString(types.NumMaxVestings)
if err != nil {
return err
}
Expand All @@ -78,15 +80,30 @@ func CmdUpdateVestingInfo() *cobra.Command {
return errors.New("signer address is missing")
}

govAddress := sdk.AccAddress(address.Module("gov"))
numEpochs, err := strconv.ParseInt(argNumEpochs, 10, 64)
if err != nil {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %d", argNumEpochs)
}

vestNowFactor, err := strconv.ParseInt(argVestNowFactor, 10, 64)
if err != nil {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %d", argVestNowFactor)
}

maxVestings, err := strconv.ParseInt(argNumMaxVestings, 10, 64)
if err != nil {
return sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %d", argNumMaxVestings)
}

govAddress := sdk.AccAddress(address.Module(govtypes.ModuleName))
msg := types.NewMsgUpdateVestingInfo(
govAddress.String(),
argBaseDenom,
argVestingDenom,
argEpochIdentifier,
argNumEpochs,
argVestNowFactor,
argNumMaxVestings,
numEpochs,
vestNowFactor,
maxVestings,
)
if err := msg.ValidateBasic(); err != nil {
return err
Expand All @@ -111,12 +128,12 @@ func CmdUpdateVestingInfo() *cobra.Command {
},
}

cmd.Flags().String("base-denom", "", "base denom (string)")
cmd.Flags().String("vesting-denom", "", "vesting-denom (string)")
cmd.Flags().String("epoch-identifier", "", "epoch-identifier (string)")
cmd.Flags().String("num-epochs", "", "num-epochs (int64)")
cmd.Flags().String("vest-now-factor", "", "vest-now-factor (decimal)")
cmd.Flags().String("num-max-vestings", "", "num-max-vestings (int64)")
cmd.Flags().String(types.BaseDenom, "", "base denom")
cmd.Flags().String(types.VestingDenom, "", "vesting-denom")
cmd.Flags().String(types.EpochIdentifier, "", "epoch-identifier")
cmd.Flags().String(types.NumEpochs, "", "num-epochs")
cmd.Flags().String(types.VestNowFactor, "", "vest-now-factor")
cmd.Flags().String(types.NumMaxVestings, "", "num-max-vestings")
cmd.Flags().String(cli.FlagTitle, "", "title of proposal")
cmd.Flags().String(cli.FlagSummary, "", "summary of proposal")
cmd.Flags().String(cli.FlagMetadata, "", "metadata of proposal")
Expand Down
28 changes: 6 additions & 22 deletions x/commitment/keeper/msg_server_update_vesting_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -17,40 +16,25 @@ func (k msgServer) UpdateVestingInfo(goCtx context.Context, msg *types.MsgUpdate
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidSigner, "invalid authority; expected %s, got %s", k.authority, msg.Authority)
}

numEpochs, err := strconv.ParseInt(msg.NumEpochs, 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %s", msg.NumEpochs)
}

vestNowFactor, err := strconv.ParseInt(msg.VestNowFactor, 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %s", msg.VestNowFactor)
}

maxVestings, err := strconv.ParseInt(msg.NumMaxVestings, 10, 64)
if err != nil {
return nil, sdkerrors.Wrapf(govtypes.ErrInvalidProposalMsg, "invalid proposal; %s", msg.NumMaxVestings)
}

params := k.GetParams(ctx)
vestingInfo, index := k.GetVestingInfo(ctx, msg.BaseDenom)
if vestingInfo == nil {
vestingInfo = &types.VestingInfo{
BaseDenom: msg.BaseDenom,
VestingDenom: msg.VestingDenom,
EpochIdentifier: msg.EpochIdentifier,
NumEpochs: numEpochs,
VestNowFactor: sdk.NewInt(vestNowFactor),
NumMaxVestings: maxVestings,
NumEpochs: msg.NumEpochs,
VestNowFactor: sdk.NewInt(msg.VestNowFactor),
NumMaxVestings: msg.NumMaxVestings,
}
params.VestingInfos = append(params.VestingInfos, vestingInfo)
} else {
params.VestingInfos[index].BaseDenom = msg.BaseDenom
params.VestingInfos[index].VestingDenom = msg.VestingDenom
params.VestingInfos[index].EpochIdentifier = msg.EpochIdentifier
params.VestingInfos[index].NumEpochs = numEpochs
params.VestingInfos[index].VestNowFactor = sdk.NewInt(vestNowFactor)
params.VestingInfos[index].NumMaxVestings = maxVestings
params.VestingInfos[index].NumEpochs = msg.NumEpochs
params.VestingInfos[index].VestNowFactor = sdk.NewInt(msg.VestNowFactor)
params.VestingInfos[index].NumMaxVestings = msg.NumMaxVestings
}

// store params
Expand Down
12 changes: 6 additions & 6 deletions x/commitment/keeper/msg_server_update_vesting_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func TestAddVestingInfo(t *testing.T) {
BaseDenom: "test_denom",
VestingDenom: "test_denom",
EpochIdentifier: "day",
NumEpochs: "10",
VestNowFactor: "10",
NumMaxVestings: "10",
NumEpochs: 10,
VestNowFactor: 10,
NumMaxVestings: 10,
}
_, err := msgServer.UpdateVestingInfo(ctx, &msg)
require.NoError(t, err)
Expand Down Expand Up @@ -79,9 +79,9 @@ func TestUpdateVestingInfo(t *testing.T) {
BaseDenom: "test_denom",
VestingDenom: "test_denom",
EpochIdentifier: "day",
NumEpochs: "10",
VestNowFactor: "10",
NumMaxVestings: "10",
NumEpochs: 10,
VestNowFactor: 10,
NumMaxVestings: 10,
}
_, err := msgServer.UpdateVestingInfo(ctx, &msg)
require.NoError(t, err)
Expand Down
9 changes: 9 additions & 0 deletions x/commitment/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ const (
MemStoreKey = "mem_commitment"
)

const (
BaseDenom = "base-denom"
VestingDenom = "vesting-denom"
EpochIdentifier = "epoch-identifier"
NumEpochs = "num-epochs"
VestNowFactor = "vest-now-factor"
NumMaxVestings = "num-max-vestings"
)

func KeyPrefix(p string) []byte {
return []byte(p)
}
2 changes: 1 addition & 1 deletion x/commitment/types/message_update_vesting_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TypeMsgUpdateVestingInfo = "update_vesting_info"

var _ sdk.Msg = &MsgUpdateVestingInfo{}

func NewMsgUpdateVestingInfo(creator string, baseDenom string, vestingDenom string, epochIdentifier string, numEpochs string, vestNowFactor string, numMaxVestings string) *MsgUpdateVestingInfo {
func NewMsgUpdateVestingInfo(creator string, baseDenom string, vestingDenom string, epochIdentifier string, numEpochs int64, vestNowFactor int64, numMaxVestings int64) *MsgUpdateVestingInfo {
return &MsgUpdateVestingInfo{
Authority: creator,
BaseDenom: baseDenom,
Expand Down
Loading

0 comments on commit d48c561

Please sign in to comment.