1
1
package cli
2
2
3
3
import (
4
- "fmt"
5
4
"bufio"
6
-
7
- "github.com/spf13/cobra"
5
+ "crypto/sha256"
6
+ "encoding/hex"
7
+ "fmt"
8
8
9
9
"github.com/cosmos/cosmos-sdk/client"
10
- "github.com/cosmos/cosmos-sdk/client/flags"
11
10
"github.com/cosmos/cosmos-sdk/client/context"
11
+ "github.com/cosmos/cosmos-sdk/client/flags"
12
12
"github.com/cosmos/cosmos-sdk/codec"
13
- sdk "github.com/cosmos/cosmos-sdk/types"
14
13
"github.com/cosmos/cosmos-sdk/x/auth"
15
14
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
15
+ "github.com/spf13/cobra"
16
+
16
17
"github.com/victor118/scavenge/x/scavenge/types"
17
18
)
18
19
@@ -27,33 +28,95 @@ func GetTxCmd(cdc *codec.Codec) *cobra.Command {
27
28
}
28
29
29
30
scavengeTxCmd .AddCommand (flags .PostCommands (
30
- // TODO: Add tx based commands
31
- // GetCmd<Action>(cdc)
31
+ GetCmdCreateScavenge (cdc ),
32
+ GetCmdCommitSolution (cdc ),
33
+ GetCmdRevealSolution (cdc ),
32
34
)... )
33
35
34
36
return scavengeTxCmd
35
37
}
36
38
37
- // Example:
38
- //
39
- // GetCmd<Action> is the CLI command for doing <Action>
40
- // func GetCmd<Action>(cdc *codec.Codec) *cobra.Command {
41
- // return &cobra.Command{
42
- // Use: "/* Describe your action cmd */",
43
- // Short: "/* Provide a short description on the cmd */",
44
- // Args: cobra.ExactArgs(2), // Does your request require arguments
45
- // RunE: func(cmd *cobra.Command, args []string) error {
46
- // cliCtx := context.NewCLIContext().WithCodec(cdc)
47
- // inBuf := bufio.NewReader(cmd.InOrStdin())
48
- // txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))
49
-
50
- // msg := types.NewMsg<Action>(/* Action params */)
51
- // err = msg.ValidateBasic()
52
- // if err != nil {
53
- // return err
54
- // }
55
-
56
- // return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
57
- // },
58
- // }
59
- // }
39
+ func GetCmdCreateScavenge (cdc * codec.Codec ) * cobra.Command {
40
+ return & cobra.Command {
41
+ Use : "createScavenge [reward] [solution] [description]" ,
42
+ Short : "Creates a new scavenge with a reward" ,
43
+ Args : cobra .ExactArgs (3 ), // Does your request require arguments
44
+ RunE : func (cmd * cobra.Command , args []string ) error {
45
+
46
+ cliCtx := context .NewCLIContext ().WithCodec (cdc )
47
+ inBuf := bufio .NewReader (cmd .InOrStdin ())
48
+ txBldr := auth .NewTxBuilderFromCLI (inBuf ).WithTxEncoder (utils .GetTxEncoder (cdc ))
49
+
50
+ reward , err := sdk .ParseCoins (args [0 ])
51
+ if err != nil {
52
+ return err
53
+ }
54
+
55
+ var solution = args [1 ]
56
+ var solutionHash = sha256 .Sum256 ([]byte (solution ))
57
+ var solutionHashString = hex .EncodeToString (solutionHash [:])
58
+
59
+ msg := types .NewMsgCreateScavenge (cliCtx .GetFromAddress (), args [2 ], solutionHashString , reward )
60
+ err = msg .ValidateBasic ()
61
+ if err != nil {
62
+ return err
63
+ }
64
+
65
+ return utils .GenerateOrBroadcastMsgs (cliCtx , txBldr , []sdk.Msg {msg })
66
+ },
67
+ }
68
+ }
69
+
70
+ func GetCmdCommitSolution (cdc * codec.Codec ) * cobra.Command {
71
+ return & cobra.Command {
72
+ Use : "commitSolution [solution]" ,
73
+ Short : "Commits a solution for scavenge" ,
74
+ Args : cobra .ExactArgs (1 ), // Does your request require arguments
75
+ RunE : func (cmd * cobra.Command , args []string ) error {
76
+
77
+ cliCtx := context .NewCLIContext ().WithCodec (cdc )
78
+ inBuf := bufio .NewReader (cmd .InOrStdin ())
79
+ txBldr := auth .NewTxBuilderFromCLI (inBuf ).WithTxEncoder (utils .GetTxEncoder (cdc ))
80
+
81
+ var solution = args [0 ]
82
+ var solutionHash = sha256 .Sum256 ([]byte (solution ))
83
+ var solutionHashString = hex .EncodeToString (solutionHash [:])
84
+
85
+ var scavenger = cliCtx .GetFromAddress ().String ()
86
+
87
+ var solutionScavengerHash = sha256 .Sum256 ([]byte (solution + scavenger ))
88
+ var solutionScavengerHashString = hex .EncodeToString (solutionScavengerHash [:])
89
+
90
+ msg := types .NewMsgCommitSolution (cliCtx .GetFromAddress (), solutionHashString , solutionScavengerHashString )
91
+ err := msg .ValidateBasic ()
92
+ if err != nil {
93
+ return err
94
+ }
95
+ return utils .GenerateOrBroadcastMsgs (cliCtx , txBldr , []sdk.Msg {msg })
96
+ },
97
+ }
98
+ }
99
+
100
+ func GetCmdRevealSolution (cdc * codec.Codec ) * cobra.Command {
101
+ return & cobra.Command {
102
+ Use : "revealSolution [solution]" ,
103
+ Short : "Reveals a solution for scavenge" ,
104
+ Args : cobra .ExactArgs (1 ), // Does your request require arguments
105
+ RunE : func (cmd * cobra.Command , args []string ) error {
106
+
107
+ cliCtx := context .NewCLIContext ().WithCodec (cdc )
108
+ inBuf := bufio .NewReader (cmd .InOrStdin ())
109
+ txBldr := auth .NewTxBuilderFromCLI (inBuf ).WithTxEncoder (utils .GetTxEncoder (cdc ))
110
+
111
+ var solution = args [0 ]
112
+
113
+ msg := types .NewMsgRevealSolution (cliCtx .GetFromAddress (), solution )
114
+ err := msg .ValidateBasic ()
115
+ if err != nil {
116
+ return err
117
+ }
118
+
119
+ return utils .GenerateOrBroadcastMsgs (cliCtx , txBldr , []sdk.Msg {msg })
120
+ },
121
+ }
122
+ }
0 commit comments