-
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.
- Loading branch information
Showing
4 changed files
with
174 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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) | ||
} |
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,57 @@ | ||
package types | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// A SendRestrictionFn can restrict sends and/or provide a new receiver address. | ||
type SendRestrictionFn func(ctx context.Context, fromAddr, toAddr []byte, amt sdk.Coins) (newToAddr []byte, err error) | ||
|
||
// IsOnePerModuleType implements the depinject.OnePerModuleType interface. | ||
func (SendRestrictionFn) IsOnePerModuleType() {} | ||
|
||
var _ SendRestrictionFn = NoOpSendRestrictionFn | ||
|
||
// NoOpSendRestrictionFn is a no-op SendRestrictionFn. | ||
func NoOpSendRestrictionFn(_ context.Context, _, toAddr []byte, _ sdk.Coins) ([]byte, error) { | ||
return toAddr, nil | ||
} | ||
|
||
// Then creates a composite restriction that runs this one then the provided second one. | ||
func (r SendRestrictionFn) Then(second SendRestrictionFn) SendRestrictionFn { | ||
return ComposeSendRestrictions(r, second) | ||
} | ||
|
||
// ComposeSendRestrictions combines multiple SendRestrictionFn into one. | ||
// nil entries are ignored. | ||
// If all entries are nil, nil is returned. | ||
// If exactly one entry is not nil, it is returned. | ||
// Otherwise, a new SendRestrictionFn is returned that runs the non-nil restrictions in the order they are given. | ||
// The composition runs each send restriction until an error is encountered and returns that error, | ||
// otherwise it returns the toAddr of the last send restriction. | ||
func ComposeSendRestrictions(restrictions ...SendRestrictionFn) SendRestrictionFn { | ||
toRun := make([]SendRestrictionFn, 0, len(restrictions)) | ||
for _, r := range restrictions { | ||
if r != nil { | ||
toRun = append(toRun, r) | ||
} | ||
} | ||
switch len(toRun) { | ||
case 0: | ||
return nil | ||
case 1: | ||
return toRun[0] | ||
} | ||
return func(ctx context.Context, fromAddr, toAddr []byte, amt sdk.Coins) ([]byte, error) { | ||
var err error | ||
for _, r := range toRun { | ||
toAddr, err = r(ctx, fromAddr, toAddr, amt) | ||
if err != nil { | ||
return toAddr, err | ||
} | ||
} | ||
return toAddr, err | ||
} | ||
} |