From 710269924d7b0d525215dfac7404572d73c4528d Mon Sep 17 00:00:00 2001 From: Yaroms Date: Mon, 12 Feb 2024 20:17:09 +0200 Subject: [PATCH 1/4] fix credit issue --- x/subscription/keeper/subscription.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/x/subscription/keeper/subscription.go b/x/subscription/keeper/subscription.go index 29ddca3388..32d2d5fe9f 100644 --- a/x/subscription/keeper/subscription.go +++ b/x/subscription/keeper/subscription.go @@ -622,6 +622,7 @@ func (k Keeper) CreateFutureSubscription(ctx sdk.Context, newPlanPrice := plan.GetPrice() newPlanPrice.Amount = newPlanPrice.Amount.MulRaw(int64(duration)) + chargePrice := newPlanPrice k.applyPlanDiscountIfEligible(duration, &plan, &newPlanPrice) if sub.FutureSubscription != nil { @@ -635,13 +636,8 @@ func (k Keeper) CreateFutureSubscription(ctx sdk.Context, ) } - consumerBoughDuration := sub.FutureSubscription.DurationBought - consumerPaid := currentPlan.GetPrice() - consumerPaid.Amount = consumerPaid.Amount.MulRaw(int64(consumerBoughDuration)) - k.applyPlanDiscountIfEligible(consumerBoughDuration, &plan, &consumerPaid) - - if newPlanPrice.Amount.GT(consumerPaid.Amount) { - newPlanPrice.Amount = newPlanPrice.Amount.Sub(consumerPaid.Amount) + if newPlanPrice.Amount.GT(sub.FutureSubscription.Credit.Amount) { + chargePrice.Amount = newPlanPrice.Amount.Sub(sub.FutureSubscription.Credit.Amount) details := map[string]string{ "creator": creator, @@ -658,7 +654,7 @@ func (k Keeper) CreateFutureSubscription(ctx sdk.Context, } } - err = k.chargeFromCreatorAccountToModule(ctx, creatorAcct, newPlanPrice) + err = k.chargeFromCreatorAccountToModule(ctx, creatorAcct, chargePrice) if err != nil { return err } From c97882c70260e77af21723ca8e67a65d61d9d0c6 Mon Sep 17 00:00:00 2001 From: Yarom Swisa Date: Tue, 13 Feb 2024 11:23:31 +0200 Subject: [PATCH 2/4] fix --- x/subscription/keeper/subscription.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/subscription/keeper/subscription.go b/x/subscription/keeper/subscription.go index 32d2d5fe9f..bec7c4d8d3 100644 --- a/x/subscription/keeper/subscription.go +++ b/x/subscription/keeper/subscription.go @@ -622,8 +622,8 @@ func (k Keeper) CreateFutureSubscription(ctx sdk.Context, newPlanPrice := plan.GetPrice() newPlanPrice.Amount = newPlanPrice.Amount.MulRaw(int64(duration)) - chargePrice := newPlanPrice k.applyPlanDiscountIfEligible(duration, &plan, &newPlanPrice) + chargePrice := newPlanPrice if sub.FutureSubscription != nil { // Consumer already has a future subscription From fcd28d4e040a603030db1e2b6f53b8eb680e5dd1 Mon Sep 17 00:00:00 2001 From: Yarom Swisa Date: Mon, 19 Feb 2024 12:13:15 +0200 Subject: [PATCH 3/4] test --- x/subscription/keeper/subscription_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x/subscription/keeper/subscription_test.go b/x/subscription/keeper/subscription_test.go index 80f6655849..4f32543ba8 100644 --- a/x/subscription/keeper/subscription_test.go +++ b/x/subscription/keeper/subscription_test.go @@ -1734,6 +1734,12 @@ func TestSubscriptionAdvancePurchaseSuccessOnPricierPlan_SameBlock(t *testing.T) // Make sure the balance is updated require.Equal(t, consumerBalance, ts.GetBalance(consumerAcc.Addr), testName) + + // Make sure the credit is properly updated + sub, found := ts.getSubscription(consumerAddr) + require.True(t, found) + require.Equal(t, testCase.price, sub.FutureSubscription.Credit.Amount.Int64()) + } } From c1fc6e8ebc0fe4b13084bb36fa390bcf9c07b0c0 Mon Sep 17 00:00:00 2001 From: Yarom Swisa Date: Mon, 19 Feb 2024 12:20:07 +0200 Subject: [PATCH 4/4] lint --- x/subscription/keeper/subscription_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/subscription/keeper/subscription_test.go b/x/subscription/keeper/subscription_test.go index 4f32543ba8..111ae77c05 100644 --- a/x/subscription/keeper/subscription_test.go +++ b/x/subscription/keeper/subscription_test.go @@ -1739,7 +1739,6 @@ func TestSubscriptionAdvancePurchaseSuccessOnPricierPlan_SameBlock(t *testing.T) sub, found := ts.getSubscription(consumerAddr) require.True(t, found) require.Equal(t, testCase.price, sub.FutureSubscription.Credit.Amount.Int64()) - } }