Skip to content

Commit

Permalink
Merge pull request #9687 from vegaprotocol/add-and-cache-referral-stats
Browse files Browse the repository at this point in the history
feat: cache and forward referral rewards multiplier and factor multplier
  • Loading branch information
jeremyletang authored Oct 9, 2023
2 parents b2c6c2d + 4ebe331 commit 30c4701
Show file tree
Hide file tree
Showing 19 changed files with 2,146 additions and 1,870 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [8857](https://github.com/vegaprotocol/vega/issues/8857) - Add a step for getting the balance of the liquidity provider liquidity fee account
- [8847](https://github.com/vegaprotocol/vega/issues/8847) - Implement internal time trigger data source.
- [8895](https://github.com/vegaprotocol/vega/issues/8895) - Allow to set runtime parameters in the SQL Store connection structure
- [9678](https://github.com/vegaprotocol/vega/issues/9678) - Cache and forward referral rewards multiplier and factor multiplier
- [8779](https://github.com/vegaprotocol/vega/issues/8779) - Query all details of liquidity providers via an API.
- [8924](https://github.com/vegaprotocol/vega/issues/8924) - Refactor slightly to remove need to deep clone `proto` types
- [8782](https://github.com/vegaprotocol/vega/issues/8782) - List all active liquidity providers for a market via API.
Expand Down
7 changes: 7 additions & 0 deletions core/events/referral_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ func (t ReferralSetStatsUpdated) Unwrap() *types.ReferralSetStats {
volume, _ := num.UintFromString(t.e.ReferralSetRunningNotionalTakerVolume, 10)
stats := map[types.PartyID]*types.RefereeStats{}
rewardFactor, _ := num.DecimalFromString(t.e.RewardFactor)
rewardsMultiplier, _ := num.DecimalFromString(t.e.RewardsMultiplier)
rewardsFactorMultiplier, _ := num.DecimalFromString(t.e.RewardsFactorMultiplier)

for _, stat := range t.e.RefereesStats {
discountFactor, _ := num.DecimalFromString(stat.DiscountFactor)

stats[types.PartyID(stat.PartyId)] = &types.RefereeStats{
DiscountFactor: discountFactor,
}
Expand All @@ -69,6 +72,8 @@ func (t ReferralSetStatsUpdated) Unwrap() *types.ReferralSetStats {
ReferralSetRunningVolume: volume,
RefereesStats: stats,
RewardFactor: rewardFactor,
RewardsMultiplier: rewardsMultiplier,
RewardsFactorMultiplier: rewardsFactorMultiplier,
}
}

Expand Down Expand Up @@ -107,6 +112,8 @@ func NewReferralSetStatsUpdatedEvent(ctx context.Context, update *types.Referral
ReferralSetRunningNotionalTakerVolume: update.ReferralSetRunningVolume.String(),
RefereesStats: refereesStats,
RewardFactor: update.RewardFactor.String(),
RewardsMultiplier: update.RewardsMultiplier.String(),
RewardsFactorMultiplier: update.RewardsFactorMultiplier.String(),
},
}
}
Expand Down
81 changes: 48 additions & 33 deletions core/referral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func (e *Engine) CreateReferralSet(ctx context.Context, party types.PartyID, det
JoinedAt: now,
StartedAtEpoch: e.currentEpoch,
},
CurrentRewardFactor: num.DecimalZero(),
CurrentRewardFactor: num.DecimalZero(),
CurrentRewardsMultiplier: num.DecimalZero(),
CurrentRewardsFactorMultiplier: num.DecimalZero(),
}

e.sets[deterministicSetID] = &newSet
Expand Down Expand Up @@ -260,44 +262,21 @@ func (e *Engine) RewardsFactorForParty(party types.PartyID) num.Decimal {
}

func (e *Engine) RewardsFactorMultiplierAppliedForParty(party types.PartyID) num.Decimal {
return num.MinD(
e.RewardsFactorForParty(party).Mul(e.RewardsMultiplierForParty(party)),
e.rewardProportionUpdate,
)
}

func (e *Engine) RewardsMultiplierForParty(party types.PartyID) num.Decimal {
if e.programHasEnded {
setID, ok := e.referees[party]
if !ok {
return num.DecimalZero()
}

setID, isReferee := e.referees[party]
if !isReferee {
// This party is not eligible to referral program rewards.
return num.DecimalZero()
}
return e.sets[setID].CurrentRewardsFactorMultiplier
}

if e.isSetEligible(setID) != nil {
func (e *Engine) RewardsMultiplierForParty(party types.PartyID) num.Decimal {
setID, ok := e.referees[party]
if !ok {
return num.DecimalZero()
}

balance, _ := e.staking.GetAvailableBalance(
string(e.sets[setID].Referrer.PartyID),
)

multiplier := num.DecimalOne()
for _, v := range e.currentProgram.StakingTiers {
if balance.LTE(v.MinimumStakedTokens) {
break
}
multiplier = v.ReferralRewardMultiplier
}

return multiplier
}

func (e *Engine) VolumeDiscountFactorForParty(party types.PartyID) num.Decimal {
return num.DecimalZero()
return e.sets[setID].CurrentRewardsMultiplier
}

func (e *Engine) OnReferralProgramMaxReferralRewardProportionUpdate(_ context.Context, value num.Decimal) error {
Expand Down Expand Up @@ -331,6 +310,33 @@ func (e *Engine) OnEpochRestore(_ context.Context, ep types.Epoch) {
}
}

func (e *Engine) calculateRewardsFactorMultiplierForParty(multiplierForParty, rewardsFactorForParty num.Decimal) num.Decimal {
return num.MinD(
rewardsFactorForParty.Mul(multiplierForParty),
e.rewardProportionUpdate,
)
}

func (e *Engine) calculateMultiplierForParty(setID types.ReferralSetID) num.Decimal {
if e.isSetEligible(setID) != nil {
return num.DecimalZero()
}

balance, _ := e.staking.GetAvailableBalance(
string(e.sets[setID].Referrer.PartyID),
)

multiplier := num.DecimalOne()
for _, v := range e.currentProgram.StakingTiers {
if balance.LTE(v.MinimumStakedTokens) {
break
}
multiplier = v.ReferralRewardMultiplier
}

return multiplier
}

func (e *Engine) applyProgramUpdate(ctx context.Context, startEpochTime time.Time) {
if e.newProgram != nil {
if e.currentProgram != nil {
Expand Down Expand Up @@ -420,7 +426,9 @@ func (e *Engine) loadReferralSetsFromSnapshot(setsProto *snapshotpb.ReferralSets
JoinedAt: time.Unix(0, setProto.Referrer.JoinedAt),
StartedAtEpoch: setProto.Referrer.StartedAtEpoch,
},
CurrentRewardFactor: num.MustDecimalFromString(setProto.CurrentRewardFactor),
CurrentRewardFactor: num.MustDecimalFromString(setProto.CurrentRewardFactor),
CurrentRewardsMultiplier: num.MustDecimalFromString(setProto.CurrentRewardsMultiplier),
CurrentRewardsFactorMultiplier: num.MustDecimalFromString(setProto.CurrentRewardsFactorMultiplier),
}

e.referrers[types.PartyID(setProto.Referrer.PartyId)] = setID
Expand Down Expand Up @@ -510,7 +518,14 @@ func (e *Engine) computeFactorsByReferee(ctx context.Context, epoch uint64, take
}
}

rewardsMultiplier := e.calculateMultiplierForParty(setID)
set.RewardsMultiplier = rewardsMultiplier
set.RewardsFactorMultiplier = e.calculateRewardsFactorMultiplierForParty(rewardsMultiplier, set.RewardFactor)

e.sets[setID].CurrentRewardFactor = set.RewardFactor
e.sets[setID].CurrentRewardsMultiplier = rewardsMultiplier
e.sets[setID].CurrentRewardsFactorMultiplier = set.RewardsFactorMultiplier

allStats[setID] = set
}

Expand Down
4 changes: 3 additions & 1 deletion core/referral/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func (e *SnapshottedEngine) serialiseReferralSets() ([]byte, error) {
JoinedAt: set.Referrer.JoinedAt.UnixNano(),
StartedAtEpoch: set.Referrer.StartedAtEpoch,
},
CurrentRewardFactor: set.CurrentRewardFactor.String(),
CurrentRewardFactor: set.CurrentRewardFactor.String(),
CurrentRewardsMultiplier: set.CurrentRewardsMultiplier.String(),
CurrentRewardsFactorMultiplier: set.CurrentRewardsFactorMultiplier.String(),
}

for _, r := range set.Referees {
Expand Down
2 changes: 2 additions & 0 deletions core/referral/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) {
te.marketActivityTracker.EXPECT().NotionalTakerVolumeForParty(string(referee7)).Return(num.UintFromUint64(100)).Times(1)
te.marketActivityTracker.EXPECT().NotionalTakerVolumeForParty(string(referee8)).Return(num.UintFromUint64(100)).Times(1)
te.marketActivityTracker.EXPECT().NotionalTakerVolumeForParty(string(referee9)).Return(num.UintFromUint64(100)).Times(1)
te.staking.EXPECT().GetAvailableBalance(gomock.Any()).AnyTimes().Return(num.NewUint(100), nil)

gomock.InOrder(
expectReferralSetStatsUpdatedEvent(t, te, 4),
Expand Down Expand Up @@ -198,6 +199,7 @@ func TestTakingAndRestoringSnapshotSucceeds(t *testing.T) {
te2.marketActivityTracker.EXPECT().NotionalTakerVolumeForParty(string(referee7)).Return(num.UintFromUint64(100)).Times(1)
te2.marketActivityTracker.EXPECT().NotionalTakerVolumeForParty(string(referee8)).Return(num.UintFromUint64(100)).Times(1)
te2.marketActivityTracker.EXPECT().NotionalTakerVolumeForParty(string(referee9)).Return(num.UintFromUint64(100)).Times(1)
te2.staking.EXPECT().GetAvailableBalance(gomock.Any()).AnyTimes().Return(num.NewUint(100), nil)
expectReferralSetStatsUpdatedEvent(t, te2, 4)
// This triggers the state restoration from the local snapshot.
require.NoError(t, snapshotEngine2.Start(ctx))
Expand Down
6 changes: 5 additions & 1 deletion core/types/referral_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ type ReferralSet struct {
Referrer *Membership
Referees []*Membership

CurrentRewardFactor num.Decimal
CurrentRewardFactor num.Decimal
CurrentRewardsMultiplier num.Decimal
CurrentRewardsFactorMultiplier num.Decimal
}

type ReferralSetStats struct {
Expand All @@ -26,6 +28,8 @@ type ReferralSetStats struct {
ReferralSetRunningVolume *num.Uint
RefereesStats map[PartyID]*RefereeStats
RewardFactor num.Decimal
RewardsMultiplier num.Decimal
RewardsFactorMultiplier num.Decimal
}

type RefereeStats struct {
Expand Down
8 changes: 8 additions & 0 deletions datanode/entities/referral_set_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type (
RefereesStats []*eventspb.RefereeStats
VegaTime time.Time
RewardFactor string
RewardsMultiplier string
RewardsFactorMultiplier string
}

FlattenReferralSetStats struct {
Expand All @@ -26,6 +28,8 @@ type (
DiscountFactor string
EpochNotionalTakerVolume string
RewardFactor string
RewardsMultiplier string
RewardsFactorMultiplier string
}

ReferralSetStatsCursor struct {
Expand Down Expand Up @@ -70,6 +74,8 @@ func (s FlattenReferralSetStats) ToProto() *v2.ReferralSetStats {
DiscountFactor: s.DiscountFactor,
RewardFactor: s.RewardFactor,
EpochNotionalTakerVolume: s.EpochNotionalTakerVolume,
RewardsMultiplier: s.RewardsMultiplier,
RewardsFactorMultiplier: s.RewardsFactorMultiplier,
}
}

Expand All @@ -93,6 +99,8 @@ func ReferralSetStatsFromProto(proto *eventspb.ReferralSetStatsUpdated, vegaTime
RefereesStats: proto.RefereesStats,
VegaTime: vegaTime,
RewardFactor: proto.RewardFactor,
RewardsMultiplier: proto.RewardsMultiplier,
RewardsFactorMultiplier: proto.RewardsFactorMultiplier,
}, nil
}

Expand Down
Loading

0 comments on commit 30c4701

Please sign in to comment.