Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate token params #364

Merged
merged 4 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion modules/coinswap/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"github.com/irisnet/irismod/types/exported"
)

// ConsensusVersion defines the current coinswap module consensus version.
const ConsensusVersion = 5

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -159,7 +162,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 5 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// ____________________________________________________________________________

Expand Down
5 changes: 4 additions & 1 deletion modules/farm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"github.com/irisnet/irismod/types/exported"
)

// ConsensusVersion defines the current farm module consensus version.
const ConsensusVersion = 3

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -161,7 +164,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 3 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// BeginBlock performs a no-op.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {}
Expand Down
5 changes: 4 additions & 1 deletion modules/htlc/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
"github.com/irisnet/irismod/types/exported"
)

// ConsensusVersion defines the current htlc module consensus version.
const ConsensusVersion = 2

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -155,7 +158,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// BeginBlock performs a no-op.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
5 changes: 4 additions & 1 deletion modules/service/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"github.com/irisnet/irismod/types/exported"
)

// ConsensusVersion defines the current service module consensus version.
const ConsensusVersion = 2

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
Expand Down Expand Up @@ -152,7 +155,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// BeginBlock performs a no-op.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
Expand Down
6 changes: 4 additions & 2 deletions modules/token/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, data v1.GenesisState) {
panic(err.Error())
}

k.SetParamSet(ctx, data.Params)
if err := k.SetParams(ctx, data.Params); err != nil {
panic(err.Error())
}

// init tokens
for _, token := range data.Tokens {
Expand Down Expand Up @@ -43,7 +45,7 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *v1.GenesisState {
tokens = append(tokens, *t)
}
return &v1.GenesisState{
Params: k.GetParamSet(ctx),
Params: k.GetParams(ctx),
Tokens: tokens,
BurnedCoins: k.GetAllBurnCoin(ctx),
}
Expand Down
5 changes: 3 additions & 2 deletions modules/token/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ func (suite *HandlerSuite) SetupTest() {
suite.bk = app.BankKeeper

// set params
suite.keeper.SetParamSet(suite.ctx, v1.DefaultParams())
err := suite.keeper.SetParams(suite.ctx, v1.DefaultParams())
suite.NoError(err)

// init tokens to addr
err := suite.bk.MintCoins(suite.ctx, types.ModuleName, initCoin)
err = suite.bk.MintCoins(suite.ctx, types.ModuleName, initCoin)
suite.NoError(err)
err = suite.bk.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, owner, initCoin)
suite.NoError(err)
Expand Down
8 changes: 5 additions & 3 deletions modules/token/keeper/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ func (k Keeper) GetTokenMintFee(ctx sdk.Context, symbol string) (sdk.Coin, error
}

mintFee := sdk.NewDecFromInt(fee.Amount).Mul(params.MintTokenFeeRatio).TruncateInt()
return token.ToMinCoin(sdk.NewDecCoinFromDec(params.IssueTokenBaseFee.Denom, sdk.NewDecFromInt(mintFee)))
return token.ToMinCoin(
sdk.NewDecCoinFromDec(params.IssueTokenBaseFee.Denom, sdk.NewDecFromInt(mintFee)),
)
}

func (k Keeper) calcTokenIssueFee(ctx sdk.Context, symbol string) (sdk.Coin, v1.Params) {
// get params
params := k.GetParamSet(ctx)
params := k.GetParams(ctx)
issueTokenBaseFee := params.IssueTokenBaseFee

// compute the fee
Expand All @@ -74,7 +76,7 @@ func (k Keeper) calcTokenIssueFee(ctx sdk.Context, symbol string) (sdk.Coin, v1.

// feeHandler handles the fee of token
func feeHandler(ctx sdk.Context, k Keeper, feeAcc sdk.AccAddress, fee sdk.Coin) error {
params := k.GetParamSet(ctx)
params := k.GetParams(ctx)
tokenTaxRate := params.TokenTaxRate

// compute community tax and burned coin
Expand Down
6 changes: 3 additions & 3 deletions modules/token/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (k Keeper) Tokens(
pageRes, err = query.Paginate(
tokenStore,
shapePageRequest(req.Pagination),
func(key []byte, value []byte) error {
func(_ []byte, value []byte) error {
var token v1.Token
k.cdc.MustUnmarshal(value, &token)
tokens = append(tokens, &token)
Expand All @@ -88,7 +88,7 @@ func (k Keeper) Tokens(
}
} else {
tokenStore := prefix.NewStore(store, types.KeyTokens(owner, ""))
pageRes, err = query.Paginate(tokenStore, shapePageRequest(req.Pagination), func(key []byte, value []byte) error {
pageRes, err = query.Paginate(tokenStore, shapePageRequest(req.Pagination), func(_ []byte, value []byte) error {
var symbol gogotypes.StringValue
k.cdc.MustUnmarshal(value, &symbol)
token, err := k.GetToken(ctx, symbol.Value)
Expand Down Expand Up @@ -151,7 +151,7 @@ func (k Keeper) Params(
req *v1.QueryParamsRequest,
) (*v1.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
params := k.GetParamSet(ctx)
params := k.GetParams(ctx)

return &v1.QueryParamsResponse{Params: params}, nil
}
Expand Down
12 changes: 9 additions & 3 deletions modules/token/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ func (suite *KeeperTestSuite) TestGRPCQueryToken() {
_ = suite.app.TokenKeeper.AddToken(ctx, token)

// Query token
tokenResp1, err := queryClient.Token(gocontext.Background(), &v1.QueryTokenRequest{Denom: "btc"})
tokenResp1, err := queryClient.Token(
gocontext.Background(),
&v1.QueryTokenRequest{Denom: "btc"},
)
suite.Require().NoError(err)
suite.Require().NotNil(tokenResp1)

tokenResp2, err := queryClient.Token(gocontext.Background(), &v1.QueryTokenRequest{Denom: "satoshi"})
tokenResp2, err := queryClient.Token(
gocontext.Background(),
&v1.QueryTokenRequest{Denom: "satoshi"},
)
suite.Require().NoError(err)
suite.Require().NotNil(tokenResp2)

Expand Down Expand Up @@ -56,7 +62,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryParams() {
queryClient := v1.NewQueryClient(queryHelper)

paramsResp, err := queryClient.Params(gocontext.Background(), &v1.QueryParamsRequest{})
params := app.TokenKeeper.GetParamSet(ctx)
params := app.TokenKeeper.GetParams(ctx)
suite.Require().NoError(err)
suite.Equal(params, paramsResp.Params)
}
Expand Down
12 changes: 3 additions & 9 deletions modules/token/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/irisnet/irismod/modules/token/types"
v1 "github.com/irisnet/irismod/modules/token/types/v1"
Expand All @@ -20,33 +19,28 @@ type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.Codec
bankKeeper types.BankKeeper
paramSpace paramstypes.Subspace
blockedAddrs map[string]bool
feeCollectorName string
authority string
registry v1.SwapRegistry
}

func NewKeeper(
cdc codec.Codec,
key storetypes.StoreKey,
paramSpace paramstypes.Subspace,
bankKeeper types.BankKeeper,
blockedAddrs map[string]bool,
feeCollectorName string,
authority string,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(v1.ParamKeyTable())
}

return Keeper{
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
bankKeeper: bankKeeper,
feeCollectorName: feeCollectorName,
blockedAddrs: blockedAddrs,
registry: make(v1.SwapRegistry),
authority: authority,
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/token/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (suite *KeeperTestSuite) SetupTest() {
suite.app = app

// set params
suite.keeper.SetParamSet(suite.ctx, v1.DefaultParams())
suite.keeper.SetParams(suite.ctx, v1.DefaultParams())

// init tokens to addr
err := suite.bk.MintCoins(suite.ctx, types.ModuleName, initCoin)
Expand Down
24 changes: 24 additions & 0 deletions modules/token/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/irisnet/irismod/modules/token/migrations/v2"
"github.com/irisnet/irismod/types/exported"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
k Keeper
legacySubspace exported.Subspace
}

// NewMigrator returns a new Migrator.
func NewMigrator(k Keeper, legacySubspace exported.Subspace) Migrator {
return Migrator{k: k, legacySubspace: legacySubspace}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.Migrate(ctx, m.k, m.legacySubspace)
}
Loading