Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow updating of weight range #276

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ GenesisState defines the module's genesis state.
| `take_rate` | [string](#string) | | |
| `reward_change_rate` | [string](#string) | | |
| `reward_change_interval` | [google.protobuf.Duration](#google.protobuf.Duration) | | |
| `reward_weight_range` | [RewardWeightRange](#alliance.alliance.RewardWeightRange) | | set a bound of weight range to limit how much reward weights can scale. |



Expand Down
4 changes: 4 additions & 0 deletions proto/alliance/gov.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ message MsgUpdateAllianceProposal {
(gogoproto.stdduration) = true
];

// set a bound of weight range to limit how much reward weights can scale.
RewardWeightRange reward_weight_range = 8 [
(gogoproto.nullable) = false
];
}

message MsgDeleteAllianceProposal {
Expand Down
16 changes: 15 additions & 1 deletion x/alliance/client/cli/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func CreateAlliance() *cobra.Command {

func UpdateAlliance() *cobra.Command {
cmd := &cobra.Command{
Use: "update-alliance denom reward-weight take-rate reward-change-rate reward-change-interval",
Use: "update-alliance denom reward-weight reward-weight-min reward-weight-max take-rate reward-change-rate reward-change-interval",
Args: cobra.ExactArgs(5),
Short: "Update an alliance with the specified parameters",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -143,6 +143,16 @@ func UpdateAlliance() *cobra.Command {
return err
}

rewardWeightMin, err := sdk.NewDecFromStr(args[2])
if err != nil {
return err
}

rewardWeightMax, err := sdk.NewDecFromStr(args[3])
if err != nil {
return err
}

takeRate, err := sdk.NewDecFromStr(args[2])
if err != nil {
return err
Expand Down Expand Up @@ -175,6 +185,10 @@ func UpdateAlliance() *cobra.Command {
description,
denom,
rewardWeight,
types.RewardWeightRange{
Min: rewardWeightMin,
Max: rewardWeightMax,
},
takeRate,
rewardChangeRate,
rewardChangeInterval,
Expand Down
1 change: 1 addition & 0 deletions x/alliance/keeper/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (k Keeper) UpdateAllianceAsset(ctx sdk.Context, newAsset types.AllianceAsse
asset.RewardChangeRate = newAsset.RewardChangeRate
asset.RewardChangeInterval = newAsset.RewardChangeInterval
asset.LastRewardChangeTime = newAsset.LastRewardChangeTime
asset.RewardWeightRange = newAsset.RewardWeightRange
k.SetAsset(ctx, asset)

return nil
Expand Down
1 change: 1 addition & 0 deletions x/alliance/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (k Keeper) UpdateAlliance(ctx context.Context, req *types.MsgUpdateAlliance
if !found {
return status.Errorf(codes.NotFound, "Asset with denom: %s does not exist", req.Denom)
}
asset.RewardWeightRange = req.RewardWeightRange
if asset.RewardWeightRange.Min.GT(req.RewardWeight) || asset.RewardWeightRange.Max.LT(req.RewardWeight) {
return types.ErrRewardWeightOutOfBound
}
Expand Down
16 changes: 10 additions & 6 deletions x/alliance/keeper/tests/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ func TestUpdateAlliance(t *testing.T) {

// WHEN
updateErr := app.AllianceKeeper.UpdateAlliance(ctx, &types.MsgUpdateAllianceProposal{
Title: "",
Description: "",
Denom: "uluna",
RewardWeight: sdk.NewDec(6),
Title: "",
Description: "",
Denom: "uluna",
RewardWeight: sdk.NewDec(11),
RewardWeightRange: types.RewardWeightRange{
Min: sdk.NewDec(0),
Max: sdk.NewDec(11),
},
TakeRate: sdk.NewDec(7),
RewardChangeInterval: 0,
RewardChangeRate: sdk.ZeroDec(),
Expand All @@ -120,8 +124,8 @@ func TestUpdateAlliance(t *testing.T) {
Alliances: []types.AllianceAsset{
{
Denom: "uluna",
RewardWeight: sdk.NewDec(6),
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(10)},
RewardWeight: sdk.NewDec(11),
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(11)},
TakeRate: sdk.NewDec(7),
TotalTokens: sdk.ZeroInt(),
TotalValidatorShares: sdk.NewDec(0),
Expand Down
16 changes: 15 additions & 1 deletion x/alliance/types/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (m *MsgCreateAllianceProposal) ValidateBasic() error {
return nil
}

func NewMsgUpdateAllianceProposal(title, description, denom string, rewardWeight, takeRate sdk.Dec, rewardChangeRate sdk.Dec, rewardChangeInterval time.Duration) govtypes.Content {
func NewMsgUpdateAllianceProposal(title, description, denom string, rewardWeight sdk.Dec, rewardWeightRange RewardWeightRange, takeRate sdk.Dec, rewardChangeRate sdk.Dec, rewardChangeInterval time.Duration) govtypes.Content {
return &MsgUpdateAllianceProposal{
Title: title,
Description: description,
Expand All @@ -94,6 +94,7 @@ func NewMsgUpdateAllianceProposal(title, description, denom string, rewardWeight
TakeRate: takeRate,
RewardChangeRate: rewardChangeRate,
RewardChangeInterval: rewardChangeInterval,
RewardWeightRange: rewardWeightRange,
}
}
func (m *MsgUpdateAllianceProposal) GetTitle() string { return m.Title }
Expand Down Expand Up @@ -122,6 +123,19 @@ func (m *MsgUpdateAllianceProposal) ValidateBasic() error {
return status.Errorf(codes.InvalidArgument, "Alliance rewardChangeInterval must be strictly a positive number")
}

if m.RewardWeightRange.Min.IsNil() || m.RewardWeightRange.Min.LT(sdk.ZeroDec()) ||
m.RewardWeightRange.Max.IsNil() || m.RewardWeightRange.Max.LT(sdk.ZeroDec()) {
return status.Errorf(codes.InvalidArgument, "Alliance rewardWeight min and max must be zero or a positive number")
}

if m.RewardWeightRange.Min.GT(m.RewardWeightRange.Max) {
return status.Errorf(codes.InvalidArgument, "Alliance rewardWeight min must be less or equal to rewardWeight max")
}

if m.RewardWeight.LT(m.RewardWeightRange.Min) || m.RewardWeight.GT(m.RewardWeightRange.Max) {
return status.Errorf(codes.InvalidArgument, "Alliance rewardWeight must be bounded in RewardWeightRange")
}

return nil
}

Expand Down
116 changes: 81 additions & 35 deletions x/alliance/types/gov.pb.go

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

4 changes: 2 additions & 2 deletions x/alliance/types/tests/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestProposalsContent(t *testing.T) {
str: "title:\"Alliance1\" description:\"Alliance with 1\" denom:\"ibc/denom1\" reward_weight:\"1000000000000000000\" take_rate:\"1000000000000000000\" reward_change_rate:\"1000000000000000000\" reward_change_interval:<seconds:1 > reward_weight_range:<min:\"0\" max:\"5000000000000000000\" > ",
},
"msg_update_alliance_proposal": {
p: types.NewMsgUpdateAllianceProposal("Alliance2", "Alliance with 2", "ibc/denom2", sdk.NewDec(2), sdk.NewDec(2), sdk.NewDec(2), time.Hour),
p: types.NewMsgUpdateAllianceProposal("Alliance2", "Alliance with 2", "ibc/denom2", sdk.NewDec(2), types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(5)}, sdk.NewDec(2), sdk.NewDec(2), time.Hour),
title: "Alliance2",
desc: "Alliance with 2",
typ: "msg_update_alliance_proposal",
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestInvalidProposalsContent(t *testing.T) {
typ: "msg_create_alliance_proposal",
},
"msg_update_alliance_proposal": {
p: types.NewMsgUpdateAllianceProposal("Alliance2", "Alliance with 2", "ibc/denom2", sdk.NewDec(2), sdk.NewDec(2), sdk.NewDec(2), -time.Hour),
p: types.NewMsgUpdateAllianceProposal("Alliance2", "Alliance with 2", "ibc/denom2", sdk.NewDec(2), types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(5)}, sdk.NewDec(2), sdk.NewDec(2), -time.Hour),
title: "Alliance2",
desc: "Alliance with 2",
typ: "msg_update_alliance_proposal",
Expand Down
Loading