Skip to content

Commit ca4ef43

Browse files
committed
add minimum staking amount limitation for farming
1 parent 7930bb9 commit ca4ef43

File tree

6 files changed

+238
-75
lines changed

6 files changed

+238
-75
lines changed

api/side/farming/params.pulsar.go

Lines changed: 121 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/side/farming/params.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ message Asset {
1818
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
1919
(gogoproto.nullable) = false
2020
];
21+
// Minimum staking amount
22+
string min_staking_amount = 3 [
23+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
24+
(gogoproto.nullable) = false
25+
];
2126
}
2227

2328
// Params defines the parameters for the module.

x/farming/keeper/msg_server.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func (m msgServer) Stake(goCtx context.Context, msg *types.MsgStake) (*types.Msg
3232
return nil, errorsmod.Wrapf(types.ErrAssetNotEligible, "asset %s not eligible", msg.Amount.Denom)
3333
}
3434

35+
asset := m.Asset(ctx, msg.Amount.Denom)
36+
if msg.Amount.Amount.LT(asset.MinStakingAmount) {
37+
return nil, errorsmod.Wrapf(types.ErrInvalidAmount, "amount cannot be less than min staking amount %s", asset.MinStakingAmount)
38+
}
39+
3540
if !m.LockDurationExists(ctx, msg.LockDuration) {
3641
return nil, types.ErrInvalidLockDuration
3742
}

x/farming/keeper/params.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ func (k Keeper) IsEligibleAsset(ctx sdk.Context, denom string) bool {
5050
return false
5151
}
5252

53+
// Asset gets the asset by the given denom
54+
func (k Keeper) Asset(ctx sdk.Context, denom string) types.Asset {
55+
for _, asset := range k.EligibleAssets(ctx) {
56+
if asset.Denom == denom {
57+
return asset
58+
}
59+
}
60+
61+
return types.Asset{}
62+
}
63+
5364
// OnParamsChanged is called when the params are changed
5465
func (k Keeper) OnParamsChanged(ctx sdk.Context, params types.Params, newParams types.Params) {
5566
if !params.Enabled && newParams.Enabled {

x/farming/types/params.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ func validateEligibleAssets(p Params) error {
132132
return errorsmod.Wrapf(ErrInvalidParams, "invalid asset denom: %v", err)
133133
}
134134

135-
if asset.RewardRatio.IsNegative() {
136-
return errorsmod.Wrap(ErrInvalidParams, "asset reward ratio cannot be negative")
135+
if err := validateRewardRatio(asset.RewardRatio); err != nil {
136+
return err
137137
}
138138

139-
if asset.RewardRatio.GT(sdkmath.LegacyOneDec()) {
140-
return errorsmod.Wrap(ErrInvalidParams, "asset reward ratio cannot be greater than 1")
139+
if !asset.MinStakingAmount.IsPositive() {
140+
return errorsmod.Wrapf(ErrInvalidParams, "min staking amount must be positive")
141141
}
142142

143143
totalRewardRatio = totalRewardRatio.Add(asset.RewardRatio)
@@ -150,3 +150,16 @@ func validateEligibleAssets(p Params) error {
150150

151151
return nil
152152
}
153+
154+
// validateRewardRatio validates the given reward ratio
155+
func validateRewardRatio(rewardRatio sdkmath.LegacyDec) error {
156+
if rewardRatio.IsNegative() {
157+
return errorsmod.Wrap(ErrInvalidParams, "asset reward ratio cannot be negative")
158+
}
159+
160+
if rewardRatio.GT(sdkmath.LegacyOneDec()) {
161+
return errorsmod.Wrap(ErrInvalidParams, "asset reward ratio cannot be greater than 1")
162+
}
163+
164+
return nil
165+
}

0 commit comments

Comments
 (0)