-
Notifications
You must be signed in to change notification settings - Fork 199
/
grpc_query_subscription_monthly_payout.go
88 lines (72 loc) · 2.98 KB
/
grpc_query_subscription_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
86
87
88
package keeper
import (
"context"
"fmt"
"sort"
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) SubscriptionMonthlyPayout(goCtx context.Context, req *types.QuerySubscriptionMonthlyPayoutRequest) (*types.QuerySubscriptionMonthlyPayoutResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
var total uint64
detailsMap := make(map[string][]*types.ProviderPayout)
var details []*types.ChainIDPayout
sub := req.Consumer
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},
)
}
plan, err := k.subscriptionKeeper.GetPlanFromSubscription(ctx, sub, subObj.Block)
if err != nil {
return nil, err
}
trackedCuInds := k.subscriptionKeeper.GetAllSubTrackedCuIndices(ctx, req.Consumer)
for _, trackCU := range trackedCuInds {
_, provider, chainID := subsciptiontypes.DecodeCuTrackerKey(trackCU)
providerCu, found, _ := k.subscriptionKeeper.GetTrackedCu(ctx, sub, 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),
)
}
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)
total += totalMonthlyReward.Uint64()
if _, ok := detailsMap[chainID]; ok {
payouts := detailsMap[chainID]
payouts = append(payouts, &types.ProviderPayout{Provider: provider, Amount: totalMonthlyReward.Uint64()})
detailsMap[chainID] = payouts
} else {
detailsMap[chainID] = []*types.ProviderPayout{{Provider: provider, Amount: totalMonthlyReward.Uint64()}}
}
}
var chains []string
for chainID := range detailsMap {
chains = append(chains, chainID)
}
sort.Strings(chains)
for _, chainID := range chains {
details = append(details, &types.ChainIDPayout{ChainId: chainID, Payouts: detailsMap[chainID]})
}
return &types.QuerySubscriptionMonthlyPayoutResponse{Total: total, Details: details}, nil
}