From f76f6ef6388aa069c7e7a38e754bd42d54309415 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:50:09 -0500 Subject: [PATCH] fix(affiliates): Reject self referral in `RegisterAffiliate` (backport #2637) (#2638) Co-authored-by: Teddy Ding --- protocol/x/affiliates/keeper/keeper.go | 6 ++++++ protocol/x/affiliates/keeper/keeper_test.go | 9 +++++++++ protocol/x/affiliates/types/errors.go | 1 + 3 files changed, 16 insertions(+) diff --git a/protocol/x/affiliates/keeper/keeper.go b/protocol/x/affiliates/keeper/keeper.go index 92554b8f1c..52cb1bfd3a 100644 --- a/protocol/x/affiliates/keeper/keeper.go +++ b/protocol/x/affiliates/keeper/keeper.go @@ -61,6 +61,12 @@ func (k Keeper) RegisterAffiliate( referee string, affiliateAddr string, ) error { + if referee == affiliateAddr { + return errorsmod.Wrapf( + types.ErrSelfReferral, "referee: %s, affiliate: %s", + referee, affiliateAddr, + ) + } if _, err := sdk.AccAddressFromBech32(referee); err != nil { return errorsmod.Wrapf(types.ErrInvalidAddress, "referee: %s", referee) } diff --git a/protocol/x/affiliates/keeper/keeper_test.go b/protocol/x/affiliates/keeper/keeper_test.go index 7a417cbdd6..5a889d1f9d 100644 --- a/protocol/x/affiliates/keeper/keeper_test.go +++ b/protocol/x/affiliates/keeper/keeper_test.go @@ -88,6 +88,15 @@ func TestRegisterAffiliate_GetReferredBy(t *testing.T) { // No setup needed for this test case }, }, + { + name: "Self referral", + referee: constants.AliceAccAddress.String(), + affiliate: constants.AliceAccAddress.String(), + expectError: types.ErrSelfReferral, + setup: func(t *testing.T, ctx sdk.Context, k *keeper.Keeper) { + // No setup needed for this test case + }, + }, } for _, tc := range tests { diff --git a/protocol/x/affiliates/types/errors.go b/protocol/x/affiliates/types/errors.go index ed8a2062d3..a60b522930 100644 --- a/protocol/x/affiliates/types/errors.go +++ b/protocol/x/affiliates/types/errors.go @@ -16,4 +16,5 @@ var ( ModuleName, 8, "Duplicate affiliate address for whitelist") ErrAffiliateTiersNotSet = errorsmod.Register(ModuleName, 9, "Affiliate tiers not set (affiliate program is not active)") + ErrSelfReferral = errorsmod.Register(ModuleName, 10, "Self referral not allowed") )