Skip to content

Commit

Permalink
feat: add referrer taker volume to the refferral set stats
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Letang <me@jeremyletang.com>
  • Loading branch information
jeremyletang committed Oct 27, 2023
1 parent 0e2664e commit a666f5b
Show file tree
Hide file tree
Showing 15 changed files with 1,962 additions and 1,840 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [9408](https://github.com/vegaprotocol/vega/issues/9408) - Enforce pagination range.
- [9757](https://github.com/vegaprotocol/vega/issues/9757) - Liquidity provisions `API` shows the pending `LP` instead of the current when an update is accepted by the network.


### 🗑️ Deprecation

- [](https://github.com/vegaprotocol/vega/issues/xxxx) -
Expand Down Expand Up @@ -176,6 +177,7 @@
- [9770](https://github.com/vegaprotocol/vega/issues/9770) - Fix `PnL` flickering bug on an already open position.
- [9827](https://github.com/vegaprotocol/vega/issues/9827) - Expose `was_eligible` in the referral set statistics.
- [9918](https://github.com/vegaprotocol/vega/issues/9918) - Expose vesting quantum balance.
- [9923](https://github.com/vegaprotocol/vega/issues/9923) - Add referrer volume to referral set stats.

### 🐛 Fixes

Expand Down
1 change: 1 addition & 0 deletions core/events/referral_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func NewReferralSetStatsUpdatedEvent(ctx context.Context, update *types.Referral
AtEpoch: update.AtEpoch,
WasEligible: update.WasEligible,
ReferralSetRunningNotionalTakerVolume: update.ReferralSetRunningVolume.String(),
ReferrerTakerVolume: update.ReferrerTakerVolume.String(),
RefereesStats: refereesStats,
RewardFactor: update.RewardFactor.String(),
RewardsMultiplier: update.RewardsMultiplier.String(),
Expand Down
16 changes: 14 additions & 2 deletions core/referral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,12 @@ func (e *Engine) computeReferralSetsStats(ctx context.Context, epoch types.Epoch
}
e.referralSetsNotionalVolumes.RemovePriorEpoch(priorEpoch)

referrersTakerVolume := map[types.PartyID]*num.Uint{}

for partyID, setID := range e.referrers {
volumeForEpoch := e.marketActivityTracker.NotionalTakerVolumeForParty(string(partyID))
e.referralSetsNotionalVolumes.Add(epoch.Seq, setID, volumeForEpoch)
referrersTakerVolume[partyID] = volumeForEpoch
}

partiesTakerVolume := map[types.PartyID]*num.Uint{}
Expand All @@ -496,20 +499,29 @@ func (e *Engine) computeReferralSetsStats(ctx context.Context, epoch types.Epoch
return
}

e.computeFactorsByReferee(ctx, epoch.Seq, partiesTakerVolume)
e.computeFactorsByReferee(ctx, epoch.Seq, partiesTakerVolume, referrersTakerVolume)
}

func (e *Engine) computeFactorsByReferee(ctx context.Context, epoch uint64, takerVolumePerParty map[types.PartyID]*num.Uint) {
func (e *Engine) computeFactorsByReferee(
ctx context.Context,
epoch uint64,
takerVolumePerParty, referrersTakesVolume map[types.PartyID]*num.Uint,
) {
e.factorsByReferee = map[types.PartyID]*types.RefereeStats{}

allStats := map[types.ReferralSetID]*types.ReferralSetStats{}
tiersLen := len(e.currentProgram.BenefitTiers)

for setID, set := range e.sets {
referrerTakerVolume := num.UintZero()
if takerVolume := referrersTakesVolume[set.Referrer.PartyID]; takerVolume != nil {
referrerTakerVolume = takerVolume
}
setStats := &types.ReferralSetStats{
AtEpoch: epoch,
SetID: setID,
RefereesStats: map[types.PartyID]*types.RefereeStats{},
ReferrerTakerVolume: referrerTakerVolume,
ReferralSetRunningVolume: e.referralSetsNotionalVolumes.RunningSetVolumeForWindow(setID, e.currentProgram.WindowLength),
RewardFactor: num.DecimalZero(),
}
Expand Down
1 change: 1 addition & 0 deletions core/types/referral_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ReferralSetStats struct {
SetID ReferralSetID
WasEligible bool
ReferralSetRunningVolume *num.Uint
ReferrerTakerVolume *num.Uint
RefereesStats map[PartyID]*RefereeStats
RewardFactor num.Decimal
RewardsMultiplier num.Decimal
Expand Down
4 changes: 4 additions & 0 deletions datanode/entities/referral_set_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type (
AtEpoch uint64
WasEligible bool
ReferralSetRunningNotionalTakerVolume string
ReferrerTakerVolume string
RefereesStats []*eventspb.RefereeStats
VegaTime time.Time
RewardFactor string
Expand All @@ -41,6 +42,7 @@ type (
AtEpoch uint64
WasEligible bool
ReferralSetRunningNotionalTakerVolume string
ReferrerTakerVolume string
VegaTime time.Time
PartyID string
DiscountFactor string
Expand Down Expand Up @@ -78,6 +80,7 @@ func (s FlattenReferralSetStats) ToProto() *v2.ReferralSetStats {
return &v2.ReferralSetStats{
AtEpoch: s.AtEpoch,
ReferralSetRunningNotionalTakerVolume: s.ReferralSetRunningNotionalTakerVolume,
ReferrerTakerVolume: s.ReferrerTakerVolume,
PartyId: s.PartyID,
DiscountFactor: s.DiscountFactor,
RewardFactor: s.RewardFactor,
Expand Down Expand Up @@ -105,6 +108,7 @@ func ReferralSetStatsFromProto(proto *eventspb.ReferralSetStatsUpdated, vegaTime
AtEpoch: proto.AtEpoch,
WasEligible: proto.WasEligible,
ReferralSetRunningNotionalTakerVolume: proto.ReferralSetRunningNotionalTakerVolume,
ReferrerTakerVolume: proto.ReferrerTakerVolume,
RefereesStats: proto.RefereesStats,
VegaTime: vegaTime,
RewardFactor: proto.RewardFactor,
Expand Down
61 changes: 61 additions & 0 deletions datanode/gateway/graphql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions datanode/gateway/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -6279,6 +6279,8 @@ type ReferralSetStats {
rewardsMultiplier: String!
"The proportion of the referees taker fees to be rewarded to the referrer."
rewardsFactorMultiplier: String!
"The referrer's taker volume"
referrerTakerVolume: String!
}

"Team record containing the team information."
Expand Down
12 changes: 6 additions & 6 deletions datanode/networkhistory/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ func TestMain(t *testing.M) {
log.Infof("%s", goldenSourceHistorySegment[4000].HistorySegmentID)
log.Infof("%s", goldenSourceHistorySegment[5000].HistorySegmentID)

panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "Qmbhc6dntm8gZnrVVMueciLJqXH6aViaZnRCE2iTn7oi3V", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmXbhq694htpQP75BZTz4nDv9dreRA7Mn8aqBrbiz4yeNJ", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmPExgLTRVgy7825bDC6ngWZHEe6sTgtDKZsf6owR5WhEe", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmeYsvai83fNURJxU4yoEPNDvjzyx3j5gB2bXzTzbxFso1", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmUqjB5chgFhPABLA4FCcT4xUXvZkGZ4wG7eMKHuZfeWhw", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmWvWQeEpSmM4VrihfXq6iBxwsb7HZHm7XoKUs8SdVt7C8", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "QmQGrPqM72evPWmpaCduiEzcndEwzsVM8PrWe7w9gqPtv4", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmQKVcMmo9D9Bx8MFHg7q2hkmDksi5zmYhanvzXmpNk7kx", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmVAP6NsCAC3qTN2VnVxyBY6A8s4tVLqppUhWL8ArkVePg", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmUXg7sLvDd53NmykEptcKUyzkb6RUvNXGR9StjjyUiE2K", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmUCbCaewhdYsEPHpC7f2uynSJjcnMXY5GEw4UKUWuJLCu", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmcGBEV4XiM98idDU9evmAArP7pZwmFe9DSP2aYsnfTg9w", snapshots)
}, postgresRuntimePath, sqlFs)

if exitCode != 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ alter table referral_set_stats
alter table referral_set_stats
add column was_eligible bool not null default true;

alter table referral_set_stats
add column referrer_taker_volume text not null default '0';

-- +goose Down

alter table referral_set_stats
Expand All @@ -18,3 +21,6 @@ alter table referral_set_stats

alter table referral_set_stats
drop column was_eligible;

alter table referral_set_stats
drop column referrer_taker_volume;
5 changes: 4 additions & 1 deletion datanode/sqlstore/referral_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ func (rs *ReferralSets) AddReferralSetStats(ctx context.Context, stats *entities
at_epoch,
was_eligible,
referral_set_running_notional_taker_volume,
referrer_taker_volume,
referees_stats,
vega_time,
reward_factor,
rewards_multiplier,
rewards_factor_multiplier)
values ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`,
stats.SetID,
stats.AtEpoch,
stats.WasEligible,
stats.ReferralSetRunningNotionalTakerVolume,
stats.ReferrerTakerVolume,
refereesStats,
stats.VegaTime,
stats.RewardFactor,
Expand All @@ -178,6 +180,7 @@ func (rs *ReferralSets) GetReferralSetStats(ctx context.Context, setID *entities
was_eligible,
vega_time,
referral_set_running_notional_taker_volume,
referrer_taker_volume,
reward_factor,
referee_stats->>'party_id' as party_id,
referee_stats->>'discount_factor' as discount_factor,
Expand Down
2 changes: 2 additions & 0 deletions datanode/sqlstore/referral_sets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ func TestReferralSets_AddReferralSetStats(t *testing.T) {
SetID: set.ID,
AtEpoch: epoch,
ReferralSetRunningNotionalTakerVolume: takerVolume,
ReferrerTakerVolume: "100",
RefereesStats: getRefereeStats(t, refs, "0.01"),
VegaTime: block.VegaTime,
RewardFactor: "0.02",
Expand All @@ -513,6 +514,7 @@ func TestReferralSets_AddReferralSetStats(t *testing.T) {
SetID: set.ID,
AtEpoch: epoch,
ReferralSetRunningNotionalTakerVolume: takerVolume,
ReferrerTakerVolume: "100",
RefereesStats: getRefereeStats(t, refs, "0.01"),
VegaTime: block.VegaTime,
RewardFactor: "0.02",
Expand Down
Loading

0 comments on commit a666f5b

Please sign in to comment.