-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bank/v2): Introduce global send restriction (#21925)
- Loading branch information
Showing
7 changed files
with
301 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/x/bank/v2/types" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// sendRestriction is a struct that houses a SendRestrictionFn. | ||
// It exists so that the SendRestrictionFn can be updated in the SendKeeper without needing to have a pointer receiver. | ||
type sendRestriction struct { | ||
fn types.SendRestrictionFn | ||
} | ||
|
||
// newSendRestriction creates a new sendRestriction with nil send restriction. | ||
func newSendRestriction() *sendRestriction { | ||
return &sendRestriction{ | ||
fn: nil, | ||
} | ||
} | ||
|
||
// append adds the provided restriction to this, to be run after the existing function. | ||
func (r *sendRestriction) append(restriction types.SendRestrictionFn) { | ||
r.fn = r.fn.Then(restriction) | ||
} | ||
|
||
// prepend adds the provided restriction to this, to be run before the existing function. | ||
func (r *sendRestriction) prepend(restriction types.SendRestrictionFn) { | ||
r.fn = restriction.Then(r.fn) | ||
} | ||
|
||
// clear removes the send restriction (sets it to nil). | ||
func (r *sendRestriction) clear() { | ||
r.fn = nil | ||
} | ||
|
||
var _ types.SendRestrictionFn = (*sendRestriction)(nil).apply | ||
|
||
// apply applies the send restriction if there is one. If not, it's a no-op. | ||
func (r *sendRestriction) apply(ctx context.Context, fromAddr, toAddr []byte, amt sdk.Coins) ([]byte, error) { | ||
if r == nil || r.fn == nil { | ||
return toAddr, nil | ||
} | ||
return r.fn(ctx, fromAddr, toAddr, amt) | ||
} | ||
|
||
// AppendSendRestriction adds the provided SendRestrictionFn to run after previously provided restrictions. | ||
func (k Keeper) AppendGlobalSendRestriction(restriction types.SendRestrictionFn) { | ||
k.sendRestriction.append(restriction) | ||
} | ||
|
||
// PrependSendRestriction adds the provided SendRestrictionFn to run before previously provided restrictions. | ||
func (k Keeper) PrependGlobalSendRestriction(restriction types.SendRestrictionFn) { | ||
k.sendRestriction.prepend(restriction) | ||
} | ||
|
||
// ClearSendRestriction removes the send restriction (if there is one). | ||
func (k Keeper) ClearGlobalSendRestriction() { | ||
k.sendRestriction.clear() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.