-
Notifications
You must be signed in to change notification settings - Fork 838
/
upgrades.go
81 lines (72 loc) · 3.05 KB
/
upgrades.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2022 Evmos Foundation
// This file is part of the Evmos Network packages.
//
// Evmos is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Evmos packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE
package v9
import (
"fmt"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
distrKeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/evmos/evmos/v12/utils"
)
// CreateUpgradeHandler creates an SDK upgrade handler for v9
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
dk distrKeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrade", UpgradeName)
if utils.IsMainnet(ctx.ChainID()) {
logger.Debug("recovering lost funds from clawback...")
if err := ReturnFundsFromCommunityPool(ctx, dk); err != nil {
// log error instead of aborting the upgrade
logger.Error("FAILED TO RECOVER FROM COMMUNITY FUNDS", "error", err.Error())
}
}
// Leave modules are as-is to avoid running InitGenesis.
logger.Debug("running module migrations ...")
return mm.RunMigrations(ctx, configurator, vm)
}
}
// ReturnFundsFromCommunityPool handles the return of funds from the community pool to accounts affected during the claims clawback
func ReturnFundsFromCommunityPool(ctx sdk.Context, dk distrKeeper.Keeper) error {
availableCoins, ok := sdk.NewIntFromString(MaxRecover)
if !ok || availableCoins.IsNegative() {
return fmt.Errorf("failed to read maximum amount to recover from community funds")
}
for i := range Accounts {
refund, _ := sdk.NewIntFromString(Accounts[i][1])
if availableCoins.LT(refund) {
return fmt.Errorf("refund exceeds the total available coins: %s > %s", Accounts[i][1], availableCoins)
}
if err := ReturnFundsFromCommunityPoolToAccount(ctx, dk, Accounts[i][0], refund); err != nil {
return err
}
availableCoins = availableCoins.Sub(refund)
}
return nil
}
// ReturnFundsFromCommunityPoolToAccount sends specified amount from the community pool to the affected account
func ReturnFundsFromCommunityPoolToAccount(ctx sdk.Context, dk distrKeeper.Keeper, account string, amount sdkmath.Int) error {
to := sdk.MustAccAddressFromBech32(account)
balance := sdk.Coin{
Denom: "aevmos",
Amount: amount,
}
return dk.DistributeFromFeePool(ctx, sdk.Coins{balance}, to)
}