## Description Closes: #XXXX --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
package posthandler
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/tx"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
)
|
|
|
|
// ValidateBasicDecorator will call tx.ValidateBasic and return any non-nil error.
|
|
// If ValidateBasic passes, decorator calls next AnteHandler in chain. Note,
|
|
// ValidateBasicDecorator decorator will not get executed on ReCheckTx since it
|
|
// is not dependent on application state.
|
|
type tipDecorator struct {
|
|
bankKeeper types.BankKeeper
|
|
}
|
|
|
|
// NewTipDecorator returns a new decorator for handling transactions with
|
|
// tips.
|
|
//
|
|
// IMPORTANT: This decorator is still in beta, please use it at your own risk.
|
|
func NewTipDecorator(bankKeeper types.BankKeeper) sdk.AnteDecorator {
|
|
return tipDecorator{
|
|
bankKeeper: bankKeeper,
|
|
}
|
|
}
|
|
|
|
func (d tipDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
|
err := d.transferTip(ctx, tx)
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
|
|
return next(ctx, tx, simulate)
|
|
}
|
|
|
|
// transferTip transfers the tip from the tipper to the fee payer.
|
|
func (d tipDecorator) transferTip(ctx sdk.Context, sdkTx sdk.Tx) error {
|
|
tipTx, ok := sdkTx.(tx.TipTx)
|
|
|
|
// No-op if the tx doesn't have tips.
|
|
if !ok || tipTx.GetTip() == nil {
|
|
return nil
|
|
}
|
|
|
|
tipper, err := sdk.AccAddressFromBech32(tipTx.GetTip().Tipper)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
coins := tipTx.GetTip().Amount
|
|
if err := d.bankKeeper.IsSendEnabledCoins(ctx, coins...); err != nil {
|
|
return fmt.Errorf("cannot tip these coins: %w", err)
|
|
}
|
|
|
|
return d.bankKeeper.SendCoins(ctx, tipper, tipTx.FeePayer(), coins)
|
|
}
|