Skip to content

Commit

Permalink
update cli for commit solution and submit scavenge
Browse files Browse the repository at this point in the history
  • Loading branch information
nashqueue committed Aug 25, 2022
1 parent a978009 commit fb71ee0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
45 changes: 31 additions & 14 deletions x/scavenge/client/cli/tx_commit_solution.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
// x/scavenge/client/cli/tx_commit_solution.go

package cli

import (
"strconv"
"crypto/sha256"
"encoding/hex"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"

"scavenge/x/scavenge/types"
)

var _ = strconv.Itoa(0)

func CmdCommitSolution() *cobra.Command {
cmd := &cobra.Command{
Use: "commit-solution [solution-hash] [solution-scavenger-hash]",
// pass a solution as the only argument
Use: "commit-solution [solution]",
Short: "Broadcast message commit-solution",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argSolutionHash := args[0]
argSolutionScavengerHash := args[1]

// set the number of arguments to 1
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgCommitSolution(
clientCtx.GetFromAddress().String(),
argSolutionHash,
argSolutionScavengerHash,
)
solution := args[0]

// find a hash of the solution
solutionHash := sha256.Sum256([]byte(solution))

// convert the solution hash to string
solutionHashString := hex.EncodeToString(solutionHash[:])

// convert a scavenger address to string
var scavenger = clientCtx.GetFromAddress().String()

// find the hash of solution and scavenger address
var solutionScavengerHash = sha256.Sum256([]byte(solution + scavenger))

// convert the hash to string
var solutionScavengerHashString = hex.EncodeToString(solutionScavengerHash[:])

// create a new message
msg := types.NewMsgCommitSolution(clientCtx.GetFromAddress().String(), string(solutionHashString), string(solutionScavengerHashString))
if err := msg.ValidateBasic(); err != nil {
return err
}

// broadcast the transaction with the message to the blockchain
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand Down
34 changes: 19 additions & 15 deletions x/scavenge/client/cli/tx_submit_scavenge.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
// x/scavenge/client/cli/tx_submit_scavenge.go

package cli

import (
"strconv"
"crypto/sha256"
"encoding/hex"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"

"scavenge/x/scavenge/types"
)

var _ = strconv.Itoa(0)

func CmdSubmitScavenge() *cobra.Command {
cmd := &cobra.Command{
Use: "submit-scavenge [solution-hash] [description] [reward]",
Use: "submit-scavenge [solution] [description] [reward]",
Short: "Broadcast message submit-scavenge",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argSolutionHash := args[0]
argDescription := args[1]
argReward := args[2]

RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgSubmitScavenge(
clientCtx.GetFromAddress().String(),
argSolutionHash,
argDescription,
argReward,
)
// find a hash of the solution
solutionHash := sha256.Sum256([]byte(args[0]))

// convert the hash to string
solutionHashString := hex.EncodeToString(solutionHash[:])
argsDescription := string(args[1])
argsReward := string(args[2])

// create a new message
msg := types.NewMsgSubmitScavenge(clientCtx.GetFromAddress().String(), string(solutionHashString), string(argsDescription), string(argsReward))
if err := msg.ValidateBasic(); err != nil {
return err
}

// broadcast the transaction with the message to the blockchain
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand Down

0 comments on commit fb71ee0

Please sign in to comment.