From 34e189f7738a6f5e190db3e62de7ffecd9d8566e Mon Sep 17 00:00:00 2001 From: keithsue Date: Thu, 16 Jan 2025 20:12:26 +0800 Subject: [PATCH] add query cli for lending --- x/lending/client/cli/query.go | 171 ++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) diff --git a/x/lending/client/cli/query.go b/x/lending/client/cli/query.go index 4a38584..11d3684 100644 --- a/x/lending/client/cli/query.go +++ b/x/lending/client/cli/query.go @@ -2,12 +2,15 @@ package cli import ( "fmt" + "strconv" // "strings" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/sideprotocol/side/x/lending/types" ) @@ -23,7 +26,175 @@ func GetQueryCmd(_ string) *cobra.Command { RunE: client.ValidateCmd, } + cmd.AddCommand(CmdQueryParams()) + cmd.AddCommand(CmdQueryCollateralAddress()) + cmd.AddCommand(CmdQueryLiquidationEvent()) + cmd.AddCommand(CmdQueryCETs()) + cmd.AddCommand(CmdQueryRepaymentTx()) // this line is used by starport scaffolding # 1 return cmd } + +func CmdQueryParams() *cobra.Command { + cmd := &cobra.Command{ + Use: "params", + Short: "Query the parameters of the module", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + +func CmdQueryCollateralAddress() *cobra.Command { + cmd := &cobra.Command{ + Use: "collateral-address [btc public key] [hash of loan secret] [maturity time] [final timeout]", + Short: "Query the collateral address by the specified loan params", + Args: cobra.ExactArgs(4), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + maturityTime, err := strconv.ParseUint(args[2], 10, 64) + if err != nil { + return err + } + + finalTimeout, err := strconv.ParseUint(args[3], 10, 64) + if err != nil { + return err + } + + res, err := queryClient.CollateralAddress(cmd.Context(), &types.QueryCollateralAddressRequest{ + BorrowerPubkey: args[0], + HashOfLoanSecret: args[1], + MaturityTime: maturityTime, + FinalTimeout: finalTimeout, + }) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + +func CmdQueryLiquidationEvent() *cobra.Command { + cmd := &cobra.Command{ + Use: "liquidation-event [collateral amount] [borrowed amount]", + Short: "Query the corresponding liquidation event according to the collateral amount and borrowed amount", + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + collateralAmount, err := sdk.ParseCoinNormalized(args[0]) + if err != nil { + return err + } + + borrowedAmount, err := sdk.ParseCoinNormalized(args[1]) + if err != nil { + return err + } + + res, err := queryClient.LiquidationEvent(cmd.Context(), &types.QueryLiquidationEventRequest{ + BorrowAmount: &borrowedAmount, + CollateralAcmount: &collateralAmount, + }) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + +func CmdQueryCETs() *cobra.Command { + cmd := &cobra.Command{ + Use: "cets [loan id]", + Short: "Query CETs of the given loan", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + res, err := queryClient.LoanCETs(cmd.Context(), &types.QueryLoanCETsRequest{LoanId: args[0]}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + +func CmdQueryRepaymentTx() *cobra.Command { + cmd := &cobra.Command{ + Use: "repayment-tx [loan id]", + Short: "Query the unsigned btc repayment tx of the repaid loan", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + queryClient := types.NewQueryClient(clientCtx) + + res, err := queryClient.UnsignedPaymentTx(cmd.Context(), &types.QueryRepaymentTxRequest{LoanId: args[0]}) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +}