Skip to content

Commit

Permalink
Check affiliate tiers are strictly increasing before updating tiers
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Sep 6, 2024
1 parent 0ce92b3 commit a326931
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
21 changes: 17 additions & 4 deletions protocol/x/affiliates/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,22 @@ func (k Keeper) GetTierForAffiliate(

// UpdateAffiliateTiers updates the affiliate tiers.
// Used primarily through governance.
func (k Keeper) UpdateAffiliateTiers(ctx sdk.Context, affiliateTiers types.AffiliateTiers) {
func (k Keeper) UpdateAffiliateTiers(ctx sdk.Context, affiliateTiers types.AffiliateTiers) error {
store := ctx.KVStore(k.storeKey)
// TODO(OTE-779): Check strictly increasing volume and
// staking requirements hold in UpdateAffiliateTiers
store.Set([]byte(types.AffiliateTiersKey), k.cdc.MustMarshal(&affiliateTiers))
affiliateTiersBytes, err := k.cdc.Marshal(&affiliateTiers)
if err != nil {
return errorsmod.Wrapf(types.ErrInvalidAffiliateTiers,
"error marshalling affiliate tiers: %s", err)
}
tiers := affiliateTiers.GetTiers()
// start at 1, since 0 is the default tier.
for i := 1; i < len(tiers); i++ {
if tiers[i].ReqReferredVolumeQuoteQuantums <= tiers[i-1].ReqReferredVolumeQuoteQuantums ||
tiers[i].ReqStakedWholeCoins <= tiers[i-1].ReqStakedWholeCoins {
return errorsmod.Wrapf(types.ErrInvalidAffiliateTiers,
"tiers values must be strictly increasing")
}
}
store.Set([]byte(types.AffiliateTiersKey), affiliateTiersBytes)
return nil
}
69 changes: 56 additions & 13 deletions protocol/x/affiliates/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ func TestGetTakerFeeShareViaReferredVolume(t *testing.T) {
k := tApp.App.AffiliatesKeeper
// Set up affiliate tiers
affiliateTiers := types.DefaultAffiliateTiers
k.UpdateAffiliateTiers(ctx, affiliateTiers)
err := k.UpdateAffiliateTiers(ctx, affiliateTiers)
require.NoError(t, err)
stakingKeeper := tApp.App.StakingKeeper

err := stakingKeeper.SetDelegation(ctx,
err = stakingKeeper.SetDelegation(ctx,
stakingtypes.NewDelegation(constants.AliceAccAddress.String(),
constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(
new(big.Int).Mul(
Expand Down Expand Up @@ -194,13 +195,14 @@ func TestGetTakerFeeShareViaStakedAmount(t *testing.T) {
ctx = ctx.WithBlockTime(time.Now())
// Set up affiliate tiers
affiliateTiers := types.DefaultAffiliateTiers
k.UpdateAffiliateTiers(ctx, affiliateTiers)
err := k.UpdateAffiliateTiers(ctx, affiliateTiers)
require.NoError(t, err)

// Register affiliate and referee
affiliate := constants.AliceAccAddress.String()
referee := constants.BobAccAddress.String()
stakingKeeper := tApp.App.StakingKeeper
err := stakingKeeper.SetDelegation(ctx,
err = stakingKeeper.SetDelegation(ctx,
stakingtypes.NewDelegation(constants.AliceAccAddress.String(),
constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(
new(big.Int).Mul(
Expand Down Expand Up @@ -248,12 +250,13 @@ func TestGetTierForAffiliate_VolumeAndStake(t *testing.T) {
k := tApp.App.AffiliatesKeeper

affiliateTiers := types.DefaultAffiliateTiers
k.UpdateAffiliateTiers(ctx, affiliateTiers)
err := k.UpdateAffiliateTiers(ctx, affiliateTiers)
require.NoError(t, err)
affiliate := constants.AliceAccAddress.String()
referee := constants.BobAccAddress.String()
stakingKeeper := tApp.App.StakingKeeper

err := stakingKeeper.SetDelegation(ctx,
err = stakingKeeper.SetDelegation(ctx,
stakingtypes.NewDelegation(constants.AliceAccAddress.String(),
constants.AliceValAddress.String(), math.LegacyNewDecFromBigInt(
new(big.Int).Mul(
Expand Down Expand Up @@ -296,12 +299,52 @@ func TestUpdateAffiliateTiers(t *testing.T) {
ctx := tApp.InitChain()
k := tApp.App.AffiliatesKeeper

// Set up valid affiliate tiers
validTiers := types.DefaultAffiliateTiers
k.UpdateAffiliateTiers(ctx, validTiers)
tests := []struct {
name string
affiliateTiers types.AffiliateTiers
expectedError error
}{
{
name: "Valid tiers",
affiliateTiers: types.DefaultAffiliateTiers,
expectedError: nil,
},
{
name: "Invalid tiers - decreasing volume requirement",
affiliateTiers: types.AffiliateTiers{
Tiers: []types.AffiliateTiers_Tier{
{ReqReferredVolumeQuoteQuantums: 1000, ReqStakedWholeCoins: 100, TakerFeeSharePpm: 100},
{ReqReferredVolumeQuoteQuantums: 500, ReqStakedWholeCoins: 200, TakerFeeSharePpm: 200},
},
},
expectedError: types.ErrInvalidAffiliateTiers,
},
{
name: "Invalid tiers - decreasing staking requirement",
affiliateTiers: types.AffiliateTiers{
Tiers: []types.AffiliateTiers_Tier{
{ReqReferredVolumeQuoteQuantums: 1000, ReqStakedWholeCoins: 200, TakerFeeSharePpm: 100},
{ReqReferredVolumeQuoteQuantums: 2000, ReqStakedWholeCoins: 100, TakerFeeSharePpm: 200},
},
},
expectedError: types.ErrInvalidAffiliateTiers,
},
}

// Retrieve and validate updated tiers
updatedTiers, err := k.GetAllAffiliateTiers(ctx)
require.NoError(t, err)
require.Equal(t, validTiers, updatedTiers)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := k.UpdateAffiliateTiers(ctx, tc.affiliateTiers)

if tc.expectedError != nil {
require.ErrorIs(t, err, tc.expectedError)
} else {
require.NoError(t, err)

// Retrieve and validate updated tiers
updatedTiers, err := k.GetAllAffiliateTiers(ctx)
require.NoError(t, err)
require.Equal(t, tc.affiliateTiers, updatedTiers)
}
})
}
}

0 comments on commit a326931

Please sign in to comment.