2018-04-26 19:59:30 -07:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2018-04-27 12:20:12 -07:00
|
|
|
// Handle fee distribution to the validators and delegators
|
2018-04-30 23:07:06 -07:00
|
|
|
func (k Keeper) FeeHandler(ctx sdk.Context, collectedFees sdk.Coins) {
|
|
|
|
pool := k.GetPool(ctx)
|
|
|
|
params := k.GetParams(ctx)
|
2018-04-26 19:59:30 -07:00
|
|
|
|
2018-05-03 21:02:45 -07:00
|
|
|
// XXX determine
|
2018-04-30 23:07:06 -07:00
|
|
|
candidate := NewCandidate(addrs[0], pks[0], Description{})
|
|
|
|
|
2018-05-03 21:02:45 -07:00
|
|
|
// calculate the proposer reward
|
|
|
|
precommitPower := k.GetTotalPrecommitVotingPower(ctx)
|
|
|
|
toProposer := coinsMulRat(collectedFees, (sdk.NewRat(1, 100).Add(sdk.NewRat(4, 100).Mul(precommitPower).Quo(pool.BondedShares))))
|
2018-04-30 23:07:06 -07:00
|
|
|
candidate.ProposerRewardPool = candidate.ProposerRewardPool.Plus(toProposer)
|
|
|
|
|
|
|
|
toReservePool := coinsMulRat(collectedFees, params.ReservePoolFee)
|
2018-05-04 12:38:25 -07:00
|
|
|
pool.FeeReservePool = pool.FeeReservePool.Plus(toReservePool)
|
2018-04-30 23:07:06 -07:00
|
|
|
|
|
|
|
distributedReward := (collectedFees.Minus(toProposer)).Minus(toReservePool)
|
|
|
|
pool.FeePool = pool.FeePool.Plus(distributedReward)
|
2018-05-04 12:38:25 -07:00
|
|
|
pool.FeeSumReceived = pool.FeeSumReceived.Plus(distributedReward)
|
|
|
|
pool.FeeRecent = distributedReward
|
|
|
|
|
|
|
|
// lastly update the FeeRecent term
|
|
|
|
pool.FeeRecent = collectedFees
|
2018-04-30 23:07:06 -07:00
|
|
|
|
|
|
|
k.setPool(ctx, pool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func coinsMulRat(coins sdk.Coins, rat sdk.Rat) sdk.Coins {
|
|
|
|
var res sdk.Coins
|
|
|
|
for _, coin := range coins {
|
|
|
|
coinMulAmt := rat.Mul(sdk.NewRat(coin.Amount)).Evaluate()
|
|
|
|
coinMul := sdk.Coins{{coin.Denom, coinMulAmt}}
|
|
|
|
res = res.Plus(coinMul)
|
|
|
|
}
|
|
|
|
return res
|
2018-04-26 19:59:30 -07:00
|
|
|
}
|
2018-05-03 23:00:30 -07:00
|
|
|
|
|
|
|
//____________________________________________________________________________-
|
|
|
|
|
|
|
|
// calculate adjustment changes for a candidate at a height
|
2018-05-04 12:38:25 -07:00
|
|
|
func CalculateAdjustmentChange(candidate Candidate, pool Pool, denoms []string, height int64) (Candidate, Pool) {
|
2018-05-03 23:00:30 -07:00
|
|
|
|
|
|
|
heightRat := sdk.NewRat(height)
|
|
|
|
lastHeightRat := sdk.NewRat(height - 1)
|
2018-05-04 12:38:25 -07:00
|
|
|
candidateFeeCount := candidate.BondedShares.Mul(heightRat)
|
2018-05-03 23:00:30 -07:00
|
|
|
poolFeeCount := pool.BondedShares.Mul(heightRat)
|
|
|
|
|
2018-05-04 12:38:25 -07:00
|
|
|
for i, denom := range denoms {
|
|
|
|
poolFeeSumReceived := sdk.NewRat(pool.FeeSumReceived.AmountOf(denom))
|
|
|
|
poolFeeRecent := sdk.NewRat(pool.FeeRecent.AmountOf(denom))
|
|
|
|
// calculate simple and projected pools
|
|
|
|
simplePool := candidateFeeCount.Quo(poolFeeCount).Mul(poolFeeSumReceived)
|
|
|
|
calc1 := candidate.PrevBondedShares.Mul(lastHeightRat).Quo(pool.PrevBondedShares.Mul(lastHeightRat)).Mul(poolFeeRecent)
|
|
|
|
calc2 := candidate.BondedShares.Quo(pool.BondedShares).Mul(poolFeeRecent)
|
|
|
|
projectedPool := calc1.Add(calc2)
|
|
|
|
|
|
|
|
AdjustmentChange := simplePool.Sub(projectedPool)
|
|
|
|
candidate.FeeAdjustments[i] = candidate.FeeAdjustments[i].Add(AdjustmentChange)
|
|
|
|
pool.FeeAdjustments[i] = pool.FeeAdjustments[i].Add(AdjustmentChange)
|
|
|
|
}
|
2018-05-03 23:00:30 -07:00
|
|
|
|
|
|
|
return candidate, pool
|
|
|
|
}
|