cosmos-sdk/x/auth/feekeeper.go

63 lines
1.6 KiB
Go
Raw Normal View History

2018-05-25 20:29:40 -07:00
package auth
import (
codec "github.com/cosmos/cosmos-sdk/codec"
2018-05-25 20:29:40 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-05-25 21:14:49 -07:00
var (
collectedFeesKey = []byte("collectedFees")
)
// FeeCollectionKeeper handles collection of fees in the anteHandler
2018-05-25 20:29:40 -07:00
// and setting of MinFees for different fee tokens
type FeeCollectionKeeper struct {
// The (unexposed) key used to access the fee store from the Context.
key sdk.StoreKey
// The codec codec for binary encoding/decoding of accounts.
cdc *codec.Codec
2018-05-25 20:29:40 -07:00
}
// NewFeeCollectionKeeper returns a new FeeCollectionKeeper
func NewFeeCollectionKeeper(cdc *codec.Codec, key sdk.StoreKey) FeeCollectionKeeper {
2018-05-25 20:29:40 -07:00
return FeeCollectionKeeper{
key: key,
cdc: cdc,
}
}
// GetCollectedFees - retrieves the collected fee pool
2018-05-25 20:29:40 -07:00
func (fck FeeCollectionKeeper) GetCollectedFees(ctx sdk.Context) sdk.Coins {
store := ctx.KVStore(fck.key)
2018-05-25 21:14:49 -07:00
bz := store.Get(collectedFeesKey)
2018-05-25 20:29:40 -07:00
if bz == nil {
2019-03-07 16:55:08 -08:00
return sdk.NewCoins()
2018-05-25 20:29:40 -07:00
}
2019-03-07 16:55:08 -08:00
emptyFees := sdk.NewCoins()
feePool := &emptyFees
fck.cdc.MustUnmarshalBinaryLengthPrefixed(bz, feePool)
2018-05-25 20:29:40 -07:00
return *feePool
}
func (fck FeeCollectionKeeper) setCollectedFees(ctx sdk.Context, coins sdk.Coins) {
bz := fck.cdc.MustMarshalBinaryLengthPrefixed(coins)
2018-05-25 20:29:40 -07:00
store := ctx.KVStore(fck.key)
2018-05-25 21:14:49 -07:00
store.Set(collectedFeesKey, bz)
2018-05-25 20:29:40 -07:00
}
// AddCollectedFees - add to the fee pool
2018-10-19 11:36:00 -07:00
func (fck FeeCollectionKeeper) AddCollectedFees(ctx sdk.Context, coins sdk.Coins) sdk.Coins {
newCoins := fck.GetCollectedFees(ctx).Add(coins)
2018-05-25 20:29:40 -07:00
fck.setCollectedFees(ctx, newCoins)
return newCoins
}
// ClearCollectedFees - clear the fee pool
2018-05-25 20:29:40 -07:00
func (fck FeeCollectionKeeper) ClearCollectedFees(ctx sdk.Context) {
2019-03-07 16:55:08 -08:00
fck.setCollectedFees(ctx, sdk.NewCoins())
2018-05-25 20:29:40 -07:00
}