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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/loomnetwork/gamechain/types/zb/zb_calls"
     6  	"github.com/loomnetwork/gamechain/types/zb/zb_data"
     7  	"github.com/loomnetwork/go-loom"
     8  	"github.com/loomnetwork/go-loom/auth"
     9  	"github.com/spf13/cobra"
    10  	"io/ioutil"
    11  )
    12  
    13  var callGameModeCustomGameModeFunctionArgs struct {
    14  	ID       string
    15  	abiInputFile string
    16  }
    17  
    18  var callGameModeCustomGameModeFunctionCmd = &cobra.Command{
    19  	Use:   "call_game_mode_custom_function",
    20  	Short: "calls a custom function on a game mode",
    21  	RunE: func(cmd *cobra.Command, args []string) error {
    22  		abiInputFileContents, err := ioutil.ReadFile(callGameModeCustomGameModeFunctionArgs.abiInputFile)
    23  		if err != nil {
    24  			return fmt.Errorf("unable to ABI-encoded call data from file: %s",
    25  				callGameModeCustomGameModeFunctionArgs.abiInputFile)
    26  		}
    27  
    28  		signer := auth.NewEd25519Signer(commonTxObjs.privateKey)
    29  		callerAddr := loom.Address{
    30  			ChainID: commonTxObjs.rpcClient.GetChainID(),
    31  			Local:   loom.LocalAddressFromPublicKey(signer.PublicKey()),
    32  		}
    33  
    34  		var req zb_calls.GetGameModeRequest
    35  		var gameMode = zb_data.GameMode{}
    36  
    37  		req.ID = callGameModeCustomGameModeFunctionArgs.ID
    38  
    39  		_, err = commonTxObjs.contract.StaticCall("GetGameMode", &req, callerAddr, &gameMode)
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		var reqUi zb_calls.CallCustomGameModeFunctionRequest
    45  
    46  		reqUi.Address = gameMode.Address
    47  		reqUi.CallData = abiInputFileContents
    48  
    49  		_, err = commonTxObjs.contract.Call("CallCustomGameModeFunction", &reqUi, signer, nil)
    50  		if err != nil {
    51  			return err
    52  		}
    53  
    54  		return nil
    55  	},
    56  }
    57  
    58  func init() {
    59  	rootCmd.AddCommand(callGameModeCustomGameModeFunctionCmd)
    60  	callGameModeCustomGameModeFunctionCmd.Flags().StringVar(&callGameModeCustomGameModeFunctionArgs.ID, "id", "", "id of the game mode")
    61  	callGameModeCustomGameModeFunctionCmd.Flags().StringVar(&callGameModeCustomGameModeFunctionArgs.abiInputFile, "abiInputFile", "call.bin", "Binary ABI-encoded function call data file")
    62  }