-
Notifications
You must be signed in to change notification settings - Fork 0
/
reward.go
76 lines (61 loc) · 2.03 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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elysium-station/blackfury/x/oracle/types"
)
// RewardBallotWinners gives out portion of seigniorage reward to the
// oracle voters that voted faithfully, at the end of every VotePeriod.
func (k Keeper) RewardBallotWinners(
ctx sdk.Context,
votePeriod int64,
rewardDistributionWindow int64,
voteTargets map[string]struct{},
ballotWinners map[string]types.Claim,
) {
rewardDenoms := make([]string, len(voteTargets))
i := 0
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
}
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),
))
}
// 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...)
}
}
// Move distributed reward to distribution module
err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.distrName, distributedReward)
if err != nil {
panic(fmt.Sprintf("failed to send coins to distribution module %s", err.Error()))
}
}