-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write upgrade for new minimum commission rate
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package v4 | ||
|
||
const ( | ||
// UpgradeName defines the on-chain upgrade name. | ||
UpgradeName = "v4-Commission" | ||
NewMinCommisionRate = "0.05" | ||
) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package v4 | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" | ||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" | ||
) | ||
|
||
func CreateUpgradeHandler( | ||
mm *module.Manager, | ||
configurator module.Configurator, | ||
sk stakingkeeper.Keeper, | ||
) upgradetypes.UpgradeHandler { | ||
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { | ||
ctx.Logger().Info("Starting upgrade for multi staking...") | ||
fixMinCommisionRate(ctx, sk) | ||
return mm.RunMigrations(ctx, configurator, vm) | ||
} | ||
} | ||
|
||
func fixMinCommisionRate(ctx sdk.Context, staking stakingkeeper.Keeper) { | ||
// Upgrade every validators min-commission rate | ||
validators := staking.GetAllValidators(ctx) | ||
newComm := sdk.MustNewDecFromStr(NewMinCommisionRate) | ||
params := staking.GetParams(ctx) | ||
params.MinCommissionRate = newComm | ||
staking.SetParams(ctx, params) | ||
for _, v := range validators { | ||
// nolint | ||
if v.Commission.Rate.LT(newComm) { | ||
comm, err := staking.UpdateValidatorCommission(ctx, v, newComm) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
v.Commission = comm | ||
|
||
// call the before-modification hook since we're about to update the commission | ||
staking.BeforeValidatorModified(ctx, v.GetOperator()) | ||
staking.SetValidator(ctx, v) | ||
} | ||
} | ||
} |