-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add Proposal for multistaking module (#131)
* add test proposal * nit * add clinet * remove unused code * lint * add codec * add registry * lint * nit * proto * nit * add proposal bond weight * add codec * add cli * add test * nit * lint * nit * add check bondweight * update cli --------- Co-authored-by: Hieu Vu <gundamaster5@gmail.com>
- Loading branch information
Showing
14 changed files
with
356 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/realio-tech/multi-staking-module/x/multi-staking/types" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/client/tx" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" | ||
) | ||
|
||
func NewCmdSubmitAddMultiStakingCoinProposal() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "add-multistaking-coin [title] [description] [denom] [bond_weight] [deposit]", | ||
Args: cobra.ExactArgs(5), | ||
Short: "Submit an add multistaking coin proposal", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bondWeight, err := sdk.NewDecFromStr(args[3]) | ||
if err != nil { | ||
return err | ||
} | ||
from := clientCtx.GetFromAddress() | ||
content := types.NewAddMultiStakingCoinProposal( | ||
args[0], args[1], args[2], bondWeight, | ||
) | ||
|
||
deposit, err := sdk.ParseCoinsNormalized(args[4]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg, err := govv1beta1.NewMsgSubmitProposal(content, deposit, from) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func NewCmdUpdateBondWeightProposal() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update-bond-weight [title] [description] [denom] [bond_weight] [deposit]", | ||
Args: cobra.ExactArgs(5), | ||
Short: "Submit update bond weight for bond coin proposal", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bondWeight, err := sdk.NewDecFromStr(args[3]) | ||
if err != nil { | ||
return err | ||
} | ||
from := clientCtx.GetFromAddress() | ||
content := types.NewUpdateBondWeightProposal( | ||
args[0], args[1], args[2], bondWeight, | ||
) | ||
|
||
deposit, err := sdk.ParseCoinsNormalized(args[4]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
msg, err := govv1beta1.NewMsgSubmitProposal(content, deposit, from) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/realio-tech/multi-staking-module/x/multi-staking/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// AddMultiStakingCoinProposal handles the proposals to add a new bond token | ||
func (k Keeper) AddMultiStakingCoinProposal( | ||
ctx sdk.Context, | ||
p *types.AddMultiStakingCoinProposal, | ||
) error { | ||
_, found := k.GetBondWeight(ctx, p.Denom) | ||
if found { | ||
return fmt.Errorf("Error MultiStakingCoin %s already exist", p.Denom) //nolint:stylecheck | ||
} | ||
|
||
bondWeight := *p.BondWeight | ||
if bondWeight.LTE(sdk.ZeroDec()) { | ||
return fmt.Errorf("Error MultiStakingCoin BondWeight %s invalid", bondWeight) //nolint:stylecheck | ||
} | ||
|
||
k.SetBondWeight(ctx, p.Denom, bondWeight) | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
types.EventTypeAddMultiStakingCoin, | ||
sdk.NewAttribute(types.AttributeKeyDenom, p.Denom), | ||
sdk.NewAttribute(types.AttributeKeyBondWeight, p.BondWeight.String()), | ||
), | ||
) | ||
return nil | ||
} | ||
|
||
func (k Keeper) BondWeightProposal( | ||
ctx sdk.Context, | ||
p *types.UpdateBondWeightProposal, | ||
) error { | ||
_, found := k.GetBondWeight(ctx, p.Denom) | ||
if !found { | ||
return fmt.Errorf("Error MultiStakingCoin %s not found", p.Denom) //nolint:stylecheck | ||
} | ||
|
||
bondWeight := *p.UpdatedBondWeight | ||
if bondWeight.LTE(sdk.ZeroDec()) { | ||
return fmt.Errorf("Error MultiStakingCoin BondWeight %s invalid", bondWeight) //nolint:stylecheck | ||
} | ||
|
||
k.SetBondWeight(ctx, p.Denom, bondWeight) | ||
|
||
ctx.EventManager().EmitEvent( | ||
sdk.NewEvent( | ||
types.EventTypeAddMultiStakingCoin, | ||
sdk.NewAttribute(types.AttributeKeyDenom, p.Denom), | ||
sdk.NewAttribute(types.AttributeKeyBondWeight, p.UpdatedBondWeight.String()), | ||
), | ||
) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"github.com/realio-tech/multi-staking-module/x/multi-staking/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
govv1types "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestAddMultiStakingCoinProposal() { | ||
bondWeight := sdk.NewDec(1) | ||
|
||
for _, tc := range []struct { | ||
desc string | ||
malleate func(p *types.AddMultiStakingCoinProposal) | ||
proposal *types.AddMultiStakingCoinProposal | ||
shouldErr bool | ||
}{ | ||
{ | ||
desc: "Success", | ||
malleate: func(p *types.AddMultiStakingCoinProposal) { | ||
_, found := suite.msKeeper.GetBondWeight(suite.ctx, p.Denom) | ||
suite.Require().False(found) | ||
}, | ||
proposal: &types.AddMultiStakingCoinProposal{ | ||
Title: "Add multistaking coin", | ||
Description: "Add new multistaking coin", | ||
Denom: "stake1", | ||
BondWeight: &bondWeight, | ||
}, | ||
shouldErr: false, | ||
}, | ||
{ | ||
desc: "Error multistaking coin already exists", | ||
malleate: func(p *types.AddMultiStakingCoinProposal) { | ||
suite.msKeeper.SetBondWeight(suite.ctx, p.Denom, *p.BondWeight) | ||
}, | ||
proposal: &types.AddMultiStakingCoinProposal{ | ||
Title: "Add multistaking coin", | ||
Description: "Add new multistaking coin", | ||
Denom: "stake2", | ||
BondWeight: &bondWeight, | ||
}, | ||
shouldErr: true, | ||
}, | ||
} { | ||
tc := tc | ||
suite.Run(tc.desc, func() { | ||
suite.SetupTest() | ||
tc.malleate(tc.proposal) | ||
|
||
legacyProposal, err := govv1types.NewLegacyContent(tc.proposal, authtypes.NewModuleAddress(govtypes.ModuleName).String()) | ||
suite.Require().NoError(err) | ||
|
||
if !tc.shouldErr { | ||
// store proposal | ||
_, err = suite.govKeeper.SubmitProposal(suite.ctx, []sdk.Msg{legacyProposal}, "") | ||
suite.Require().NoError(err) | ||
|
||
// execute proposal | ||
handler := suite.govKeeper.LegacyRouter().GetRoute(tc.proposal.ProposalRoute()) | ||
err = handler(suite.ctx, tc.proposal) | ||
suite.Require().NoError(err) | ||
|
||
_, found := suite.msKeeper.GetBondWeight(suite.ctx, tc.proposal.Denom) | ||
suite.Require().True(found) | ||
} else { | ||
// store proposal | ||
_, err = suite.govKeeper.SubmitProposal(suite.ctx, []sdk.Msg{legacyProposal}, "") | ||
suite.Require().Error(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (suite *KeeperTestSuite) TestUpdateBondWeightProposal() { | ||
bondWeight := sdk.NewDec(1) | ||
|
||
for _, tc := range []struct { | ||
desc string | ||
malleate func(p *types.UpdateBondWeightProposal) | ||
proposal *types.UpdateBondWeightProposal | ||
shouldErr bool | ||
}{ | ||
{ | ||
desc: "Success", | ||
malleate: func(p *types.UpdateBondWeightProposal) { | ||
oldBondWeight := sdk.NewDec(2) | ||
suite.msKeeper.SetBondWeight(suite.ctx, p.Denom, oldBondWeight) | ||
}, | ||
proposal: &types.UpdateBondWeightProposal{ | ||
Title: "Add multistaking coin", | ||
Description: "Add new multistaking coin", | ||
Denom: "stake1", | ||
UpdatedBondWeight: &bondWeight, | ||
}, | ||
shouldErr: false, | ||
}, | ||
{ | ||
desc: "Error multistaking coin not exists", | ||
malleate: func(p *types.UpdateBondWeightProposal) {}, | ||
proposal: &types.UpdateBondWeightProposal{ | ||
Title: "Add multistaking coin", | ||
Description: "Add new multistaking coin", | ||
Denom: "stake2", | ||
UpdatedBondWeight: &bondWeight, | ||
}, | ||
shouldErr: true, | ||
}, | ||
} { | ||
tc := tc | ||
suite.Run(tc.desc, func() { | ||
suite.SetupTest() | ||
tc.malleate(tc.proposal) | ||
|
||
legacyProposal, err := govv1types.NewLegacyContent(tc.proposal, authtypes.NewModuleAddress(govtypes.ModuleName).String()) | ||
suite.Require().NoError(err) | ||
|
||
if !tc.shouldErr { | ||
// store proposal | ||
_, err = suite.govKeeper.SubmitProposal(suite.ctx, []sdk.Msg{legacyProposal}, "") | ||
suite.Require().NoError(err) | ||
|
||
// execute proposal | ||
handler := suite.govKeeper.LegacyRouter().GetRoute(tc.proposal.ProposalRoute()) | ||
err = handler(suite.ctx, tc.proposal) | ||
suite.Require().NoError(err) | ||
|
||
weight, found := suite.msKeeper.GetBondWeight(suite.ctx, tc.proposal.Denom) | ||
suite.Require().True(found) | ||
suite.Require().True(weight.Equal(bondWeight)) | ||
} else { | ||
// store proposal | ||
_, err = suite.govKeeper.SubmitProposal(suite.ctx, []sdk.Msg{legacyProposal}, "") | ||
suite.Require().Error(err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.