/
grpc_query_price_check.go
44 lines (33 loc) · 1.1 KB
/
grpc_query_price_check.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
package keeper
import (
"context"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (k Keeper) PriceCheck(c context.Context, req *types.QueryPriceCheckRequest) (*types.QueryPriceCheckResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
duration, err := time.ParseDuration(req.Duration)
if err != nil {
return nil, fmt.Errorf("duration can't be parsed: %s", err.Error())
}
timeMonth := time.Hour * 24 * 30
if duration.Truncate(timeMonth) <= 0 {
return nil, fmt.Errorf("duration can't be less than 1 month")
}
bytes := req.Bytes
size := sdk.NewInt(bytes)
s := size.Quo(sdk.NewInt(1_000_000)).Int64() // round to mbs
if s <= 0 {
s = 1
}
hours := sdk.NewDec(duration.Milliseconds()).Quo(sdk.NewDec(60 * 60 * 1000))
cost := k.GetStorageCostKbs(ctx, s*1000, hours.TruncateInt64()) // pay for 200 years in mbs
return &types.QueryPriceCheckResponse{Price: cost.Int64()}, nil
}