Skip to content

Commit

Permalink
feat(x/bank): support depinject for send restrictions (#20014)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
johnletey and coderabbitai[bot] authored Apr 12, 2024
1 parent 2496cfd commit ab919fe
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 19 deletions.
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.

1 change: 1 addition & 0 deletions x/bank/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

* [#17569](https://github.com/cosmos/cosmos-sdk/pull/17569) Introduce a new message type, `MsgBurn`, to burn coins.
* [#20014](https://github.com/cosmos/cosmos-sdk/pull/20014) Support app wiring for `SendRestrictionFn`.

### Improvements

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

import (
"fmt"
"sort"

"golang.org/x/exp/maps"

modulev1 "cosmossdk.io/api/cosmos/bank/module/v1"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/depinject"
Expand All @@ -18,8 +23,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 +93,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;
}
3 changes: 3 additions & 0 deletions x/bank/types/restrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func ComposeMintingRestrictions(restrictions ...MintingRestrictionFn) MintingRes
// A SendRestrictionFn can restrict sends and/or provide a new receiver address.
type SendRestrictionFn func(ctx context.Context, fromAddr, toAddr sdk.AccAddress, amt sdk.Coins) (newToAddr sdk.AccAddress, err error)

// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (SendRestrictionFn) IsOnePerModuleType() {}

var _ SendRestrictionFn = NoOpSendRestrictionFn

// NoOpSendRestrictionFn is a no-op SendRestrictionFn.
Expand Down

0 comments on commit ab919fe

Please sign in to comment.