108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
package keeper
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"cosmossdk.io/math"
|
|
core "clawchain/types"
|
|
"clawchain/x/oracle/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
// RewardBallotWinners implements
|
|
// at the end of every VotePeriod, give out a portion of spread fees collected in the oracle reward pool
|
|
//
|
|
// to the oracle voters that voted faithfully.
|
|
func (k Keeper) RewardBallotWinners(
|
|
ctx sdk.Context,
|
|
votePeriod int64,
|
|
rewardDistributionWindow int64,
|
|
voteTargets map[string]math.LegacyDec,
|
|
ballotWinners map[string]types.Claim,
|
|
) {
|
|
// Add Luna explicitly for oracle account balance coming from the market swap fee
|
|
rewardDenoms := make([]string, len(voteTargets)+1)
|
|
rewardDenoms[0] = core.MicroLunaDenom
|
|
|
|
i := 1
|
|
for denom := range voteTargets {
|
|
rewardDenoms[i] = denom
|
|
i++
|
|
}
|
|
|
|
// Sum weight of the claims
|
|
ballotPowerSum := int64(0)
|
|
for _, winner := range ballotWinners {
|
|
ballotPowerSum += winner.Weight
|
|
}
|
|
|
|
// Exit if the ballot is empty
|
|
if ballotPowerSum == 0 {
|
|
return
|
|
}
|
|
|
|
// The Reward distributionRatio = votePeriod/rewardDistributionWindow
|
|
distributionRatio := math.LegacyNewDec(votePeriod).QuoInt64(rewardDistributionWindow)
|
|
|
|
var periodRewards sdk.DecCoins
|
|
for _, denom := range rewardDenoms {
|
|
rewardPool := k.GetRewardPool(ctx, denom)
|
|
|
|
// return if there's no rewards to give out
|
|
if rewardPool.IsZero() {
|
|
continue
|
|
}
|
|
|
|
periodRewards = periodRewards.Add(sdk.NewDecCoinFromDec(
|
|
denom,
|
|
math.LegacyNewDecFromInt(rewardPool.Amount).Mul(distributionRatio),
|
|
))
|
|
}
|
|
|
|
logger := k.Logger(ctx)
|
|
logger.Debug("RewardBallotWinner", "periodRewards", periodRewards)
|
|
|
|
// Dole out rewards
|
|
var distributedReward sdk.Coins
|
|
for _, winner := range ballotWinners {
|
|
receiverVal, err := k.StakingKeeper.Validator(ctx, winner.Recipient)
|
|
if err != nil || receiverVal == nil {
|
|
continue
|
|
}
|
|
|
|
// Reflects contribution
|
|
rewardCoins, _ := periodRewards.MulDec(math.LegacyNewDec(winner.Weight).QuoInt64(ballotPowerSum)).TruncateDecimal()
|
|
|
|
// In case absence of the validator, we just skip distribution
|
|
if !rewardCoins.IsZero() {
|
|
k.distrKeeper.AllocateTokensToValidator(ctx, receiverVal, sdk.NewDecCoinsFromCoins(rewardCoins...))
|
|
distributedReward = distributedReward.Add(rewardCoins...)
|
|
} else {
|
|
valAddr, err := sdk.ValAddressFromBech32(receiverVal.GetOperator())
|
|
if err != nil {
|
|
logger.Error("failed to parse validator address", "error", err)
|
|
continue
|
|
}
|
|
logger.Debug(fmt.Sprintf("no reward %s(%s)",
|
|
receiverVal.GetMoniker(),
|
|
receiverVal.GetOperator()),
|
|
"miss", k.GetMissCounter(ctx, valAddr),
|
|
"wincount", winner.WinCount)
|
|
}
|
|
}
|
|
|
|
// Move distributed reward to distribution module
|
|
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.distrName, distributedReward); err != nil {
|
|
logger.Error("failed to send oracle rewards to distribution module", "error", err, "amount", distributedReward)
|
|
return
|
|
}
|
|
|
|
// Prometheus: track distributed rewards and ballot power
|
|
OracleBallotPowerGauge.Set(float64(ballotPowerSum))
|
|
for _, coin := range distributedReward {
|
|
if coin.Denom == core.MicroLunaDenom {
|
|
OracleRewardDistributedGauge.Set(float64(coin.Amount.Int64()))
|
|
}
|
|
}
|
|
}
|