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-09-19 19:13:12 -07:00
|
|
|
bondedTokens := k.stakeKeeper.TotalPower(ctx)
|
2018-09-11 10:35:47 -07:00
|
|
|
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-19 19:13:12 -07:00
|
|
|
sumPowerPrecommitValidators).Quo(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-27 20:04:37 -07:00
|
|
|
commission := proposerReward.Mul(proposerValidator.GetCommission())
|
|
|
|
remaining := proposerReward.Mul(sdk.OneDec().Sub(proposerValidator.GetCommission()))
|
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-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-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-09-19 19:13:12 -07:00
|
|
|
poolReceived := feesCollectedDec.Mul(sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax))
|
|
|
|
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
|
|
|
}
|