github.com/loomnetwork/gamechain@v0.0.0-20200406110549-36c47eb97a92/cli/cmd/process_oracle_event_batch.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/gogo/protobuf/proto"
     7  	"github.com/loomnetwork/gamechain/tools/battleground_utility"
     8  	"github.com/loomnetwork/gamechain/types/oracle"
     9  	"io/ioutil"
    10  	"strings"
    11  
    12  	"github.com/loomnetwork/go-loom/auth"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var processOracleEventBatchCmdArgs struct {
    17  	processOracleEventBatchJsonFile string
    18  	processOracleEventBatchProtobufFile string
    19  }
    20  
    21  var processOracleEventBatchCmd = &cobra.Command{
    22  	Use:   "process_oracle_event_batch",
    23  	Short: "calls ProcessOracleEventBatch method of the contract with a request from a file",
    24  	RunE: func(cmd *cobra.Command, args []string) error {
    25  		var req oracle.ProcessOracleEventBatchRequest
    26  
    27  		var err error
    28  		if processOracleEventBatchCmdArgs.processOracleEventBatchJsonFile != "" {
    29  			err = battleground_utility.ReadJsonFileToProtoMessage(processOracleEventBatchCmdArgs.processOracleEventBatchJsonFile, &req)
    30  			if err != nil {
    31  				return err
    32  			}
    33  		} else {
    34  			bytes, err := ioutil.ReadFile(processOracleEventBatchCmdArgs.processOracleEventBatchProtobufFile)
    35  			if err != nil {
    36  				return err
    37  			}
    38  
    39  			err = proto.Unmarshal(bytes, &req)
    40  			if err != nil {
    41  				return err
    42  			}
    43  		}
    44  
    45  		signer := auth.NewEd25519Signer(commonTxObjs.privateKey)
    46  		_, err = commonTxObjs.contract.Call("ProcessOracleEventBatch", &req, signer, nil)
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		switch strings.ToLower(rootCmdArgs.outputFormat) {
    52  		case "json":
    53  			output, err := json.Marshal(map[string]interface{}{"success": true})
    54  			if err != nil {
    55  				return err
    56  			}
    57  			fmt.Println(string(output))
    58  		default:
    59  			fmt.Println("simulated event batch sent")
    60  		}
    61  
    62  		return nil
    63  	},
    64  }
    65  
    66  func init() {
    67  	rootCmd.AddCommand(processOracleEventBatchCmd)
    68  	processOracleEventBatchCmd.Flags().StringVarP(&processOracleEventBatchCmdArgs.processOracleEventBatchJsonFile, "requestJson", "", "", "file with JSON of ProcessOracleEventBatchRequest protobuf")
    69  	processOracleEventBatchCmd.Flags().StringVarP(&processOracleEventBatchCmdArgs.processOracleEventBatchProtobufFile, "requestPb", "", "", "binary file with ProcessOracleEventBatchRequest protobuf")
    70  }