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

feat(x/bank): support depinject for send restrictions #20014

Merged
merged 8 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
175 changes: 157 additions & 18 deletions api/cosmos/bank/module/v1/module.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 43 additions & 1 deletion x/bank/depinject.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package bank

import (
"fmt"
"sort"

modulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
Expand All @@ -10,6 +13,7 @@ import (
"cosmossdk.io/x/bank/types"

"github.com/cosmos/cosmos-sdk/codec"
"golang.org/x/exp/maps"
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
)

var _ depinject.OnePerModuleType = AppModule{}
Expand All @@ -18,8 +22,10 @@ var _ depinject.OnePerModuleType = AppModule{}
func (am AppModule) IsOnePerModuleType() {}

func init() {
appconfig.RegisterModule(&modulev1.Module{},
appconfig.RegisterModule(
&modulev1.Module{},
appconfig.Provide(ProvideModule),
appconfig.Invoke(InvokeSetSendRestrictions),
)
}

Expand Down Expand Up @@ -86,3 +92,39 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {

return ModuleOutputs{BankKeeper: bankKeeper, Module: m}
}

func InvokeSetSendRestrictions(
config *modulev1.Module,
keeper keeper.BaseKeeper,
restrictions map[string]types.SendRestrictionFn,
) error {
if config == nil {
return nil
}

modules := maps.Keys(restrictions)
order := config.RestrictionsOrder
if len(order) == 0 {
order = modules
sort.Strings(order)
}

if len(order) != len(modules) {
return fmt.Errorf("len(restrictions order: %v) != len(restriction modules: %v)", order, modules)
}

if len(modules) == 0 {
return nil
}

for _, module := range order {
restriction, ok := restrictions[module]
if !ok {
return fmt.Errorf("can't find send restriction for module %s", module)
}

keeper.AppendSendRestriction(restriction)
}

return nil
}
6 changes: 6 additions & 0 deletions x/bank/proto/cosmos/bank/module/v1/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ message Module {

// authority defines the custom module authority. If not set, defaults to the governance module.
string authority = 2;

// restrictions_order specifies the order of send restrictions and should be
// a list of module names which provide a send restriction instance. If no
// order is provided, then restrictions will be applied in alphabetical order
// of module names.
repeated string restrictions_order = 3;
}
Loading