Skip to content

Commit

Permalink
Merge pull request #276 from terra-money/feat/0.1/update-weight-range
Browse files Browse the repository at this point in the history
feat: allow updating of weight range
  • Loading branch information
emidev98 authored Nov 13, 2023
2 parents 014f68f + 9b41d87 commit 7bc4802
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 46 deletions.
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
2 changes: 2 additions & 0 deletions x/alliance/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ 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
4 changes: 4 additions & 0 deletions x/alliance/keeper/tests/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ func TestRewardWeightDecay(t *testing.T) {
Description: "",
Denom: AllianceDenom,
RewardWeight: sdk.MustNewDecFromStr("0.5"),
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(5)},
TakeRate: sdk.ZeroDec(),
RewardChangeRate: sdk.ZeroDec(),
RewardChangeInterval: 0,
Expand All @@ -752,6 +753,7 @@ func TestRewardWeightDecay(t *testing.T) {
Description: "",
Denom: AllianceDenom,
RewardWeight: sdk.MustNewDecFromStr("0.5"),
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(5)},
TakeRate: sdk.ZeroDec(),
RewardChangeRate: sdk.MustNewDecFromStr("0.1"),
RewardChangeInterval: decayInterval,
Expand All @@ -778,6 +780,7 @@ func TestRewardWeightDecay(t *testing.T) {
Description: "",
Denom: AllianceDenomTwo,
RewardWeight: sdk.MustNewDecFromStr("0.5"),
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(5)},
TakeRate: sdk.ZeroDec(),
RewardChangeRate: sdk.MustNewDecFromStr("0.1"),
RewardChangeInterval: decayInterval,
Expand Down Expand Up @@ -1112,6 +1115,7 @@ func TestRewardWeightRateChange(t *testing.T) {
Description: "",
Denom: alliance.Denom,
RewardWeight: alliance.RewardWeight,
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(0), Max: sdk.NewDec(5)},
TakeRate: alliance.TakeRate,
RewardChangeRate: sdk.MustNewDecFromStr("1.001"),
RewardChangeInterval: time.Minute * 5,
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.

6 changes: 3 additions & 3 deletions x/alliance/types/tests/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ 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",
str: "title:\"Alliance2\" description:\"Alliance with 2\" denom:\"ibc/denom2\" reward_weight:\"2000000000000000000\" take_rate:\"2000000000000000000\" reward_change_rate:\"2000000000000000000\" reward_change_interval:<seconds:3600 > ",
str: "title:\"Alliance2\" description:\"Alliance with 2\" denom:\"ibc/denom2\" reward_weight:\"2000000000000000000\" take_rate:\"2000000000000000000\" reward_change_rate:\"2000000000000000000\" reward_change_interval:<seconds:3600 > reward_weight_range:<min:\"0\" max:\"5000000000000000000\" > ",
},
"msg_delete_alliance_proposal": {
p: types.NewMsgDeleteAllianceProposal("test", "abcd", "ibc/denom"),
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

0 comments on commit 7bc4802

Please sign in to comment.