/
grpc_query_provider_monthly_payout.go
85 lines (70 loc) · 3.17 KB
/
grpc_query_provider_monthly_payout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package keeper
import (
"context"
"fmt"
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"
)
func (k Keeper) ProviderMonthlyPayout(goCtx context.Context, req *types.QueryProviderMonthlyPayoutRequest) (*types.QueryProviderMonthlyPayoutResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
var total uint64
subs := k.subscriptionKeeper.GetAllSubscriptionsIndices(ctx)
var details []*types.SubscriptionPayout
for _, sub := range subs {
trackedCuInds := k.subscriptionKeeper.GetAllSubTrackedCuIndices(ctx, subsciptiontypes.CuTrackerKey(sub, req.Provider, ""))
for _, trackCU := range trackedCuInds {
_, _, chainID := subsciptiontypes.DecodeCuTrackerKey(trackCU)
subObj, found := k.subscriptionKeeper.GetSubscription(ctx, sub)
if !found {
return nil, utils.LavaFormatError("cannot get tracked CU", fmt.Errorf("subscription not found"),
utils.Attribute{Key: "sub", Value: sub},
utils.Attribute{Key: "provider", Value: req.Provider},
utils.Attribute{Key: "chain_id", Value: chainID},
)
}
plan, err := k.subscriptionKeeper.GetPlanFromSubscription(ctx, sub, subObj.Block)
if err != nil {
return nil, err
}
providerCu, found, _ := k.subscriptionKeeper.GetTrackedCu(ctx, sub, req.Provider, chainID, subObj.Block)
if !found {
continue
}
totalTokenAmount := plan.Price.Amount
totalCuTracked := subObj.MonthCuTotal - subObj.MonthCuLeft
// Sanity check - totalCuTracked > 0
if totalCuTracked <= 0 {
return nil, utils.LavaFormatWarning("totalCuTracked is zero or negative", fmt.Errorf("critical: Attempt to divide by zero or negative number"),
utils.LogAttr("subObj.MonthCuTotal", subObj.MonthCuTotal),
utils.LogAttr("subObj.MonthCuLeft", subObj.MonthCuLeft),
utils.LogAttr("totalCuTracked", totalCuTracked),
utils.LogAttr("sub_block", subObj.Block),
)
}
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, req.Provider, chainID, sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), totalMonthlyReward)), subsciptiontypes.ModuleName, true, true, true)
if err != nil {
return nil, err
}
details = append(details, &types.SubscriptionPayout{
Subscription: sub,
ChainId: chainID,
Amount: providerReward.AmountOf(k.stakingKeeper.BondDenom(ctx)).Uint64(),
})
total += providerReward.AmountOf(k.stakingKeeper.BondDenom(ctx)).Uint64()
}
}
return &types.QueryProviderMonthlyPayoutResponse{Total: total, Details: details}, nil
}