Skip to content

Commit

Permalink
add query cli for lending
Browse files Browse the repository at this point in the history
  • Loading branch information
keithsue committed Jan 16, 2025
1 parent 9d6d7da commit 34e189f
Showing 1 changed file with 171 additions and 0 deletions.
171 changes: 171 additions & 0 deletions x/lending/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

0 comments on commit 34e189f

Please sign in to comment.