laconicd/x/registry/client/cli/tx.go
Prathamesh Musale ba7a127d7f Add bond-record association commands to registry module (#8)
Reviewed-on: deep-stack/laconic2d#8
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-02-26 12:56:11 +00:00

83 lines
1.8 KiB
Go

package cli
import (
"os"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"gopkg.in/yaml.v3"
"github.com/spf13/cobra"
registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry"
)
// GetTxCmd returns transaction commands for this module.
func GetTxCmd() *cobra.Command {
registryTxCmd := &cobra.Command{
Use: registrytypes.ModuleName,
Short: "registry transaction subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
registryTxCmd.AddCommand(
GetCmdSetRecord(),
)
return registryTxCmd
}
// GetCmdSetRecord is the CLI command for creating/updating a record.
func GetCmdSetRecord() *cobra.Command {
cmd := &cobra.Command{
Use: "set [payload-file-path] [bond-id]",
Short: "Set record",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
payloadType, err := GetPayloadFromFile(args[0])
if err != nil {
return err
}
payload := payloadType.ToPayload()
msg := registrytypes.NewMsgSetRecord(payload, args[1], clientCtx.GetFromAddress())
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
// GetPayloadFromFile loads payload object from YAML file.
func GetPayloadFromFile(filePath string) (*registrytypes.ReadablePayload, error) {
var payload registrytypes.ReadablePayload
data, err := os.ReadFile(filePath) // #nosec G304
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &payload)
if err != nil {
return nil, err
}
return &payload, nil
}