diff --git a/x/pairing/keeper/grpc_query_monthly_payout.go b/x/pairing/keeper/grpc_query_monthly_payout.go index 477383b40c..34666a4a9f 100644 --- a/x/pairing/keeper/grpc_query_monthly_payout.go +++ b/x/pairing/keeper/grpc_query_monthly_payout.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/lavanet/lava/utils" "github.com/lavanet/lava/x/pairing/types" + subsciption "github.com/lavanet/lava/x/subscription/keeper" subsciptiontypes "github.com/lavanet/lava/x/subscription/types" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -53,7 +54,13 @@ func (k Keeper) MonthlyPayout(goCtx context.Context, req *types.QueryMonthlyPayo if !found { continue } - totalMonthlyReward := k.subscriptionKeeper.CalcTotalMonthlyReward(ctx, plan, providerCu, subObj.MonthCuTotal-subObj.MonthCuLeft) + totalTokenAmount := plan.Price.Amount + totalCuTracked := subObj.MonthCuTotal - subObj.MonthCuLeft + if plan.Price.Amount.Quo(sdk.NewIntFromUint64(totalCuTracked)).GT(sdk.NewIntFromUint64(subsciption.LIMIT_TOKEN_PER_CU)) { + totalTokenAmount = sdk.NewIntFromUint64(subsciption.LIMIT_TOKEN_PER_CU * totalCuTracked) + } + + totalMonthlyReward := k.subscriptionKeeper.CalcTotalMonthlyReward(ctx, totalTokenAmount, providerCu, totalCuTracked) // calculate only the provider reward providerReward, err := k.dualstakingKeeper.RewardProvidersAndDelegators(ctx, providerAddr, chainID, totalMonthlyReward, subsciptiontypes.ModuleName, true) diff --git a/x/pairing/types/expected_keepers.go b/x/pairing/types/expected_keepers.go index 0cc011eab3..22d26d331d 100644 --- a/x/pairing/types/expected_keepers.go +++ b/x/pairing/types/expected_keepers.go @@ -82,7 +82,7 @@ type SubscriptionKeeper interface { GetSubscription(ctx sdk.Context, consumer string) (val subscriptiontypes.Subscription, found bool) GetAllSubTrackedCuIndices(ctx sdk.Context, sub string) []string GetTrackedCu(ctx sdk.Context, sub string, provider string, chainID string, block uint64) (cu uint64, found bool, key string) - CalcTotalMonthlyReward(ctx sdk.Context, plan planstypes.Plan, trackedCu uint64, totalCuUsedBySub uint64) math.Int + CalcTotalMonthlyReward(ctx sdk.Context, totalAmount math.Int, trackedCu uint64, totalCuUsedBySub uint64) math.Int AddTrackedCu(ctx sdk.Context, sub string, provider string, chainID string, cu uint64, block uint64) error GetAllSubscriptionsIndices(ctx sdk.Context) []string } diff --git a/x/subscription/keeper/cu_tracker.go b/x/subscription/keeper/cu_tracker.go index 554780403c..52e200946b 100644 --- a/x/subscription/keeper/cu_tracker.go +++ b/x/subscription/keeper/cu_tracker.go @@ -8,10 +8,11 @@ import ( legacyerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/lavanet/lava/utils" epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types" - planstypes "github.com/lavanet/lava/x/plans/types" "github.com/lavanet/lava/x/subscription/types" ) +const LIMIT_TOKEN_PER_CU = 100 + // GetTrackedCu gets the tracked CU counter (with QoS influence) and the trackedCu entry's block func (k Keeper) GetTrackedCu(ctx sdk.Context, sub string, provider string, chainID string, subBlock uint64) (cu uint64, found bool, key string) { cuTrackerKey := types.CuTrackerKey(sub, provider, chainID) @@ -143,6 +144,11 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes return } + totalTokenAmount := plan.Price.Amount + if plan.Price.Amount.Quo(sdk.NewIntFromUint64(totalCuTracked)).GT(sdk.NewIntFromUint64(LIMIT_TOKEN_PER_CU)) { + totalTokenAmount = sdk.NewIntFromUint64(LIMIT_TOKEN_PER_CU * totalCuTracked) + } + for _, trackedCuInfo := range trackedCuList { trackedCu := trackedCuInfo.trackedCu provider := trackedCuInfo.provider @@ -162,7 +168,8 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes // monthly reward = (tracked_CU / total_CU_used_in_sub_this_month) * plan_price // TODO: deal with the reward's remainder (uint division...) - totalMonthlyReward := k.CalcTotalMonthlyReward(ctx, plan, trackedCu, totalCuTracked) + + totalMonthlyReward := k.CalcTotalMonthlyReward(ctx, totalTokenAmount, trackedCu, totalCuTracked) // calculate the provider reward (smaller than totalMonthlyReward // because it's shared with delegators) @@ -207,12 +214,12 @@ func (k Keeper) RewardAndResetCuTracker(ctx sdk.Context, cuTrackerTimerKeyBytes } } -func (k Keeper) CalcTotalMonthlyReward(ctx sdk.Context, plan planstypes.Plan, trackedCu uint64, totalCuUsedBySub uint64) math.Int { +func (k Keeper) CalcTotalMonthlyReward(ctx sdk.Context, totalAmount math.Int, trackedCu uint64, totalCuUsedBySub uint64) math.Int { // TODO: deal with the reward's remainder (uint division...) // monthly reward = (tracked_CU / total_CU_used_in_sub_this_month) * plan_price if totalCuUsedBySub == 0 { return math.ZeroInt() } - totalMonthlyReward := plan.Price.Amount.MulRaw(int64(trackedCu)).QuoRaw(int64(totalCuUsedBySub)) + totalMonthlyReward := totalAmount.MulRaw(int64(trackedCu)).QuoRaw(int64(totalCuUsedBySub)) return totalMonthlyReward }