cosmos-sdk/x/distribution/keeper/allocation.go

49 lines
1.8 KiB
Go
Raw Normal View History

2018-09-04 23:41:17 -07:00
package keeper
2018-09-11 10:35:47 -07:00
import (
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-10-19 11:36:00 -07:00
func (k Keeper) AllocateTokens(ctx sdk.Context, percentVotes sdk.Dec, proposer sdk.ConsAddress) {
2018-09-04 23:41:17 -07:00
2018-09-11 10:35:47 -07:00
// get the proposer of this block
proposerValidator := k.stakeKeeper.ValidatorByConsAddr(ctx, proposer)
2018-09-27 20:04:37 -07:00
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-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)
proposerDist.ValCommission = proposerDist.ValCommission.Plus(commission)
proposerDist.DelPool = proposerDist.DelPool.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)
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
poolReceived := feesCollectedDec.Minus(proposerReward).Minus(communityFunding)
feePool.ValPool = feePool.ValPool.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
}