2018-05-25 20:29:40 -07:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2018-09-13 11:17:32 -07:00
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
2018-05-25 20:29:40 -07:00
|
|
|
// This FeeCollectionKeeper handles collection of fees in the anteHandler
|
|
|
|
// 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
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
// The codec codec for binary encoding/decoding of accounts.
|
|
|
|
cdc *codec.Codec
|
2018-05-25 20:29:40 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
func NewFeeCollectionKeeper(cdc *codec.Codec, key sdk.StoreKey) FeeCollectionKeeper {
|
2018-05-25 20:29:40 -07:00
|
|
|
return FeeCollectionKeeper{
|
|
|
|
key: key,
|
|
|
|
cdc: cdc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// 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 {
|
|
|
|
return sdk.Coins{}
|
|
|
|
}
|
|
|
|
|
|
|
|
feePool := &(sdk.Coins{})
|
2018-11-04 18:28:38 -08:00
|
|
|
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) {
|
2018-11-04 18:28:38 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
// add to the fee pool
|
|
|
|
func (fck FeeCollectionKeeper) AddCollectedFees(ctx sdk.Context, coins sdk.Coins) sdk.Coins {
|
2018-05-25 20:29:40 -07:00
|
|
|
newCoins := fck.GetCollectedFees(ctx).Plus(coins)
|
|
|
|
fck.setCollectedFees(ctx, newCoins)
|
|
|
|
|
|
|
|
return newCoins
|
|
|
|
}
|
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// clear the fee pool
|
2018-05-25 20:29:40 -07:00
|
|
|
func (fck FeeCollectionKeeper) ClearCollectedFees(ctx sdk.Context) {
|
|
|
|
fck.setCollectedFees(ctx, sdk.Coins{})
|
|
|
|
}
|