-
Notifications
You must be signed in to change notification settings - Fork 22
/
contribution_reward_calculator.go
128 lines (114 loc) · 4.69 KB
/
contribution_reward_calculator.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package rewards
import (
"time"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/protos/vega"
)
// given party contribution scores, reward multipliers and distribution strategy calculate the payout per party.
func calculateRewardsByContributionIndividual(epochSeq, asset, accountID string, balance *num.Uint, partyContribution []*types.PartyContributionScore, rewardFactors map[string]num.Decimal, timestamp time.Time, ds *vega.DispatchStrategy) *payout {
po := &payout{
asset: asset,
fromAccount: accountID,
epochSeq: epochSeq,
timestamp: timestamp.Unix(),
partyToAmount: map[string]*num.Uint{},
lockedForEpochs: ds.LockPeriod,
}
total := num.UintZero()
rewardBalance := balance.ToDecimal()
var partyScores []*types.PartyContributionScore
if ds.DistributionStrategy == vega.DistributionStrategy_DISTRIBUTION_STRATEGY_PRO_RATA {
partyScores = proRataRewardCalculator(partyContribution, rewardFactors)
} else if ds.DistributionStrategy == vega.DistributionStrategy_DISTRIBUTION_STRATEGY_RANK {
partyScores = rankingRewardCalculator(partyContribution, ds.RankTable, rewardFactors)
}
for _, p := range partyScores {
partyReward, _ := num.UintFromDecimal(rewardBalance.Mul(p.Score))
if !partyReward.IsZero() {
po.partyToAmount[p.Party] = partyReward
total.AddSum(partyReward)
}
}
po.totalReward = total
if total.IsZero() {
return nil
}
return po
}
// given party contribution scores, reward multipliers and distribution strategy calculate the payout per party in a team.
func calculateRewardsByContributionTeam(epochSeq, asset, accountID string, balance *num.Uint, teamContribution []*types.PartyContributionScore, teamPartyContribution map[string][]*types.PartyContributionScore, rewardFactors map[string]num.Decimal, timestamp time.Time, ds *vega.DispatchStrategy) *payout {
po := &payout{
asset: asset,
fromAccount: accountID,
epochSeq: epochSeq,
timestamp: timestamp.Unix(),
partyToAmount: map[string]*num.Uint{},
lockedForEpochs: ds.LockPeriod,
}
total := num.UintZero()
rewardBalance := balance.ToDecimal()
var teamScores []*types.PartyContributionScore
if ds.DistributionStrategy == vega.DistributionStrategy_DISTRIBUTION_STRATEGY_PRO_RATA {
teamScores = proRataRewardCalculator(teamContribution, map[string]num.Decimal{})
} else if ds.DistributionStrategy == vega.DistributionStrategy_DISTRIBUTION_STRATEGY_RANK {
teamScores = rankingRewardCalculator(teamContribution, ds.RankTable, map[string]num.Decimal{})
}
partyScores := []*types.PartyContributionScore{}
totalScore := num.DecimalZero()
for _, teamScore := range teamScores {
partyScores = append(partyScores, calcPartyInTeamRewardShare(teamScore, teamPartyContribution[teamScore.Party], rewardFactors)...)
}
for _, pcs := range partyScores {
totalScore = totalScore.Add(pcs.Score)
}
capAtOne(partyScores, totalScore)
for _, p := range partyScores {
partyReward, _ := num.UintFromDecimal(rewardBalance.Mul(p.Score))
if !partyReward.IsZero() {
po.partyToAmount[p.Party] = partyReward
total.AddSum(partyReward)
}
}
po.totalReward = total
if total.IsZero() {
return nil
}
return po
}
func calcPartyInTeamRewardShare(teamScore *types.PartyContributionScore, partyToMetricScore []*types.PartyContributionScore, rewardFactors map[string]num.Decimal) []*types.PartyContributionScore {
ps := make([]*types.PartyContributionScore, 0, len(partyToMetricScore))
totalScores := num.DecimalZero()
for _, pcs := range partyToMetricScore {
if pcs.Score.IsZero() {
continue
}
rewardFactor := num.DecimalOne()
if factor, ok := rewardFactors[pcs.Party]; ok {
rewardFactor = factor
}
ps = append(ps, &types.PartyContributionScore{Party: pcs.Party, Score: rewardFactor})
totalScores = totalScores.Add(rewardFactor)
}
if totalScores.IsZero() {
return []*types.PartyContributionScore{}
}
for _, pcs := range ps {
pcs.Score = pcs.Score.Mul(teamScore.Score).Div(totalScores)
}
return ps
}