2018-09-04 23:41:17 -07:00
|
|
|
package keeper
|
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
import (
|
2018-09-28 00:29:52 -07:00
|
|
|
"fmt"
|
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-09-19 19:13:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2018-09-11 10:35:47 -07:00
|
|
|
)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-05 16:28:18 -07:00
|
|
|
// Allocate fees handles distribution of the collected fees
|
2018-09-13 23:35:02 -07:00
|
|
|
func (k Keeper) AllocateFees(ctx sdk.Context) {
|
2018-09-28 01:02:07 -07:00
|
|
|
ctx.Logger().With("module", "x/distribution").Error(fmt.Sprintf("allocation height: %v", ctx.BlockHeight()))
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// get the proposer of this block
|
2018-09-13 23:35:02 -07:00
|
|
|
proposerConsAddr := k.GetProposerConsAddr(ctx)
|
2018-09-27 20:04:37 -07:00
|
|
|
proposerValidator := k.stakeKeeper.ValidatorByConsAddr(ctx, proposerConsAddr)
|
|
|
|
proposerDist := k.GetValidatorDistInfo(ctx, proposerValidator.GetOperator())
|
2018-09-05 15:29:20 -07:00
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// get the fees which have been getting collected through all the
|
|
|
|
// transactions in the block
|
2018-09-19 19:13:12 -07:00
|
|
|
feesCollected := k.feeCollectionKeeper.GetCollectedFees(ctx)
|
|
|
|
feesCollectedDec := types.NewDecCoins(feesCollected)
|
2018-09-11 10:35:47 -07:00
|
|
|
|
|
|
|
// allocated rewards to proposer
|
2018-10-14 23:34:01 -07:00
|
|
|
percentVotes := k.GetPercentPrecommitVotes(ctx)
|
2018-10-15 13:12:42 -07:00
|
|
|
baseProposerReward := k.GetBaseProposerReward(ctx)
|
|
|
|
bonusProposerReward := k.GetBonusProposerReward(ctx)
|
|
|
|
proposerMultiplier := baseProposerReward.Add(bonusProposerReward.Mul(percentVotes))
|
2018-10-04 00:00:24 -07:00
|
|
|
proposerReward := feesCollectedDec.MulDec(proposerMultiplier)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// apply commission
|
2018-10-04 00:00:24 -07:00
|
|
|
commission := proposerReward.MulDec(proposerValidator.GetCommission())
|
2018-10-14 23:34:01 -07:00
|
|
|
remaining := proposerReward.Minus(commission)
|
2018-09-19 19:54:46 -07:00
|
|
|
proposerDist.PoolCommission = proposerDist.PoolCommission.Plus(commission)
|
|
|
|
proposerDist.Pool = proposerDist.Pool.Plus(remaining)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// allocate community funding
|
2018-09-18 21:42:05 -07:00
|
|
|
communityTax := k.GetCommunityTax(ctx)
|
2018-10-04 00:00:24 -07:00
|
|
|
communityFunding := feesCollectedDec.MulDec(communityTax)
|
2018-09-11 10:35:47 -07:00
|
|
|
feePool := k.GetFeePool(ctx)
|
2018-09-19 19:54:46 -07:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Plus(communityFunding)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-11 10:35:47 -07:00
|
|
|
// set the global pool within the distribution module
|
2018-10-12 04:15:13 -07:00
|
|
|
poolReceived := feesCollectedDec.Minus(proposerReward).Minus(communityFunding)
|
2018-09-19 19:13:12 -07:00
|
|
|
feePool.Pool = feePool.Pool.Plus(poolReceived)
|
2018-09-04 23:41:17 -07:00
|
|
|
|
2018-09-19 19:13:12 -07:00
|
|
|
k.SetValidatorDistInfo(ctx, proposerDist)
|
|
|
|
k.SetFeePool(ctx, feePool)
|
2018-09-11 10:35:47 -07:00
|
|
|
|
|
|
|
// clear the now distributed fees
|
2018-09-19 19:13:12 -07:00
|
|
|
k.feeCollectionKeeper.ClearCollectedFees(ctx)
|
2018-09-04 23:41:17 -07:00
|
|
|
}
|