From 069e4acf36e226a30cac396532f2189730a8e328 Mon Sep 17 00:00:00 2001 From: dheerajkd30 Date: Mon, 20 Jun 2022 19:00:46 +0530 Subject: [PATCH] update gov porposal for lend module --- x/collector/client/cli/tx.go | 9 +- x/lend/client/cli/flags.go | 93 ++++-- x/lend/client/cli/parse.go | 134 ++++++++ x/lend/client/cli/tx.go | 492 +++++++++++++++--------------- x/lend/client/proposal_handler.go | 2 +- 5 files changed, 445 insertions(+), 285 deletions(-) create mode 100644 x/lend/client/cli/parse.go diff --git a/x/collector/client/cli/tx.go b/x/collector/client/cli/tx.go index 5c794401e..fd2713067 100644 --- a/x/collector/client/cli/tx.go +++ b/x/collector/client/cli/tx.go @@ -6,21 +6,15 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov/client/cli" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/spf13/cobra" flag "github.com/spf13/pflag" "strconv" - "time" - - "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" // "github.com/cosmos/cosmos-sdk/client/flags" "github.com/comdex-official/comdex/x/collector/types" ) -var ( - DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) -) - // GetTxCmd returns the transaction commands for this module func GetTxCmd() *cobra.Command { cmd := &cobra.Command{ @@ -187,7 +181,6 @@ func NewCmdAuctionControlProposal() *cobra.Command { } func NewCreateAuctionControlParams(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) { - auctionControlParams, err := parseAuctionControlParamsFlags(fs) if err != nil { diff --git a/x/lend/client/cli/flags.go b/x/lend/client/cli/flags.go index 93e06e9cc..fc4e1b055 100644 --- a/x/lend/client/cli/flags.go +++ b/x/lend/client/cli/flags.go @@ -1,31 +1,20 @@ package cli import ( + flag "github.com/spf13/pflag" "strconv" "strings" ) const ( - flagName = "name" - flagDenom = "denom" - flagDecimal = "decimal" - flagCollateralWeight = "collateralWeight" - flagLiquidationThreshold = "liquidationThreshold" - flagBaseBorrowRate = "baseBorrowRate" - flagBaseLendRate = "baseLendRate" - flagAssetOne = "assetOne" - flagAssetTwo = "assetTwo" - flagModuleAcc = "moduleAcc" - flagbaseborrowrateasset1 = "baseborrowrateasset1" - flagbaseborrowrateasset2 = "baseborrowrateasset2" - flagbaselendrateasset1 = "baselendrateasset1" - flagbaselendrateasset2 = "baselendrateasset2" - flagIsBridgedAsset = "isBridgedAsset" + FlagNewLendPairFile = "add-lend-pair-file" + FlagAddLendPoolFile = "add-lend-pool-file" + FlagAddAssetRatesStatsFile = "add-asset-rates-stats-file" ) -func ParseStringFromString(s string, seperator string) ([]string, error) { +func ParseStringFromString(s string, separator string) ([]string, error) { var parsedStrings []string - for _, s := range strings.Split(s, seperator) { + for _, s := range strings.Split(s, separator) { s = strings.TrimSpace(s) parsedStrings = append(parsedStrings, s) @@ -33,9 +22,9 @@ func ParseStringFromString(s string, seperator string) ([]string, error) { return parsedStrings, nil } -func ParseInt64SliceFromString(s string, seperator string) ([]int64, error) { +func ParseInt64SliceFromString(s string, separator string) ([]int64, error) { var parsedInts []int64 - for _, s := range strings.Split(s, seperator) { + for _, s := range strings.Split(s, separator) { s = strings.TrimSpace(s) parsed, err := strconv.ParseInt(s, 10, 64) @@ -47,9 +36,9 @@ func ParseInt64SliceFromString(s string, seperator string) ([]int64, error) { return parsedInts, nil } -func ParseUint64SliceFromString(s string, seperator string) ([]uint64, error) { +func ParseUint64SliceFromString(s string, separator string) ([]uint64, error) { var parsedInts []uint64 - for _, s := range strings.Split(s, seperator) { + for _, s := range strings.Split(s, separator) { s = strings.TrimSpace(s) parsed, err := strconv.ParseUint(s, 10, 64) @@ -62,7 +51,6 @@ func ParseUint64SliceFromString(s string, seperator string) ([]uint64, error) { } func ParseBoolFromString(s uint64) bool { - switch s { case 1: return true @@ -70,3 +58,64 @@ func ParseBoolFromString(s uint64) bool { return false } } + +func FlagSetNewLendPairsMapping() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + + fs.String(FlagNewLendPairFile, "", "add new lend pairs json file path") + return fs +} + +func FlagSetAddLendPoolMapping() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + + fs.String(FlagAddLendPoolFile, "", "add new lend pool json file path") + return fs +} + +func FlagSetAddAssetRatesStatsMapping() *flag.FlagSet { + fs := flag.NewFlagSet("", flag.ContinueOnError) + + fs.String(FlagAddAssetRatesStatsFile, "", "add asset rates stats json file path") + return fs +} + +type addNewLendPairsInputs struct { + AssetIn string `json:"asset_in"` + AssetOut string `json:"asset_out"` + IsInterPool string `json:"is_inter_pool"` + AssetOutPoolID string `json:"asset_out_pool_id"` + LiquidationRatio string `json:"liquidation_ratio"` + Title string + Description string + Deposit string +} + +type addLendPoolInputs struct { + ModuleName string `json:"module_name"` + FirstBridgedAssetID string `json:"first_bridged_asset_id"` + SecondBridgedAssetID string `json:"second_bridged_asset_id"` + AssetID string `json:"asset_id"` + IsBridgedAsset string `json:"is_bridged_asset"` + Title string + Description string + Deposit string +} + +type addAssetRatesStatsInputs struct { + AssetID string `json:"asset_id"` + UOptimal string `json:"u_optimal"` + Base string `json:"base"` + Slope1 string `json:"slope_1"` + Slope2 string `json:"slope_2"` + StableBase string `json:"stable_base"` + StableSlope1 string `json:"stable_slope_1"` + StableSlope2 string `json:"stable_slope_2"` + LTV string `json:"ltv"` + LiquidationThreshold string `json:"liquidation_threshold"` + LiquidationPenalty string `json:"liquidation_penalty"` + ReserveFactor string `json:"reserve_factor"` + Title string + Description string + Deposit string +} diff --git a/x/lend/client/cli/parse.go b/x/lend/client/cli/parse.go new file mode 100644 index 000000000..89988d9d0 --- /dev/null +++ b/x/lend/client/cli/parse.go @@ -0,0 +1,134 @@ +package cli + +import ( + "bytes" + "encoding/json" + "fmt" + "github.com/spf13/pflag" + "io/ioutil" +) + +type XAddNewLendPairsInputs addNewLendPairsInputs +type XAddLendPoolInputs addLendPoolInputs +type XAddAssetRatesStatsInputs addAssetRatesStatsInputs + +type XAddNewLendPairsInputsExceptions struct { + XAddNewLendPairsInputs + Other *string // Other won't raise an error +} + +type XAddPoolInputsExceptions struct { + XAddLendPoolInputs + Other *string // Other won't raise an error +} +type XAddAssetRatesStatsInputsExceptions struct { + XAddAssetRatesStatsInputs + Other *string // Other won't raise an error +} + +// UnmarshalJSON should error if there are fields unexpected. +func (release *addNewLendPairsInputs) UnmarshalJSON(data []byte) error { + var addNewLendPairsParamsE XAddNewLendPairsInputsExceptions + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() // Force + + if err := dec.Decode(&addNewLendPairsParamsE); err != nil { + return err + } + + *release = addNewLendPairsInputs(addNewLendPairsParamsE.XAddNewLendPairsInputs) + return nil +} + +// UnmarshalJSON should error if there are fields unexpected. +func (release *addLendPoolInputs) UnmarshalJSON(data []byte) error { + var addPoolParamsE XAddPoolInputsExceptions + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() // Force + + if err := dec.Decode(&addPoolParamsE); err != nil { + return err + } + + *release = addLendPoolInputs(addPoolParamsE.XAddLendPoolInputs) + return nil +} + +func (release *addAssetRatesStatsInputs) UnmarshalJSON(data []byte) error { + var addAssetRatesStatsE XAddAssetRatesStatsInputsExceptions + dec := json.NewDecoder(bytes.NewReader(data)) + dec.DisallowUnknownFields() // Force + + if err := dec.Decode(&addAssetRatesStatsE); err != nil { + return err + } + + *release = addAssetRatesStatsInputs(addAssetRatesStatsE.XAddAssetRatesStatsInputs) + return nil +} + +func parseAddNewLendPairsFlags(fs *pflag.FlagSet) (*addNewLendPairsInputs, error) { + addLendPairsParams := &addNewLendPairsInputs{} + addLendPairsParamsFile, _ := fs.GetString(FlagNewLendPairFile) + + if addLendPairsParamsFile == "" { + return nil, fmt.Errorf("must pass in a add new lend pairs json using the --%s flag", FlagNewLendPairFile) + } + + contents, err := ioutil.ReadFile(addLendPairsParamsFile) + if err != nil { + return nil, err + } + + // make exception if unknown field exists + err = addLendPairsParams.UnmarshalJSON(contents) + if err != nil { + return nil, err + } + + return addLendPairsParams, nil +} + +func parseAddPoolFlags(fs *pflag.FlagSet) (*addLendPoolInputs, error) { + addPoolParams := &addLendPoolInputs{} + addPoolParamsParamsFile, _ := fs.GetString(FlagAddLendPoolFile) + + if addPoolParamsParamsFile == "" { + return nil, fmt.Errorf("must pass in a add new pool json using the --%s flag", FlagAddLendPoolFile) + } + + contents, err := ioutil.ReadFile(addPoolParamsParamsFile) + if err != nil { + return nil, err + } + + // make exception if unknown field exists + err = addPoolParams.UnmarshalJSON(contents) + if err != nil { + return nil, err + } + + return addPoolParams, nil +} + +func parseAssetRateStatsFlags(fs *pflag.FlagSet) (*addAssetRatesStatsInputs, error) { + addAssetRatesStats := &addAssetRatesStatsInputs{} + addAssetRatesStatsFile, _ := fs.GetString(FlagAddAssetRatesStatsFile) + + if addAssetRatesStatsFile == "" { + return nil, fmt.Errorf("must pass in a add asset rates stats json using the --%s flag", FlagAddAssetRatesStatsFile) + } + + contents, err := ioutil.ReadFile(addAssetRatesStatsFile) + if err != nil { + return nil, err + } + + // make exception if unknown field exists + err = addAssetRatesStats.UnmarshalJSON(contents) + if err != nil { + return nil, err + } + + return addAssetRatesStats, nil +} diff --git a/x/lend/client/cli/tx.go b/x/lend/client/cli/tx.go index 2e55f4910..54d7da485 100644 --- a/x/lend/client/cli/tx.go +++ b/x/lend/client/cli/tx.go @@ -7,23 +7,12 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/gov/client/cli" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - "strconv" - "time" - "github.com/spf13/cobra" + flag "github.com/spf13/pflag" + "strconv" - "github.com/cosmos/cosmos-sdk/client" - // "github.com/cosmos/cosmos-sdk/client/flags" "github.com/comdex-official/comdex/x/lend/types" -) - -var ( - DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds()) -) - -const ( - flagPacketTimeoutTimestamp = "packet-timeout-timestamp" - listSeparator = "," + "github.com/cosmos/cosmos-sdk/client" ) // GetTxCmd returns the transaction commands for this module @@ -360,89 +349,86 @@ func txFundModuleAccounts() *cobra.Command { func CmdAddNewLendPairsProposal() *cobra.Command { cmd := &cobra.Command{ - Use: "add-lend-pairs [asset_in] [asset_out] [is_inter_pool] [asset_out_pool_id] [liquidation_ratio]", + Use: "add-lend-pairs [flags]", Short: "Add lend asset pairs", - Args: cobra.ExactArgs(5), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) - assetIn, err := ParseUint64SliceFromString(args[0], ",") - if err != nil { - return err - } - - assetOut, err := ParseUint64SliceFromString(args[1], ",") + txf, msg, err := NewCreateNewLendPairs(clientCtx, txf, cmd.Flags()) if err != nil { return err } - isInterPool, err := ParseUint64SliceFromString(args[2], ",") - if err != nil { - return err - } + return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg) + }, + } - assetOutPoolId, err := ParseUint64SliceFromString(args[3], ",") - if err != nil { - return err - } + cmd.Flags().AddFlagSet(FlagSetNewLendPairsMapping()) + cmd.Flags().String(cli.FlagProposal, "", "Proposal file path (if this path is given, other proposal flags are ignored)") + return cmd +} - var pairs []types.Extended_Pair - for i := range assetIn { - interPool := ParseBoolFromString(isInterPool[i]) - pairs = append(pairs, types.Extended_Pair{ - AssetIn: assetIn[i], - AssetOut: assetOut[i], - IsInterPool: interPool, - AssetOutPoolId: assetOutPoolId[i], - }) - } +func NewCreateNewLendPairs(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) { + newLendPairs, err := parseAddNewLendPairsFlags(fs) - title, err := cmd.Flags().GetString(cli.FlagTitle) - if err != nil { - return err - } + if err != nil { + return txf, nil, fmt.Errorf("failed to parse add lend pairs : %w", err) + } - description, err := cmd.Flags().GetString(cli.FlagDescription) - if err != nil { - return err - } + assetIn, err := ParseUint64SliceFromString(newLendPairs.AssetIn, ",") + if err != nil { + return txf, nil, err + } - from := clientCtx.GetFromAddress() + assetOut, err := ParseUint64SliceFromString(newLendPairs.AssetOut, ",") + if err != nil { + return txf, nil, err + } - depositStr, err := cmd.Flags().GetString(cli.FlagDeposit) - if err != nil { - return err - } - deposit, err := sdk.ParseCoinsNormalized(depositStr) - if err != nil { - return err - } + isInterPool, err := ParseUint64SliceFromString(newLendPairs.IsInterPool, ",") + if err != nil { + return txf, nil, err + } - content := types.NewAddLendPairsProposal(title, description, pairs) + assetOutPoolId, err := ParseUint64SliceFromString(newLendPairs.AssetOutPoolID, ",") + if err != nil { + return txf, nil, err + } - msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) - if err != nil { - return err - } + var pairs []types.Extended_Pair + for i := range assetIn { + interPool := ParseBoolFromString(isInterPool[i]) + pairs = append(pairs, types.Extended_Pair{ + AssetIn: assetIn[i], + AssetOut: assetOut[i], + IsInterPool: interPool, + AssetOutPoolId: assetOutPoolId[i], + }) + } - if err = msg.ValidateBasic(); err != nil { - return err - } + from := clientCtx.GetFromAddress() - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, + deposit, err := sdk.ParseCoinsNormalized(newLendPairs.Deposit) + if err != nil { + return txf, nil, err } - cmd.Flags().String(cli.FlagTitle, "", "title of proposal") - cmd.Flags().String(cli.FlagDescription, "", "description of proposal") - cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal") - _ = cmd.MarkFlagRequired(cli.FlagTitle) - _ = cmd.MarkFlagRequired(cli.FlagDescription) + content := types.NewAddLendPairsProposal(newLendPairs.Title, newLendPairs.Description, pairs) - return cmd + msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) + if err != nil { + return txf, nil, err + } + + if err = msg.ValidateBasic(); err != nil { + return txf, nil, err + } + return txf, msg, nil } func CmdUpdateLendPairProposal() *cobra.Command { @@ -513,96 +499,95 @@ func CmdUpdateLendPairProposal() *cobra.Command { func CmdAddPoolProposal() *cobra.Command { cmd := &cobra.Command{ - Use: "add-lend-pool [module_name] [first_bridged_asset_id] [second_bridged_asset_id] [asset_id] [is_bridged_asset] ", + Use: "add-lend-pool [flag] ", Short: "Add lend pool ", - Args: cobra.ExactArgs(5), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) - moduleName := args[0] - - firstBridgedAssetId, err := strconv.ParseUint(args[1], 10, 64) + txf, msg, err := NewCreateLendPool(clientCtx, txf, cmd.Flags()) if err != nil { return err } - secondBridgedAssetId, err := strconv.ParseUint(args[2], 10, 64) - if err != nil { - return err - } + return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg) + }, + } - assetId, err := ParseUint64SliceFromString(args[3], ",") - if err != nil { - return err - } + cmd.Flags().AddFlagSet(FlagSetAddLendPoolMapping()) + cmd.Flags().String(cli.FlagProposal, "", "Proposal file path (if this path is given, other proposal flags are ignored)") + return cmd +} - isBridgedAsset, err := ParseUint64SliceFromString(args[4], ",") - if err != nil { - return err - } - var pool types.Pool - var assetData []types.AssetDataPoolMapping +func NewCreateLendPool(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) { + newLendPool, err := parseAddPoolFlags(fs) - for i := range assetId { - bridged := ParseBoolFromString(isBridgedAsset[i]) - assetData = append(assetData, types.AssetDataPoolMapping{ - AssetId: assetId[i], - IsBridged: bridged, - }) - } - pool = types.Pool{ - ModuleName: moduleName, - FirstBridgedAssetId: firstBridgedAssetId, - SecondBridgedAssetId: secondBridgedAssetId, - AssetData: assetData, - } + if err != nil { + return txf, nil, fmt.Errorf("failed to parse add lend pool : %w", err) + } - title, err := cmd.Flags().GetString(cli.FlagTitle) - if err != nil { - return err - } + moduleName := newLendPool.ModuleName - description, err := cmd.Flags().GetString(cli.FlagDescription) - if err != nil { - return err - } + firstBridgedAssetId, err := strconv.ParseUint(newLendPool.FirstBridgedAssetID, 10, 64) + if err != nil { + return txf, nil, err + } - from := clientCtx.GetFromAddress() + secondBridgedAssetId, err := strconv.ParseUint(newLendPool.SecondBridgedAssetID, 10, 64) + if err != nil { + return txf, nil, err + } - depositStr, err := cmd.Flags().GetString(cli.FlagDeposit) - if err != nil { - return err - } - deposit, err := sdk.ParseCoinsNormalized(depositStr) - if err != nil { - return err - } + assetId, err := ParseUint64SliceFromString(newLendPool.AssetID, ",") + if err != nil { + return txf, nil, err + } - content := types.NewAddPoolProposal(title, description, pool) + isBridgedAsset, err := ParseUint64SliceFromString(newLendPool.IsBridgedAsset, ",") + if err != nil { + return txf, nil, err + } + var pool types.Pool + var assetData []types.AssetDataPoolMapping + + for i := range assetId { + bridged := ParseBoolFromString(isBridgedAsset[i]) + assetData = append(assetData, types.AssetDataPoolMapping{ + AssetId: assetId[i], + IsBridged: bridged, + }) + } + pool = types.Pool{ + ModuleName: moduleName, + FirstBridgedAssetId: firstBridgedAssetId, + SecondBridgedAssetId: secondBridgedAssetId, + AssetData: assetData, + } - msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) - if err != nil { - return err - } + from := clientCtx.GetFromAddress() - if err = msg.ValidateBasic(); err != nil { - return err - } + deposit, err := sdk.ParseCoinsNormalized(newLendPool.Deposit) + if err != nil { + return txf, nil, err + } - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, + content := types.NewAddPoolProposal(newLendPool.Title, newLendPool.Description, pool) + + msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) + if err != nil { + return txf, nil, err } - cmd.Flags().String(cli.FlagTitle, "", "title of proposal") - cmd.Flags().String(cli.FlagDescription, "", "description of proposal") - cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal") - _ = cmd.MarkFlagRequired(cli.FlagTitle) - _ = cmd.MarkFlagRequired(cli.FlagDescription) + if err = msg.ValidateBasic(); err != nil { + return txf, nil, err + } - return cmd + return txf, msg, nil } func CmdAddAssetToPairProposal() *cobra.Command { @@ -680,145 +665,144 @@ func CmdAddAssetToPairProposal() *cobra.Command { return cmd } -func CmdAddWNewAssetRatesStatsProposal() *cobra.Command { +func CmdAddNewAssetRatesStatsProposal() *cobra.Command { cmd := &cobra.Command{ - Use: "add-asset-rates-stats [asset_id] [u_optimal] [base] [slope1] [slope2] [stable_base] [stable_slope1] [stable_slope2] [ltv] [liquidation_threshold] [liquidation_penalty] [reserve_factor]", + Use: "add-asset-rates-stats [flags]", Short: "Add lend asset pairs", - Args: cobra.ExactArgs(12), + Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) - assetId, err := ParseUint64SliceFromString(args[0], ",") + txf, msg, err := NewCreateAssetRatesStats(clientCtx, txf, cmd.Flags()) if err != nil { return err } - uOptimal, err := ParseStringFromString(args[1], ",") - if err != nil { - return err - } - base, err := ParseStringFromString(args[2], ",") - if err != nil { - return err - } - slope1, err := ParseStringFromString(args[3], ",") - if err != nil { - return err - } - slope2, err := ParseStringFromString(args[4], ",") - if err != nil { - return err - } - enableStableBorrow, err := ParseUint64SliceFromString(args[2], ",") - if err != nil { - return err - } - stableBase, err := ParseStringFromString(args[5], ",") - if err != nil { - return err - } - stableSlope1, err := ParseStringFromString(args[6], ",") - if err != nil { - return err - } - stableSlope2, err := ParseStringFromString(args[7], ",") - if err != nil { - return err - } - ltv, err := ParseStringFromString(args[8], ",") - if err != nil { - return err - } - liquidationThreshold, err := ParseStringFromString(args[9], ",") - if err != nil { - return err - } - liquidationPenalty, err := ParseStringFromString(args[10], ",") - if err != nil { - return err - } - reserveFactor, err := ParseStringFromString(args[11], ",") - if err != nil { - return err - } + return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg) + }, + } - var assetRatesStats []types.AssetRatesStats - for i := range assetId { - newUOptimal, _ := sdk.NewDecFromStr(uOptimal[i]) - newBase, _ := sdk.NewDecFromStr(base[i]) - newSlope1, _ := sdk.NewDecFromStr(slope1[i]) - newSlope2, _ := sdk.NewDecFromStr(slope2[i]) - newEnableStableBorrow := ParseBoolFromString(enableStableBorrow[i]) - newStableBase, _ := sdk.NewDecFromStr(stableBase[i]) - newStableSlope1, _ := sdk.NewDecFromStr(stableSlope1[i]) - newStableSlope2, _ := sdk.NewDecFromStr(stableSlope2[i]) - newLTV, _ := sdk.NewDecFromStr(ltv[i]) - newLiquidationThreshold, _ := sdk.NewDecFromStr(liquidationThreshold[i]) - newLiquidationPenalty, _ := sdk.NewDecFromStr(liquidationPenalty[i]) - newReserveFactor, _ := sdk.NewDecFromStr(reserveFactor[i]) + cmd.Flags().AddFlagSet(FlagSetAddAssetRatesStatsMapping()) + cmd.Flags().String(cli.FlagProposal, "", "Proposal file path (if this path is given, other proposal flags are ignored)") + return cmd +} - assetRatesStats = append(assetRatesStats, types.AssetRatesStats{ - AssetId: assetId[i], - UOptimal: newUOptimal, - Base: newBase, - Slope1: newSlope1, - Slope2: newSlope2, - EnableStableBorrow: newEnableStableBorrow, - StableBase: newStableBase, - StableSlope1: newStableSlope1, - StableSlope2: newStableSlope2, - Ltv: newLTV, - LiquidationThreshold: newLiquidationThreshold, - LiquidationPenalty: newLiquidationPenalty, - ReserveFactor: newReserveFactor, - }, - ) - } +func NewCreateAssetRatesStats(clientCtx client.Context, txf tx.Factory, fs *flag.FlagSet) (tx.Factory, sdk.Msg, error) { + assetRatesStatsInput, err := parseAssetRateStatsFlags(fs) - title, err := cmd.Flags().GetString(cli.FlagTitle) - if err != nil { - return err - } + if err != nil { + return txf, nil, fmt.Errorf("failed to parse asset rates stats : %w", err) + } - description, err := cmd.Flags().GetString(cli.FlagDescription) - if err != nil { - return err - } + assetId, err := ParseUint64SliceFromString(assetRatesStatsInput.AssetID, ",") + if err != nil { + return txf, nil, err + } - from := clientCtx.GetFromAddress() + uOptimal, err := ParseStringFromString(assetRatesStatsInput.UOptimal, ",") + if err != nil { + return txf, nil, err + } + base, err := ParseStringFromString(assetRatesStatsInput.Base, ",") + if err != nil { + return txf, nil, err + } + slope1, err := ParseStringFromString(assetRatesStatsInput.Slope1, ",") + if err != nil { + return txf, nil, err + } + slope2, err := ParseStringFromString(assetRatesStatsInput.Slope2, ",") + if err != nil { + return txf, nil, err + } + enableStableBorrow, err := ParseUint64SliceFromString(assetRatesStatsInput.Base, ",") + if err != nil { + return txf, nil, err + } + stableBase, err := ParseStringFromString(assetRatesStatsInput.StableBase, ",") + if err != nil { + return txf, nil, err + } + stableSlope1, err := ParseStringFromString(assetRatesStatsInput.StableSlope1, ",") + if err != nil { + return txf, nil, err + } + stableSlope2, err := ParseStringFromString(assetRatesStatsInput.StableSlope2, ",") + if err != nil { + return txf, nil, err + } + ltv, err := ParseStringFromString(assetRatesStatsInput.LTV, ",") + if err != nil { + return txf, nil, err + } + liquidationThreshold, err := ParseStringFromString(assetRatesStatsInput.LiquidationThreshold, ",") + if err != nil { + return txf, nil, err + } + liquidationPenalty, err := ParseStringFromString(assetRatesStatsInput.LiquidationPenalty, ",") + if err != nil { + return txf, nil, err + } + reserveFactor, err := ParseStringFromString(assetRatesStatsInput.ReserveFactor, ",") + if err != nil { + return txf, nil, err + } - depositStr, err := cmd.Flags().GetString(cli.FlagDeposit) - if err != nil { - return err - } - deposit, err := sdk.ParseCoinsNormalized(depositStr) - if err != nil { - return err - } + var assetRatesStats []types.AssetRatesStats + for i := range assetId { + newUOptimal, _ := sdk.NewDecFromStr(uOptimal[i]) + newBase, _ := sdk.NewDecFromStr(base[i]) + newSlope1, _ := sdk.NewDecFromStr(slope1[i]) + newSlope2, _ := sdk.NewDecFromStr(slope2[i]) + newEnableStableBorrow := ParseBoolFromString(enableStableBorrow[i]) + newStableBase, _ := sdk.NewDecFromStr(stableBase[i]) + newStableSlope1, _ := sdk.NewDecFromStr(stableSlope1[i]) + newStableSlope2, _ := sdk.NewDecFromStr(stableSlope2[i]) + newLTV, _ := sdk.NewDecFromStr(ltv[i]) + newLiquidationThreshold, _ := sdk.NewDecFromStr(liquidationThreshold[i]) + newLiquidationPenalty, _ := sdk.NewDecFromStr(liquidationPenalty[i]) + newReserveFactor, _ := sdk.NewDecFromStr(reserveFactor[i]) + + assetRatesStats = append(assetRatesStats, types.AssetRatesStats{ + AssetId: assetId[i], + UOptimal: newUOptimal, + Base: newBase, + Slope1: newSlope1, + Slope2: newSlope2, + EnableStableBorrow: newEnableStableBorrow, + StableBase: newStableBase, + StableSlope1: newStableSlope1, + StableSlope2: newStableSlope2, + Ltv: newLTV, + LiquidationThreshold: newLiquidationThreshold, + LiquidationPenalty: newLiquidationPenalty, + ReserveFactor: newReserveFactor, + }, + ) + } - content := types.NewAddAssetRatesStats(title, description, assetRatesStats) + from := clientCtx.GetFromAddress() - msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) - if err != nil { - return err - } + deposit, err := sdk.ParseCoinsNormalized(assetRatesStatsInput.Deposit) + if err != nil { + return txf, nil, err + } - if err = msg.ValidateBasic(); err != nil { - return err - } + content := types.NewAddAssetRatesStats(assetRatesStatsInput.Title, assetRatesStatsInput.Description, assetRatesStats) - return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) - }, + msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) + if err != nil { + return txf, nil, err } - cmd.Flags().String(cli.FlagTitle, "", "title of proposal") - cmd.Flags().String(cli.FlagDescription, "", "description of proposal") - cmd.Flags().String(cli.FlagDeposit, "", "deposit of proposal") - _ = cmd.MarkFlagRequired(cli.FlagTitle) - _ = cmd.MarkFlagRequired(cli.FlagDescription) + if err = msg.ValidateBasic(); err != nil { + return txf, nil, err + } - return cmd + return txf, msg, nil } diff --git a/x/lend/client/proposal_handler.go b/x/lend/client/proposal_handler.go index e2098b7c2..449c35602 100644 --- a/x/lend/client/proposal_handler.go +++ b/x/lend/client/proposal_handler.go @@ -11,5 +11,5 @@ var ( UpdateLendPairsHandler = govclient.NewProposalHandler(cli.CmdUpdateLendPairProposal, rest.UpdateNewPairsProposalRESTHandler) AddPoolHandler = govclient.NewProposalHandler(cli.CmdAddPoolProposal, rest.AddPoolProposalRESTHandler) AddAssetToPairHandler = govclient.NewProposalHandler(cli.CmdAddAssetToPairProposal, rest.AddAssetToPairProposalRESTHandler) - AddAssetRatesStatsHandler = govclient.NewProposalHandler(cli.CmdAddWNewAssetRatesStatsProposal, rest.AddWNewAssetRatesStatsProposalRESTHandler) + AddAssetRatesStatsHandler = govclient.NewProposalHandler(cli.CmdAddNewAssetRatesStatsProposal, rest.AddWNewAssetRatesStatsProposalRESTHandler) )