Skip to content

Commit

Permalink
fix(affiliates): Fix logic in affiliate referred volume aggregation (…
Browse files Browse the repository at this point in the history
…backport #2623) (#2625)

Co-authored-by: Teddy Ding <[email protected]>
  • Loading branch information
mergify[bot] and teddyding authored Dec 9, 2024
1 parent 62044d0 commit 493a7ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
40 changes: 24 additions & 16 deletions protocol/x/affiliates/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,34 +339,42 @@ func (k Keeper) GetAffiliateWhitelist(ctx sdk.Context) (types.AffiliateWhitelist
}
return affiliateWhitelist, nil
}

func (k Keeper) AggregateAffiliateReferredVolumeForFills(
ctx sdk.Context,
) error {
blockStats := k.statsKeeper.GetBlockStats(ctx)
referredByCache := make(map[string]string)

for _, fill := range blockStats.Fills {
// Add taker's referred volume to the cache
if _, ok := referredByCache[fill.Taker]; !ok {
referredByAddrTaker, found := k.GetReferredBy(ctx, fill.Taker)
if !found {
continue
// Process taker's referred volume
referredByAddrTaker, cached := referredByCache[fill.Taker]
if !cached {
var found bool
referredByAddrTaker, found = k.GetReferredBy(ctx, fill.Taker)
if found {
referredByCache[fill.Taker] = referredByAddrTaker
}
referredByCache[fill.Taker] = referredByAddrTaker
}
if err := k.AddReferredVolume(ctx, referredByCache[fill.Taker], lib.BigU(fill.Notional)); err != nil {
return err
if referredByAddrTaker != "" {
if err := k.AddReferredVolume(ctx, referredByAddrTaker, lib.BigU(fill.Notional)); err != nil {
return err
}
}

// Add maker's referred volume to the cache
if _, ok := referredByCache[fill.Maker]; !ok {
referredByAddrMaker, found := k.GetReferredBy(ctx, fill.Maker)
if !found {
continue
// Process maker's referred volume
referredByAddrMaker, cached := referredByCache[fill.Maker]
if !cached {
var found bool
referredByAddrMaker, found = k.GetReferredBy(ctx, fill.Maker)
if found {
referredByCache[fill.Maker] = referredByAddrMaker
}
referredByCache[fill.Maker] = referredByAddrMaker
}
if err := k.AddReferredVolume(ctx, referredByCache[fill.Maker], lib.BigU(fill.Notional)); err != nil {
return err
if referredByAddrMaker != "" {
if err := k.AddReferredVolume(ctx, referredByAddrMaker, lib.BigU(fill.Notional)); err != nil {
return err
}
}
}
return nil
Expand Down
23 changes: 23 additions & 0 deletions protocol/x/affiliates/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,29 @@ func TestAggregateAffiliateReferredVolumeForFills(t *testing.T) {
})
},
},
{
name: "2 referrals, takers not referred, maker referred",
referrals: 2,
expectedVolume: big.NewInt(300_000_000_000),
setup: func(t *testing.T, ctx sdk.Context, k *keeper.Keeper, statsKeeper *statskeeper.Keeper) {
err := k.RegisterAffiliate(ctx, maker, affiliate)
require.NoError(t, err)
statsKeeper.SetBlockStats(ctx, &statstypes.BlockStats{
Fills: []*statstypes.BlockStats_Fill{
{
Taker: referee1,
Maker: maker,
Notional: 100_000_000_000,
},
{
Taker: referee2,
Maker: maker,
Notional: 200_000_000_000,
},
},
})
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 493a7ff

Please sign in to comment.