Skip to content

Commit

Permalink
feat: allow updating of weight range
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Nov 10, 2023
1 parent 6d5f18a commit 779e0cd
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 110 deletions.
2 changes: 2 additions & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,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 Expand Up @@ -1368,6 +1369,7 @@ Params
| `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/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
5 changes: 5 additions & 0 deletions proto/alliance/alliance/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ message MsgUpdateAlliance {
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true
];

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

message MsgUpdateAllianceResponse {}
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 @@ -82,6 +82,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/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (m MsgServer) UpdateAlliance(ctx context.Context, msg *types.MsgUpdateAllia
if !found {
return nil, types.ErrUnknownAsset
}
asset.RewardWeightRange = msg.RewardWeightRange
if asset.RewardWeightRange.Min.GT(msg.RewardWeight) || asset.RewardWeightRange.Max.LT(msg.RewardWeight) {
return nil, 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 @@ -106,10 +106,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 @@ -123,8 +127,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

0 comments on commit 779e0cd

Please sign in to comment.