2024-02-15 06:56:18 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-09-25 12:38:49 +00:00
|
|
|
"strconv"
|
2024-03-04 11:16:09 +00:00
|
|
|
"time"
|
2024-02-15 06:56:18 +00:00
|
|
|
|
|
|
|
"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"
|
|
|
|
|
2024-04-01 09:57:26 +00:00
|
|
|
wnsUtils "git.vdb.to/cerc-io/laconicd/utils"
|
|
|
|
auctiontypes "git.vdb.to/cerc-io/laconicd/x/auction"
|
2024-02-15 06:56:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetTxCmd returns transaction commands for this module.
|
|
|
|
func GetTxCmd() *cobra.Command {
|
|
|
|
auctionTxCmd := &cobra.Command{
|
|
|
|
Use: auctiontypes.ModuleName,
|
|
|
|
Short: "Auction transactions subcommands",
|
|
|
|
DisableFlagParsing: true,
|
|
|
|
SuggestionsMinimumDistance: 2,
|
|
|
|
RunE: client.ValidateCmd,
|
|
|
|
}
|
|
|
|
|
|
|
|
auctionTxCmd.AddCommand(
|
|
|
|
GetCmdCommitBid(),
|
|
|
|
GetCmdRevealBid(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return auctionTxCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdCommitBid is the CLI command for committing a bid.
|
|
|
|
func GetCmdCommitBid() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "commit-bid [auction-id] [bid-amount]",
|
|
|
|
Short: "Commit sealed bid",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
// Take chain id passed by user
|
|
|
|
chainId, _ := cmd.Flags().GetString(flags.FlagChainID)
|
|
|
|
if chainId == "" {
|
|
|
|
// Take from config if not provided
|
|
|
|
chainId = clientCtx.ChainID
|
2024-10-22 09:54:33 +00:00
|
|
|
}
|
|
|
|
if chainId == "" {
|
|
|
|
return fmt.Errorf("--chain-id required")
|
2024-09-25 12:38:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 06:56:18 +00:00
|
|
|
bidAmount, err := sdk.ParseCoinNormalized(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mnemonic, err := wnsUtils.GenerateMnemonic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save reveal file.
|
|
|
|
err = os.WriteFile(fmt.Sprintf("%s-%s.json", clientCtx.GetFromName(), commitHash), content, 0o600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := auctiontypes.NewMsgCommitBid(auctionId, commitHash, clientCtx.GetFromAddress())
|
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCmdRevealBid is the CLI command for revealing a bid.
|
|
|
|
func GetCmdRevealBid() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "reveal-bid [auction-id] [reveal-file-path]",
|
|
|
|
Short: "Reveal bid",
|
|
|
|
Args: cobra.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
auctionId := args[0]
|
|
|
|
revealFilePath := args[1]
|
|
|
|
|
|
|
|
revealBytes, err := os.ReadFile(revealFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := auctiontypes.NewMsgRevealBid(auctionId, hex.EncodeToString(revealBytes), clientCtx.GetFromAddress())
|
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2024-03-04 11:16:09 +00:00
|
|
|
|
|
|
|
func GetCmdCreateAuction() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2024-09-25 12:38:49 +00:00
|
|
|
Use: "create [kind] [commits-duration] [reveals-duration] [commit-fee] [reveal-fee] [minimum-bid] [max-price] [num-providers]",
|
2024-03-04 11:16:09 +00:00
|
|
|
Short: "Create auction.",
|
2024-09-25 12:38:49 +00:00
|
|
|
Args: cobra.ExactArgs(8),
|
2024-03-04 11:16:09 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
kind := args[0]
|
|
|
|
|
|
|
|
commitsDuration, err := time.ParseDuration(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
revealsDuration, err := time.ParseDuration(args[2])
|
2024-03-04 11:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
commitFee, err := sdk.ParseCoinNormalized(args[3])
|
2024-03-04 11:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
revealFee, err := sdk.ParseCoinNormalized(args[4])
|
2024-03-04 11:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
minimumBid, err := sdk.ParseCoinNormalized(args[5])
|
2024-03-04 11:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
maxPrice, err := sdk.ParseCoinNormalized(args[6])
|
2024-03-04 11:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-25 12:38:49 +00:00
|
|
|
numProviders, err := strconv.ParseInt(args[7], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-03-04 11:16:09 +00:00
|
|
|
}
|
2024-09-25 12:38:49 +00:00
|
|
|
|
|
|
|
msg := auctiontypes.NewMsgCreateAuction(
|
|
|
|
kind,
|
|
|
|
commitsDuration,
|
|
|
|
revealsDuration,
|
|
|
|
commitFee,
|
|
|
|
revealFee,
|
|
|
|
minimumBid,
|
|
|
|
maxPrice,
|
|
|
|
int32(numProviders),
|
|
|
|
clientCtx.GetFromAddress(),
|
|
|
|
)
|
2024-03-04 11:16:09 +00:00
|
|
|
err = msg.ValidateBasic()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
|
|
|
return cmd
|
|
|
|
}
|