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

48 lines
1.7 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-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-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)
proserValidator := k.stakeKeeper.GetValidatorFromConsAddr(ctx, proposerConsAddr)
2018-09-05 16:28:18 -07:00
proposerDist := k.GetFeeDistribution(ctx, proserValidator.OperatorAddr)
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
feesCollected := k.FeeCollectionKeeper.GetCollectedFees(ctx)
2018-09-05 16:15:15 -07:00
feesCollectedDec := NewDecCoins(feesCollected)
2018-09-11 10:35:47 -07:00
// allocated rewards to proposer
stakePool := k.stakeKeeper.GetPool(ctx)
sumPowerPrecommitValidators := sdk.NewDec(1) // XXX TODO actually calculate this
2018-09-05 16:15:15 -07:00
proposerMultiplier := sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(4, 2).Mul(
2018-09-05 16:28:18 -07:00
sumPowerPrecommitValidators).Div(stakePool.BondedTokens))
2018-09-05 16:15:15 -07:00
proposerReward := feesCollectedDec.Mul(proposerMultiplier)
2018-09-04 23:41:17 -07:00
2018-09-11 10:35:47 -07:00
// apply commission
2018-09-05 16:28:18 -07:00
commission := proposerReward.Mul(proserValidator.Commission)
proposerDist.PoolCommission = proposerDist.PoolCommission.Add(commission)
proposerDist.Pool = proposerDist.Pool.Add(proposerReward.Sub(commission))
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-09-05 16:15:15 -07:00
communityFunding := feesCollectedDec.Mul(communityTax)
2018-09-11 10:35:47 -07:00
feePool := k.GetFeePool(ctx)
2018-09-05 16:15:15 -07:00
feePool.CommunityFund = feePool.CommunityFund.Add(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-09-05 16:28:18 -07:00
poolReceived := feesCollectedDec.Sub(proposerReward).Sub(communityFunding)
2018-09-05 16:15:15 -07:00
feePool.Pool = feePool.Pool.Add(poolReceived)
2018-09-04 23:41:17 -07:00
2018-09-05 16:28:18 -07:00
SetValidatorDistribution(proposerDist)
2018-09-04 23:41:17 -07:00
SetFeePool(feePool)
2018-09-11 10:35:47 -07:00
// clear the now distributed fees
k.FeeCollectionKeeper.ClearCollectedFees(ctx)
2018-09-04 23:41:17 -07:00
}