@@ -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
2120type (
@@ -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