cosmos-sdk/modules/fee/handler.go

85 lines
2.6 KiB
Go
Raw Normal View History

package fee
2017-06-01 11:01:19 -07:00
import (
"github.com/tendermint/basecoin"
"github.com/tendermint/basecoin/errors"
2017-07-06 05:59:45 -07:00
"github.com/tendermint/basecoin/modules/coin"
"github.com/tendermint/basecoin/stack"
2017-07-06 05:23:38 -07:00
"github.com/tendermint/basecoin/state"
2017-06-01 11:01:19 -07:00
)
2017-07-06 02:30:03 -07:00
// NameFee - namespace for the fee module
const NameFee = "fee"
// Bank is a default location for the fees, but pass anything into
// the middleware constructor
var Bank = basecoin.Actor{App: NameFee, Address: []byte("bank")}
2017-07-12 10:06:55 -07:00
// SimpleFeeMiddleware - middleware for fee checking, constant amount
// It used modules.coin to move the money
type SimpleFeeMiddleware struct {
// the fee must be the same denomination and >= this amount
// if the amount is 0, then the fee tx wrapper is optional
MinFee coin.Coin
// all fees go here, which could be a dump (Bank) or something reachable
// by other app logic
2017-07-12 10:06:55 -07:00
Collector basecoin.Actor
stack.PassOption
2017-06-01 11:01:19 -07:00
}
2017-07-12 10:06:55 -07:00
var _ stack.Middleware = SimpleFeeMiddleware{}
// NewSimpleFeeMiddleware returns a fee handler with a fixed minimum fee.
//
// If minFee is 0, then the FeeTx is optional
2017-07-12 11:22:42 -07:00
func NewSimpleFeeMiddleware(minFee coin.Coin, collector basecoin.Actor) SimpleFeeMiddleware {
2017-07-12 10:06:55 -07:00
return SimpleFeeMiddleware{
2017-07-12 11:22:42 -07:00
MinFee: minFee,
Collector: collector,
2017-07-12 10:06:55 -07:00
}
2017-06-01 11:01:19 -07:00
}
2017-07-06 02:30:03 -07:00
// Name - return the namespace for the fee module
2017-07-12 10:06:55 -07:00
func (SimpleFeeMiddleware) Name() string {
return NameFee
}
2017-07-06 02:30:03 -07:00
// CheckTx - check the transaction
func (h SimpleFeeMiddleware) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
2017-07-12 10:06:55 -07:00
return h.doTx(ctx, store, tx, next.CheckTx)
2017-06-01 11:01:19 -07:00
}
2017-07-06 02:30:03 -07:00
// DeliverTx - send the fee handler transaction
func (h SimpleFeeMiddleware) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
2017-07-12 10:06:55 -07:00
return h.doTx(ctx, store, tx, next.DeliverTx)
}
func (h SimpleFeeMiddleware) doTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.CheckerFunc) (res basecoin.Result, err error) {
2017-07-12 10:06:55 -07:00
feeTx, ok := tx.Unwrap().(Fee)
2017-06-01 11:01:19 -07:00
if !ok {
2017-07-12 10:06:55 -07:00
// the fee wrapper is not required if there is no minimum
if h.MinFee.IsZero() {
return next(ctx, store, tx)
}
return res, errors.ErrInvalidFormat(TypeFees, tx)
}
// see if it is the proper denom and big enough
2017-07-12 10:06:55 -07:00
fee := feeTx.Fee
if fee.Denom != h.MinFee.Denom {
return res, ErrWrongFeeDenom(h.MinFee.Denom)
}
2017-07-12 10:06:55 -07:00
if !fee.IsGTE(h.MinFee) {
2017-07-03 05:50:33 -07:00
return res, ErrInsufficientFees()
}
2017-07-12 10:06:55 -07:00
// now, try to make a IPC call to coins...
send := coin.NewSendOneTx(feeTx.Payer, h.Collector, coin.Coins{fee})
_, err = next(ctx, store, send)
2017-06-01 11:01:19 -07:00
if err != nil {
return res, err
}
2017-07-12 10:06:55 -07:00
return next(ctx, store, feeTx.Tx)
2017-06-01 11:01:19 -07:00
}