package cli
import (
"encoding/hex"
"fmt"
"os"
"time"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cerc-io/laconicd/x/auction/types"
wnsUtils "github.com/cerc-io/laconicd/utils"
)
// GetTxCmd returns transaction commands for this module.
func GetTxCmd() *cobra.Command {
auctionTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Auction transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
auctionTxCmd.AddCommand(
GetCmdCreateAuction(),
GetCmdCommitBid(),
GetCmdRevealBid(),
return auctionTxCmd
func GetCmdCreateAuction() *cobra.Command {
cmd := &cobra.Command{
Use: "create [commits-duration] [reveals-duration] [commit-fee] [reveal-fee] [minimum-bid]",
Short: "Create auction.",
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
commitsDuration, err := time.ParseDuration(args[0])
revealsDuration, err := time.ParseDuration(args[1])
commitFee, err := sdk.ParseCoinNormalized(args[2])
revealFee, err := sdk.ParseCoinNormalized(args[3])
minimumBid, err := sdk.ParseCoinNormalized(args[4])
params := types.Params{
CommitsDuration: commitsDuration,
RevealsDuration: revealsDuration,
CommitFee: commitFee,
RevealFee: revealFee,
MinimumBid: minimumBid,
msg := types.NewMsgCreateAuction(params, clientCtx.GetFromAddress())
err = msg.ValidateBasic()
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
flags.AddTxFlagsToCmd(cmd)
return cmd
// GetCmdCommitBid is the CLI command for committing a bid.
func GetCmdCommitBid() *cobra.Command {
Use: "commit-bid [auction-id] [bid-amount]",
Short: "Commit sealed bid.",
Args: cobra.ExactArgs(2),
bidAmount, err := sdk.ParseCoinNormalized(args[1])
mnemonic, err := wnsUtils.GenerateMnemonic()
chainID := viper.GetString("chain-id")
auctionID := args[0]
reveal := map[string]interface{}{
"chainId": chainID,
"auctionId": auctionID,
"bidderAddress": clientCtx.GetFromAddress().String(),
"bidAmount": bidAmount.String(),
"noise": mnemonic,
commitHash, content, err := wnsUtils.GenerateHash(reveal)
// Save reveal file.
err = os.WriteFile(fmt.Sprintf("%s-%s.json", clientCtx.GetFromName(), commitHash), content, 0o600)
msg := types.NewMsgCommitBid(auctionID, commitHash, clientCtx.GetFromAddress())
// GetCmdRevealBid is the CLI command for revealing a bid.
func GetCmdRevealBid() *cobra.Command {
Use: "reveal-bid [auction-id] [reveal-file-path]",
Short: "Reveal bid.",
revealFilePath := args[1]
revealBytes, err := os.ReadFile(revealFilePath) // #nosec G304
msg := types.NewMsgRevealBid(auctionID, hex.EncodeToString(revealBytes), clientCtx.GetFromAddress())