-
Notifications
You must be signed in to change notification settings - Fork 19
/
contribution_reward_calculator.go
126 lines (112 loc) · 4.52 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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
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
}
score := rewardFactor.Mul(pcs.Score)
ps = append(ps, &types.PartyContributionScore{Party: pcs.Party, Score: score})
totalScores = totalScores.Add(score)
}
if totalScores.IsZero() {
return []*types.PartyContributionScore{}
}
for _, pcs := range ps {
pcs.Score = pcs.Score.Mul(teamScore.Score).Div(totalScores)
}
return ps
}