Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pay periods #173

Merged
merged 4 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion proto/canine_chain/storage/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ service Query {
"/jackal-dao/canine-chain/storage/payment_info";
}

// this line is used by starport scaffolding # 2
// Queries whether user can upload a file based on size
rpc FileUploadCheck(QueryFileUploadCheck) returns (QueryFileUploadCheckResponse) {
option (google.api.http).get =
"/jackal-dao/canine-chain/storage/file_upload_check";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -241,4 +245,13 @@ message QueryAllStoragePaymentInfoResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryFileUploadCheck {
string address = 1;
int64 bytes = 2;
}

message QueryFileUploadCheckResponse {
bool valid = 1;
}

// this line is used by starport scaffolding # 3
2 changes: 2 additions & 0 deletions x/storage/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdShowFidCid())
cmd.AddCommand(CmdGetPayData())

cmd.AddCommand(CmdFileUploadCheck())

// this line is used by starport scaffolding # 1

return cmd
Expand Down
46 changes: 46 additions & 0 deletions x/storage/client/cli/query_file_upload_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cli

import (
"context"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/jackalLabs/canine-chain/x/storage/types"
"github.com/spf13/cobra"
)

func CmdFileUploadCheck() *cobra.Command {
cmd := &cobra.Command{
Use: "file-upload-check [address] [bytes]",
Short: "check user can upload file based on size",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)

argAddr := args[0]
argByte := args[1]

reqByte, err := strconv.ParseInt(argByte, 10, 64)
if err != nil {
return err
}

params := &types.QueryFileUploadCheck{
Address: argAddr,
Bytes: reqByte,
}

res, err := queryClient.FileUploadCheck(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
30 changes: 30 additions & 0 deletions x/storage/keeper/grpc_query_file_upload_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/jackalLabs/canine-chain/x/storage/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) FileUploadCheck(c context.Context, req *types.QueryFileUploadCheck) (*types.QueryFileUploadCheckResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)

spi, found := k.GetStoragePaymentInfo(ctx, req.Address)

if !found {
return nil, status.Error(codes.NotFound, "address not found")
}

if req.Bytes < 0 {
return nil, status.Error(codes.InvalidArgument, "bytes cannot be negative")
}

return &types.QueryFileUploadCheckResponse{Valid: (spi.SpaceAvailable - spi.SpaceUsed) < req.Bytes}, nil
}
Loading