From f24a3d3c6aa002bb6c03c05db235e6393ac66846 Mon Sep 17 00:00:00 2001 From: Vladislav Mikitich Date: Sun, 3 Mar 2024 23:25:25 +0300 Subject: [PATCH] Mint gas to new accounts on ibc operations --- app/app.go | 5 ++-- go.mod | 1 - x/scorum/wrapper/account_keeper.go | 40 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 x/scorum/wrapper/account_keeper.go diff --git a/app/app.go b/app/app.go index 93ff2e1..bfa8bd9 100644 --- a/app/app.go +++ b/app/app.go @@ -112,6 +112,7 @@ import ( scorumclient "github.com/scorum/cosmos-network/x/scorum/client" scorumkeeper "github.com/scorum/cosmos-network/x/scorum/keeper" scorumtypes "github.com/scorum/cosmos-network/x/scorum/types" + scorumwrapper "github.com/scorum/cosmos-network/x/scorum/wrapper" "github.com/scorum/cosmos-network/x/aviatrix" aviatrixkeeper "github.com/scorum/cosmos-network/x/aviatrix/keeper" @@ -446,7 +447,7 @@ func New( app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, - app.AccountKeeper, + scorumwrapper.NewAccountKeeper(app.AccountKeeper, app.BankKeeper, app.ScorumKeeper), app.BankKeeper, scopedTransferKeeper, ) @@ -459,7 +460,7 @@ func New( app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper, - app.AccountKeeper, + scorumwrapper.NewAccountKeeper(app.AccountKeeper, app.BankKeeper, app.ScorumKeeper), scopedICAHostKeeper, app.MsgServiceRouter(), ) diff --git a/go.mod b/go.mod index d4b862e..8b63cdd 100644 --- a/go.mod +++ b/go.mod @@ -167,7 +167,6 @@ require ( ) replace ( - // github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0 github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1 github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.27 ) diff --git a/x/scorum/wrapper/account_keeper.go b/x/scorum/wrapper/account_keeper.go new file mode 100644 index 0000000..8958c6c --- /dev/null +++ b/x/scorum/wrapper/account_keeper.go @@ -0,0 +1,40 @@ +package wrap + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/x/auth/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + scorumkeeper "github.com/scorum/cosmos-network/x/scorum/keeper" + scorumtypes "github.com/scorum/cosmos-network/x/scorum/types" +) + +// AccountKeeper is a wrapper of cosmos-sdk/x/auth/keeper.AccountKeeperI that mints gas on setting new account. +// It's used to allow free-gas transactions without registration. +type AccountKeeper struct { + accountkeeper.AccountKeeper + bk bankkeeper.Keeper + sk scorumkeeper.Keeper +} + +func NewAccountKeeper(ak accountkeeper.AccountKeeper, bk bankkeeper.Keeper, sk scorumkeeper.Keeper) AccountKeeper { + return AccountKeeper{ + AccountKeeper: ak, + bk: bk, + sk: sk, + } +} + +func (k AccountKeeper) SetAccount(ctx sdk.Context, acc types.AccountI) { + hasAccount := k.AccountKeeper.HasAccount(ctx, acc.GetAddress()) + // must be set before minting to avoid recursion (BankKeeper calls SetAccount if it's not created yet) + k.AccountKeeper.SetAccount(ctx, acc) + if !hasAccount { + if err := k.sk.Mint(ctx, acc.GetAddress(), sdk.NewCoin(scorumtypes.GasDenom, k.sk.GetParams(ctx).GasLimit.Int)); err != nil { + panic(fmt.Sprintf("failed to mint gas to new account: %s", err.Error())) + } + } +}