Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(affiliates): Fix logic in affiliate referred volume aggregation (backport #2623) #2624

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions protocol/x/affiliates/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,34 +343,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 @@ -781,6 +781,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
Loading