forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tips.go
51 lines (42 loc) · 1.43 KB
/
tips.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package posthandler
import (
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
}
return d.bankKeeper.SendCoins(ctx, tipper, tipTx.FeePayer(), tipTx.GetTip().Amount)
}