diff --git a/x/subscription/client/cmd/query.go b/x/subscription/client/cmd/query.go index 171a697..1c1232b 100644 --- a/x/subscription/client/cmd/query.go +++ b/x/subscription/client/cmd/query.go @@ -9,7 +9,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/olekukonko/tablewriter" - hubtypes "github.com/sentinel-official/hub/types" subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types" "github.com/spf13/cobra" @@ -20,20 +19,20 @@ import ( var ( subscriptionHeader = []string{ "ID", - "Owner", - "Plan", - "Expiry", - "Denom", + "Address", + "Expiry at", + "Status", "Node", - "Price", + "Gigabytes", + "Hours", "Deposit", - "Free", - "Status", + "Plan", + "Denom", } - quotaHeader = []string{ + allocationHeader = []string{ "Address", - "Allocated", - "Consumed", + "Granted bytes", + "Utilised bytes", } ) @@ -65,8 +64,13 @@ func QuerySubscription() *cobra.Command { return err } + var subscription subscriptiontypes.Subscription + if err = ctx.InterfaceRegistry.UnpackAny(result.Subscription, &subscription); err != nil { + return err + } + var ( - item = types.NewSubscriptionFromRaw(&result.Subscription) + item = types.NewSubscriptionFromRaw(subscription) table = tablewriter.NewWriter(cmd.OutOrStdout()) ) @@ -74,15 +78,15 @@ func QuerySubscription() *cobra.Command { table.Append( []string{ fmt.Sprintf("%d", item.ID), - item.Owner, - fmt.Sprintf("%d", item.Plan), - item.Expiry.String(), - item.Denom, - item.Node, - item.Price.Raw().String(), - item.Deposit.Raw().String(), - netutil.ToReadable(item.Free, 2), + item.Address, + item.ExpiryAt.String(), item.Status, + item.NodeAddress, + fmt.Sprintf("%d", item.Gigabytes), + fmt.Sprintf("%d", item.Hours), + item.Deposit, + fmt.Sprintf("%d", item.PlanID), + item.Denom, }, ) @@ -111,11 +115,6 @@ func QuerySubscriptions() *cobra.Command { return err } - status, err := cmd.Flags().GetString(flagStatus) - if err != nil { - return err - } - pagination, err := client.ReadPageRequest(cmd.Flags()) if err != nil { return err @@ -132,11 +131,10 @@ func QuerySubscriptions() *cobra.Command { return err } - result, err := qsc.QuerySubscriptionsForAddress( + result, err := qsc.QuerySubscriptionsForAccount( context.Background(), - subscriptiontypes.NewQuerySubscriptionsForAddressRequest( + subscriptiontypes.NewQuerySubscriptionsForAccountRequest( address, - hubtypes.StatusFromString(status), pagination, ), ) @@ -144,7 +142,17 @@ func QuerySubscriptions() *cobra.Command { return err } - items = append(items, types.NewSubscriptionsFromRaw(result.Subscriptions)...) + var subscriptions []subscriptiontypes.Subscription + for _, item := range result.Subscriptions { + var subscription subscriptiontypes.Subscription + if err = ctx.InterfaceRegistry.UnpackAny(item, &subscription); err != nil { + return err + } + + subscriptions = append(subscriptions, subscription) + } + + items = append(items, types.NewSubscriptionsFromRaw(subscriptions)...) } else { result, err := qsc.QuerySubscriptions( context.Background(), @@ -154,7 +162,17 @@ func QuerySubscriptions() *cobra.Command { return err } - items = append(items, types.NewSubscriptionsFromRaw(result.Subscriptions)...) + var subscriptions []subscriptiontypes.Subscription + for _, item := range result.Subscriptions { + var subscription subscriptiontypes.Subscription + if err = ctx.InterfaceRegistry.UnpackAny(item, &subscription); err != nil { + return err + } + + subscriptions = append(subscriptions, subscription) + } + + items = append(items, types.NewSubscriptionsFromRaw(subscriptions)...) } table := tablewriter.NewWriter(cmd.OutOrStdout()) @@ -164,15 +182,15 @@ func QuerySubscriptions() *cobra.Command { table.Append( []string{ fmt.Sprintf("%d", items[i].ID), - items[i].Owner, - fmt.Sprintf("%d", items[i].Plan), - items[i].Expiry.String(), - items[i].Denom, - items[i].Node, - items[i].Price.Raw().String(), - items[i].Deposit.Raw().String(), - netutil.ToReadable(items[i].Free, 2), + items[i].Address, + items[i].ExpiryAt.String(), items[i].Status, + items[i].NodeAddress, + fmt.Sprintf("%d", items[i].Gigabytes), + fmt.Sprintf("%d", items[i].Hours), + items[i].Deposit, + fmt.Sprintf("%d", items[i].PlanID), + items[i].Denom, }, ) } @@ -186,15 +204,14 @@ func QuerySubscriptions() *cobra.Command { flags.AddPaginationFlagsToCmd(cmd, "subscriptions") cmd.Flags().String(flagAddress, "", "filter with account address") - cmd.Flags().String(flagStatus, "Active", "filter with status (Active|Inactive)") return cmd } -func QueryQuota() *cobra.Command { +func QueryAllocation() *cobra.Command { cmd := &cobra.Command{ - Use: "quota [subscription] [address]", - Short: "Query a quota", + Use: "allocation [subscription] [address]", + Short: "Query a allocation", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { ctx, err := client.GetClientQueryContext(cmd) @@ -216,9 +233,9 @@ func QueryQuota() *cobra.Command { qsc = subscriptiontypes.NewQueryServiceClient(ctx) ) - result, err := qsc.QueryQuota( + result, err := qsc.QueryAllocation( context.Background(), - subscriptiontypes.NewQueryQuotaRequest( + subscriptiontypes.NewQueryAllocationRequest( id, address, ), @@ -228,16 +245,16 @@ func QueryQuota() *cobra.Command { } var ( - item = types.NewQuotaFromRaw(&result.Quota) + item = types.NewAllocationFromRaw(&result.Allocation) table = tablewriter.NewWriter(cmd.OutOrStdout()) ) - table.SetHeader(quotaHeader) + table.SetHeader(allocationHeader) table.Append( []string{ item.Address, - netutil.ToReadable(item.Allocated, 2), - netutil.ToReadable(item.Consumed, 2), + netutil.ToReadable(item.GrantedBytes, 2), + netutil.ToReadable(item.UtilisedBytes, 2), }, ) @@ -251,10 +268,10 @@ func QueryQuota() *cobra.Command { return cmd } -func QueryQuotas() *cobra.Command { +func QueryAllocations() *cobra.Command { cmd := &cobra.Command{ - Use: "quotas [subscription]", - Short: "Query quotas of a subscription", + Use: "allocations [subscription]", + Short: "Query allocations of a subscription", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) (err error) { ctx, err := client.GetClientQueryContext(cmd) @@ -276,9 +293,9 @@ func QueryQuotas() *cobra.Command { qsc = subscriptiontypes.NewQueryServiceClient(ctx) ) - result, err := qsc.QueryQuotas( + result, err := qsc.QueryAllocations( context.Background(), - subscriptiontypes.NewQueryQuotasRequest( + subscriptiontypes.NewQueryAllocationsRequest( id, pagination, ), @@ -288,17 +305,17 @@ func QueryQuotas() *cobra.Command { } var ( - items = types.NewQuotasFromRaw(result.Quotas) + items = types.NewAllocationsFromRaw(result.Allocations) table = tablewriter.NewWriter(cmd.OutOrStdout()) ) - table.SetHeader(quotaHeader) + table.SetHeader(allocationHeader) for i := 0; i < len(items); i++ { table.Append( []string{ items[i].Address, - netutil.ToReadable(items[i].Allocated, 2), - netutil.ToReadable(items[i].Consumed, 2), + netutil.ToReadable(items[i].GrantedBytes, 2), + netutil.ToReadable(items[i].UtilisedBytes, 2), }, ) } @@ -309,7 +326,7 @@ func QueryQuotas() *cobra.Command { } flags.AddQueryFlagsToCmd(cmd) - flags.AddPaginationFlagsToCmd(cmd, "quotas") + flags.AddPaginationFlagsToCmd(cmd, "allocations") return cmd } diff --git a/x/subscription/client/cmd/tx.go b/x/subscription/client/cmd/tx.go index 4811283..1e5274b 100644 --- a/x/subscription/client/cmd/tx.go +++ b/x/subscription/client/cmd/tx.go @@ -9,7 +9,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" - hubtypes "github.com/sentinel-official/hub/types" "github.com/sentinel-official/hub/x/subscription/types" ) @@ -20,138 +19,17 @@ func GetTxCommand() *cobra.Command { } cmd.AddCommand( - txSubscribeToNode(), - txSubscribeToPlan(), - txAddQuota(), - txUpdateQuota(), + txAllocate(), txCancel(), ) return cmd } -func txSubscribeToNode() *cobra.Command { +func txAllocate() *cobra.Command { cmd := &cobra.Command{ - Use: "subscribe-to-node [node] [deposit]", - Short: "Subscribe to a node", - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - ctx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - address, err := hubtypes.NodeAddressFromBech32(args[0]) - if err != nil { - return err - } - - deposit, err := sdk.ParseCoinNormalized(args[1]) - if err != nil { - return err - } - - msg := types.NewMsgSubscribeToNodeRequest( - ctx.FromAddress, - address, - deposit, - ) - if err := msg.ValidateBasic(); err != nil { - return err - } - - return tx.GenerateOrBroadcastTxCLI(ctx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func txSubscribeToPlan() *cobra.Command { - cmd := &cobra.Command{ - Use: "subscribe-to-plan [plan] [denom]", - Short: "Subscribe to a plan", - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - ctx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - id, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return err - } - - msg := types.NewMsgSubscribeToPlanRequest( - ctx.FromAddress, - id, - args[1], - ) - if err := msg.ValidateBasic(); err != nil { - return err - } - - return tx.GenerateOrBroadcastTxCLI(ctx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func txAddQuota() *cobra.Command { - cmd := &cobra.Command{ - Use: "quota-add [id] [address] [bytes]", - Short: "Add a quota for subscription", - Args: cobra.ExactArgs(3), - RunE: func(cmd *cobra.Command, args []string) error { - ctx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - - id, err := strconv.ParseUint(args[0], 10, 64) - if err != nil { - return err - } - - address, err := sdk.AccAddressFromBech32(args[1]) - if err != nil { - return err - } - - bytes, err := strconv.ParseInt(args[2], 10, 64) - if err != nil { - return err - } - - msg := types.NewMsgAddQuotaRequest( - ctx.FromAddress, - id, - address, - sdk.NewInt(bytes), - ) - if err := msg.ValidateBasic(); err != nil { - return err - } - - return tx.GenerateOrBroadcastTxCLI(ctx, cmd.Flags(), msg) - }, - } - - flags.AddTxFlagsToCmd(cmd) - - return cmd -} - -func txUpdateQuota() *cobra.Command { - cmd := &cobra.Command{ - Use: "quota-update [id] [address] [bytes]", - Short: "Update a quota for subscription", + Use: "allocate [id] [address] [bytes]", + Short: "Add an allocation for subscription", Args: cobra.ExactArgs(3), RunE: func(cmd *cobra.Command, args []string) error { ctx, err := client.GetClientTxContext(cmd) @@ -174,7 +52,7 @@ func txUpdateQuota() *cobra.Command { return err } - msg := types.NewMsgUpdateQuotaRequest( + msg := types.NewMsgAllocateRequest( ctx.FromAddress, id, address, diff --git a/x/subscription/types/allocation.go b/x/subscription/types/allocation.go new file mode 100644 index 0000000..88b9b8c --- /dev/null +++ b/x/subscription/types/allocation.go @@ -0,0 +1,30 @@ +package types + +import ( + subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types" +) + +type Allocation struct { + Address string `json:"address"` + GrantedBytes int64 `json:"granted_bytes"` + UtilisedBytes int64 `json:"utilised_bytes"` +} + +func NewAllocationFromRaw(v *subscriptiontypes.Allocation) Allocation { + return Allocation{ + Address: v.Address, + GrantedBytes: v.GrantedBytes.Int64(), + UtilisedBytes: v.UtilisedBytes.Int64(), + } +} + +type Allocations []Allocation + +func NewAllocationsFromRaw(v subscriptiontypes.Allocations) Allocations { + items := make(Allocations, 0, len(v)) + for i := 0; i < len(v); i++ { + items = append(items, NewAllocationFromRaw(&v[i])) + } + + return items +} diff --git a/x/subscription/types/quota.go b/x/subscription/types/quota.go deleted file mode 100644 index 04f6ee0..0000000 --- a/x/subscription/types/quota.go +++ /dev/null @@ -1,30 +0,0 @@ -package types - -import ( - subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types" -) - -type Quota struct { - Address string `json:"address"` - Consumed int64 `json:"consumed"` - Allocated int64 `json:"allocated"` -} - -func NewQuotaFromRaw(v *subscriptiontypes.Quota) Quota { - return Quota{ - Address: v.Address, - Consumed: v.Consumed.Int64(), - Allocated: v.Allocated.Int64(), - } -} - -type Quotas []Quota - -func NewQuotasFromRaw(v subscriptiontypes.Quotas) Quotas { - items := make(Quotas, 0, len(v)) - for i := 0; i < len(v); i++ { - items = append(items, NewQuotaFromRaw(&v[i])) - } - - return items -} diff --git a/x/subscription/types/subscription.go b/x/subscription/types/subscription.go index 27f109d..04b4e06 100644 --- a/x/subscription/types/subscription.go +++ b/x/subscription/types/subscription.go @@ -4,38 +4,45 @@ import ( "time" subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types" - - clienttypes "github.com/sentinel-official/cli-client/types" ) type Subscription struct { - ID uint64 `json:"id"` - Owner string `json:"owner"` - Plan uint64 `json:"plan"` - Expiry time.Time `json:"expiry"` - Denom string `json:"denom"` - Node string `json:"node"` - Price clienttypes.Coin `json:"price"` - Deposit clienttypes.Coin `json:"deposit"` - Free int64 `json:"free"` - Status string `json:"status"` - StatusAt time.Time `json:"status_at"` + ID uint64 `json:"id"` + Address string `json:"address"` + ExpiryAt time.Time `json:"expiry_at"` + Status string `json:"status"` + StatusAt time.Time `json:"status_at"` + + NodeAddress string `json:"node_address"` + Gigabytes int64 `json:"gigabytes"` + Hours int64 `json:"hours"` + Deposit string `json:"deposit"` + + PlanID uint64 `json:"plan_id"` + Denom string `json:"denom"` } -func NewSubscriptionFromRaw(v *subscriptiontypes.Subscription) Subscription { - return Subscription{ - ID: v.Id, - Owner: v.Owner, - Plan: v.Plan, - Expiry: v.Expiry, - Denom: v.Denom, - Node: v.Node, - Price: clienttypes.NewCoinFromRaw(&v.Price), - Deposit: clienttypes.NewCoinFromRaw(&v.Deposit), - Free: v.Free.Int64(), - Status: v.Status.String(), - StatusAt: v.StatusAt, +func NewSubscriptionFromRaw(v subscriptiontypes.Subscription) Subscription { + s := Subscription{ + ID: v.GetID(), + Address: v.GetAddress().String(), + ExpiryAt: v.GetExpiryAt(), + Status: v.GetStatus().String(), + StatusAt: v.GetStatusAt(), + } + + if v.Type() == subscriptiontypes.TypeNode { + s.NodeAddress = v.(*subscriptiontypes.NodeSubscription).NodeAddress + s.Gigabytes = v.(*subscriptiontypes.NodeSubscription).Gigabytes + s.Hours = v.(*subscriptiontypes.NodeSubscription).Hours + s.Deposit = v.(*subscriptiontypes.NodeSubscription).Deposit.String() } + if v.Type() == subscriptiontypes.TypePlan { + s.PlanID = v.(*subscriptiontypes.PlanSubscription).PlanID + s.Denom = v.(*subscriptiontypes.PlanSubscription).Denom + } + + return s } type Subscriptions []Subscription @@ -43,7 +50,7 @@ type Subscriptions []Subscription func NewSubscriptionsFromRaw(v subscriptiontypes.Subscriptions) Subscriptions { items := make(Subscriptions, 0, len(v)) for i := 0; i < len(v); i++ { - items = append(items, NewSubscriptionFromRaw(&v[i])) + items = append(items, NewSubscriptionFromRaw(v[i])) } return items