Skip to content

Commit 66e9cc0

Browse files
committed
Revise aggregation to reference user stats
1 parent c91c03a commit 66e9cc0

File tree

8 files changed

+242
-262
lines changed

8 files changed

+242
-262
lines changed

indexer/packages/v4-protos/src/codegen/dydxprotocol/stats/stats.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ export interface UserStats {
131131
/** Affiliate revenue generated in quantums */
132132

133133
affiliateRevenueGeneratedQuantums: Long;
134+
/** Affiliate referred volume in quote quantums */
135+
136+
affiliateReferredVolumeQuoteQuantums: Long;
134137
}
135138
/**
136139
* UserStats stores stats for a User. This is the sum of all stats for a user in
@@ -146,6 +149,9 @@ export interface UserStatsSDKType {
146149
/** Affiliate revenue generated in quantums */
147150

148151
affiliate_revenue_generated_quantums: Long;
152+
/** Affiliate referred volume in quote quantums */
153+
154+
affiliate_referred_volume_quote_quantums: Long;
149155
}
150156
/** CachedStakeAmount stores the last calculated total staked amount for address */
151157

@@ -496,7 +502,8 @@ function createBaseUserStats(): UserStats {
496502
return {
497503
takerNotional: Long.UZERO,
498504
makerNotional: Long.UZERO,
499-
affiliateRevenueGeneratedQuantums: Long.UZERO
505+
affiliateRevenueGeneratedQuantums: Long.UZERO,
506+
affiliateReferredVolumeQuoteQuantums: Long.UZERO
500507
};
501508
}
502509

@@ -514,6 +521,10 @@ export const UserStats = {
514521
writer.uint32(24).uint64(message.affiliateRevenueGeneratedQuantums);
515522
}
516523

524+
if (!message.affiliateReferredVolumeQuoteQuantums.isZero()) {
525+
writer.uint32(32).uint64(message.affiliateReferredVolumeQuoteQuantums);
526+
}
527+
517528
return writer;
518529
},
519530

@@ -538,6 +549,10 @@ export const UserStats = {
538549
message.affiliateRevenueGeneratedQuantums = (reader.uint64() as Long);
539550
break;
540551

552+
case 4:
553+
message.affiliateReferredVolumeQuoteQuantums = (reader.uint64() as Long);
554+
break;
555+
541556
default:
542557
reader.skipType(tag & 7);
543558
break;
@@ -552,6 +567,7 @@ export const UserStats = {
552567
message.takerNotional = object.takerNotional !== undefined && object.takerNotional !== null ? Long.fromValue(object.takerNotional) : Long.UZERO;
553568
message.makerNotional = object.makerNotional !== undefined && object.makerNotional !== null ? Long.fromValue(object.makerNotional) : Long.UZERO;
554569
message.affiliateRevenueGeneratedQuantums = object.affiliateRevenueGeneratedQuantums !== undefined && object.affiliateRevenueGeneratedQuantums !== null ? Long.fromValue(object.affiliateRevenueGeneratedQuantums) : Long.UZERO;
570+
message.affiliateReferredVolumeQuoteQuantums = object.affiliateReferredVolumeQuoteQuantums !== undefined && object.affiliateReferredVolumeQuoteQuantums !== null ? Long.fromValue(object.affiliateReferredVolumeQuoteQuantums) : Long.UZERO;
555571
return message;
556572
}
557573

proto/dydxprotocol/stats/stats.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ message UserStats {
7070

7171
// Affiliate revenue generated in quantums
7272
uint64 affiliate_revenue_generated_quantums = 3;
73+
74+
// Affiliate referred volume in quote quantums
75+
uint64 affiliate_referred_volume_quote_quantums = 4;
7376
}
7477

7578
// CachedStakeAmount stores the last calculated total staked amount for address

protocol/app/app.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,6 @@ func New(
962962
)
963963
affiliatesModule := affiliatesmodule.NewAppModule(appCodec, app.AffiliatesKeeper)
964964

965-
// Register the affiliates keeper to be notified when stats expire
966-
app.StatsKeeper.AddStatsExpirationHook(&app.AffiliatesKeeper)
967-
968965
app.MarketMapKeeper = *marketmapmodulekeeper.NewKeeper(
969966
runtime.NewKVStoreService(keys[marketmapmoduletypes.StoreKey]),
970967
appCodec,

protocol/x/affiliates/keeper/keeper.go

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
1616
"github.com/dydxprotocol/v4-chain/protocol/lib"
1717
"github.com/dydxprotocol/v4-chain/protocol/x/affiliates/types"
18-
statstypes "github.com/dydxprotocol/v4-chain/protocol/x/stats/types"
1918
)
2019

2120
type (
@@ -280,9 +279,10 @@ func (k Keeper) GetTierForAffiliate(
280279
}
281280

282281
// Get the affiliate revenue generated in the last 30d
283-
referredVolume, err := k.GetReferredVolume(ctx, affiliateAddr)
284-
if err != nil {
285-
return 0, 0, err
282+
userStats := k.statsKeeper.GetUserStats(ctx, affiliateAddr)
283+
var referredVolume *big.Int
284+
if userStats != nil {
285+
referredVolume = big.NewInt(int64(userStats.AffiliateReferredVolumeQuoteQuantums))
286286
}
287287

288288
for index, tier := range tiers {
@@ -488,10 +488,10 @@ func (k Keeper) addReferredVolumeIfQualified(
488488
"referee %s, refereeUserStats is nil", referee)
489489
}
490490

491-
// If parameter is 0 then no limit is applied
492491
previousVolume := (refereeUserStats.TakerNotional + refereeUserStats.MakerNotional +
493492
previouslyAttributedVolume[referee])
494493

494+
// If parameter is 0 then no limit is applied
495495
cap := affiliateParams.Maximum_30DAttributableVolumePerReferredUserNotional
496496
if cap != 0 {
497497
if previousVolume >= cap {
@@ -505,9 +505,11 @@ func (k Keeper) addReferredVolumeIfQualified(
505505

506506
// Add the volume to the referrer on their 30d rolling window
507507
if volume > 0 {
508-
if err := k.AddReferredVolume(ctx, referrer, lib.BigU(volume)); err != nil {
509-
return err
508+
affiliateUserStats := k.statsKeeper.GetUserStats(ctx, referrer)
509+
if affiliateUserStats != nil {
510+
affiliateUserStats.AffiliateReferredVolumeQuoteQuantums += volume
510511
}
512+
k.statsKeeper.SetUserStats(ctx, referrer, affiliateUserStats)
511513
}
512514
return nil
513515
}
@@ -573,55 +575,3 @@ func (k Keeper) AggregateAffiliateReferredVolumeForFills(
573575
}
574576
return nil
575577
}
576-
577-
// OnStatsExpired implements StatsExpirationHook interface
578-
// Called when a user's stats expire from the 30d rolling window, update the
579-
// users referred volume to reflect the expired volume.
580-
func (k Keeper) OnStatsExpired(
581-
ctx sdk.Context,
582-
userAddress string,
583-
previousUserStats *statstypes.UserStats,
584-
resultingUserStats *statstypes.UserStats,
585-
) error {
586-
// Get affiliate parameters
587-
affiliateParams, err := k.GetAffiliateParameters(ctx)
588-
if err != nil {
589-
return err
590-
}
591-
592-
// Check if this user has a referrer (is a referee)
593-
referrer, found := k.GetReferredBy(ctx, userAddress)
594-
if !found {
595-
return nil // User is not referred, nothing to do
596-
}
597-
598-
// Get current referred volume for the referrer
599-
currentVolume, err := k.GetReferredVolume(ctx, referrer)
600-
if err != nil {
601-
return err
602-
}
603-
604-
previousVolume := previousUserStats.TakerNotional + previousUserStats.MakerNotional
605-
resultingVolume := resultingUserStats.TakerNotional + resultingUserStats.MakerNotional
606-
607-
// Volume didn't change
608-
if previousVolume == resultingVolume {
609-
return nil
610-
}
611-
612-
var newVolume *big.Int = lib.BigU(uint64(resultingVolume))
613-
cap := affiliateParams.Maximum_30DAttributableVolumePerReferredUserNotional
614-
if previousVolume >= cap &&
615-
resultingVolume < cap {
616-
deltaAttributedVolume := lib.BigU(cap - resultingVolume)
617-
// Subtract the expired volume (use taker volume for consistency with how it's added)
618-
newVolume = new(big.Int).Sub(currentVolume, deltaAttributedVolume)
619-
}
620-
621-
// Ensure it doesn't go negative
622-
if newVolume.Cmp(big.NewInt(0)) < 0 {
623-
newVolume = big.NewInt(0)
624-
}
625-
626-
return k.SetReferredVolume(ctx, referrer, newVolume)
627-
}

0 commit comments

Comments
 (0)