Skip to content

Commit

Permalink
Merge pull request #277 from terra-money/feat/update-weight-ranges
Browse files Browse the repository at this point in the history
Feat/update weight ranges
  • Loading branch information
emidev98 authored Nov 13, 2023
2 parents 5c1938d + 5b8534e commit 8439e9f
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 113 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
·
<a href="https://docs.alliance.terra.money/guides/get-started">Integration Guide</a>
·
<a href="https://docs.alliance.terra.money/alliance-audit.pdf">Code Audit</a>

<a href="https://github.com/terra-money/alliance/blob/main/audit/alliance-audit-v1.0.pdf">Code Audit</a>
</p>

<br/>
Expand Down
Binary file added audit/alliance-audit-v1.0.pdf
Binary file not shown.
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
1 change: 1 addition & 0 deletions x/alliance/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (k Keeper) UpdateAlliance(ctx context.Context, req *types.MsgUpdateAlliance
RewardWeight: req.RewardWeight,
TakeRate: req.TakeRate,
RewardChangeRate: req.RewardChangeRate,
RewardWeightRange: req.RewardWeightRange,
RewardChangeInterval: req.RewardChangeInterval,
})
return err
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 @@ -741,6 +741,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 @@ -753,6 +754,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 @@ -779,6 +781,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 @@ -1113,6 +1116,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 @@ -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.

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
Loading

0 comments on commit 8439e9f

Please sign in to comment.