-
Notifications
You must be signed in to change notification settings - Fork 291
/
reward.go
61 lines (48 loc) · 1.86 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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
core "github.com/terra-project/core/types"
"github.com/terra-project/core/x/oracle/internal/types"
)
// RewardBallotWinners implements
// at the end of every VotePeriod, we give out portion of seigniorage reward(reward-weight) to the
// oracle voters that voted faithfully.
func (k Keeper) RewardBallotWinners(ctx sdk.Context, ballotWinners map[string]types.Claim) {
// 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
}
rewardPool := k.getRewardPool(ctx)
// return if there's no rewards to give out
if rewardPool.Empty() {
return
}
// rewardCoin = oraclePool * VotePeriod / RewardDistributionWindow
periodRewards := sdk.NewDecFromInt(rewardPool.AmountOf(core.MicroLunaDenom)).
MulInt64(k.VotePeriod(ctx)).QuoInt64(k.RewardDistributionWindow(ctx))
// Dole out rewards
var distributedReward sdk.Coins
for _, winner := range ballotWinners {
rewardCoins := sdk.NewCoins()
rewardeeVal := k.StakingKeeper.Validator(ctx, winner.Recipient)
// Reflects contribution
rewardAmt := periodRewards.QuoInt64(ballotPowerSum).MulInt64(winner.Weight).TruncateInt()
rewardCoins = append(rewardCoins, sdk.NewCoin(core.MicroLunaDenom, rewardAmt))
// In case absence of the validator, we just skip distribution
if rewardeeVal != nil {
k.distrKeeper.AllocateTokensToValidator(ctx, rewardeeVal, sdk.NewDecCoins(rewardCoins))
distributedReward = distributedReward.Add(rewardCoins)
}
}
// Move distributed reward to distribution module
err := k.supplyKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, k.distrName, distributedReward)
if err != nil {
panic(fmt.Sprintf("[oracle] Failed to send coins to distribution module %s", err.Error()))
}
}