-
Notifications
You must be signed in to change notification settings - Fork 2
/
upgrades.go
169 lines (148 loc) · 5.68 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package v2
import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/ethereum/go-ethereum/common"
"github.com/tendermint/tendermint/libs/log"
cvntypes "github.com/cvn-network/cvn/v2/types"
erc20keeper "github.com/cvn-network/cvn/v2/x/erc20/keeper"
erc20types "github.com/cvn-network/cvn/v2/x/erc20/types"
feemarketkeeper "github.com/cvn-network/cvn/v2/x/feemarket/keeper"
cvngovtypes "github.com/cvn-network/cvn/v2/x/gov/types"
inflationkeeper "github.com/cvn-network/cvn/v2/x/inflation/keeper"
inflationtypes "github.com/cvn-network/cvn/v2/x/inflation/types"
)
type kvStoreKey interface {
GetKey(storeKey string) *storetypes.KVStoreKey
}
// CreateUpgradeHandler creates an SDK upgrade handler for v2.0.0
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
bank bankkeeper.Keeper,
inflation inflationkeeper.Keeper,
slashing slashingkeeper.Keeper,
feeMarket feemarketkeeper.Keeper,
erc20 erc20keeper.Keeper,
auth authkeeper.AccountKeeper,
kvStoreKey kvStoreKey,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrade", plan.Name)
up := NewUpgrade(logger, bank, inflation, slashing, feeMarket, erc20, auth, kvStoreKey)
logger.Info("running upgrade handler")
up.UpdateGovModuleAccountPermissions(ctx)
up.UpdateSoulTokenPair(ctx)
up.UpdateMetadata(ctx)
up.UpdateModuleParam(ctx)
logger.Info("completed upgrade handler")
return mm.RunMigrations(ctx, configurator, vm)
}
}
type Upgrade struct {
logger log.Logger
auth authkeeper.AccountKeeper
bank bankkeeper.Keeper
inflation inflationkeeper.Keeper
slashing slashingkeeper.Keeper
feeMarket feemarketkeeper.Keeper
erc20 erc20keeper.Keeper
kvStoreKey kvStoreKey
}
func NewUpgrade(
logger log.Logger, bank bankkeeper.Keeper, inflation inflationkeeper.Keeper,
slashing slashingkeeper.Keeper, feeMarket feemarketkeeper.Keeper, erc20 erc20keeper.Keeper,
auth authkeeper.AccountKeeper, kvStoreKey kvStoreKey,
) Upgrade {
return Upgrade{
logger: logger,
bank: bank,
inflation: inflation,
slashing: slashing,
feeMarket: feeMarket,
erc20: erc20,
auth: auth,
kvStoreKey: kvStoreKey,
}
}
func (u Upgrade) UpdateGovModuleAccountPermissions(ctx sdk.Context) {
u.logger.Info("updating gov module account permissions")
account := u.auth.GetModuleAccount(ctx, govtypes.ModuleName)
govModuleAcc, ok := account.(*authtypes.ModuleAccount)
if !ok {
panic("gov module account is not a module account")
}
govModuleAcc.Permissions = append(govModuleAcc.Permissions, authtypes.Minter)
u.auth.SetModuleAccount(ctx, govModuleAcc)
}
func (u Upgrade) UpdateSoulTokenPair(ctx sdk.Context) {
u.logger.Info("updating soul token pair")
u.bank.IterateAllDenomMetaData(ctx, func(metadata banktypes.Metadata) bool {
if metadata.Symbol == "SOUL" {
storeKey := u.kvStoreKey.GetKey(banktypes.ModuleName)
store := ctx.KVStore(storeKey)
store.Delete(append(banktypes.DenomMetadataPrefix, []byte(metadata.Base)...))
tokenContract := metadata.Base[len(erc20types.ModuleName)+1:]
tokenPairID := u.erc20.GetTokenPairID(ctx, tokenContract)
tokenPair, found := u.erc20.GetTokenPair(ctx, tokenPairID)
if !found {
return true
}
u.erc20.DeleteTokenPair(ctx, tokenPair)
tokenPair.Denom = erc20types.CreateBaseDenom(metadata.Symbol)
tokenPair.ContractOwner = erc20types.OWNER_MODULE
u.erc20.SetTokenPair(ctx, tokenPair)
newTokenPairID := tokenPair.GetID()
u.erc20.SetDenomMap(ctx, tokenPair.Denom, newTokenPairID)
u.erc20.SetERC20Map(ctx, common.HexToAddress(tokenPair.Erc20Address), newTokenPairID)
return true
}
return false
})
}
func (u Upgrade) UpdateMetadata(ctx sdk.Context) {
u.logger.Info("updating acvnt denom metadata")
u.bank.SetDenomMetaData(ctx, cvntypes.GetCvnMetadata())
u.logger.Info("updating soul denom metadata")
u.bank.SetDenomMetaData(ctx, cvngovtypes.GetSoulMetadata())
}
func (u Upgrade) UpdateModuleParam(ctx sdk.Context) {
u.logger.Info("updating inflation module params")
inflationParams := u.inflation.GetParams(ctx)
if ctx.ChainID() == "cvn_2032-1" {
inflationParams.ExponentialCalculation = inflationtypes.ExponentialCalculation{
A: sdk.ZeroDec(),
R: sdk.NewDecWithPrec(50, 2),
C: sdk.NewDec(int64(10_000_000)),
BondingTarget: sdk.NewDecWithPrec(66, 2),
MaxVariance: sdk.ZeroDec(),
}
}
inflationParams.InflationDistribution = inflationtypes.InflationDistribution{
StakingRewards: sdk.NewDecWithPrec(85, 2),
UsageIncentives: sdk.NewDecWithPrec(5, 2),
CommunityPool: sdk.NewDecWithPrec(10, 2),
}
if err := u.inflation.SetParams(ctx, inflationParams); err != nil {
panic(err)
}
u.logger.Info("updating slashing module params")
slashingParams := u.slashing.GetParams(ctx)
slashingParams.SignedBlocksWindow = 5000
u.slashing.SetParams(ctx, slashingParams)
u.logger.Info("updating fee market params")
feeMarketParams := u.feeMarket.GetParams(ctx)
feeMarketParams.BaseFee = sdk.NewInt(1e8)
feeMarketParams.MinGasPrice = sdk.NewDecFromInt(feeMarketParams.BaseFee)
if err := u.feeMarket.SetParams(ctx, feeMarketParams); err != nil {
panic(err)
}
}