/
reward.go
91 lines (75 loc) · 2.62 KB
/
reward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
core "github.com/classic-terra/core/v2/types"
"github.com/classic-terra/core/v2/x/oracle/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]sdk.Dec,
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 := sdk.NewDec(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,
sdk.NewDecFromInt(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 := k.StakingKeeper.Validator(ctx, winner.Recipient)
// Reflects contribution
rewardCoins, _ := periodRewards.MulDec(sdk.NewDec(winner.Weight).QuoInt64(ballotPowerSum)).TruncateDecimal()
// In case absence of the validator, we just skip distribution
if receiverVal != nil && !rewardCoins.IsZero() {
k.distrKeeper.AllocateTokensToValidator(ctx, receiverVal, sdk.NewDecCoinsFromCoins(rewardCoins...))
distributedReward = distributedReward.Add(rewardCoins...)
} else {
logger.Debug(fmt.Sprintf("no reward %s(%s)",
receiverVal.GetMoniker(),
receiverVal.GetOperator().String()),
"miss", k.GetMissCounter(ctx, receiverVal.GetOperator()),
"wincount", winner.WinCount)
}
}
// Move distributed reward to distribution module
err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.distrName, distributedReward)
if err != nil {
panic(fmt.Sprintf("[oracle] Failed to send coins to distribution module %s", err.Error()))
}
}