github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/ws/wsprojectupdate.go (about)

     1  package ws
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"math/big"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/iotexproject/iotex-core/ioctl/cmd/action"
    12  	"github.com/iotexproject/iotex-core/ioctl/config"
    13  	"github.com/iotexproject/iotex-core/ioctl/output"
    14  	"github.com/iotexproject/iotex-core/ioctl/util"
    15  )
    16  
    17  var (
    18  	// wsProjectUpdate represents the update w3bstream project command
    19  	wsProjectUpdate = &cobra.Command{
    20  		Use:   "update",
    21  		Short: config.TranslateInLang(wsProjectUpdateShorts, config.UILanguage),
    22  		RunE: func(cmd *cobra.Command, args []string) error {
    23  			id, err := cmd.Flags().GetUint64("project-id")
    24  			if err != nil {
    25  				return output.PrintError(err)
    26  			}
    27  			filename, err := cmd.Flags().GetString("project-config-file")
    28  			if err != nil {
    29  				return output.PrintError(err)
    30  			}
    31  			hash, err := cmd.Flags().GetString("project-config-hash")
    32  			if err != nil {
    33  				return output.PrintError(err)
    34  			}
    35  			out, err := updateProject(id, filename, hash)
    36  			if err != nil {
    37  				return output.PrintError(err)
    38  			}
    39  			output.PrintResult(out)
    40  			return nil
    41  		},
    42  	}
    43  
    44  	// wsProjectUpdateShorts update w3bstream project shorts multi-lang support
    45  	wsProjectUpdateShorts = map[config.Language]string{
    46  		config.English: "update w3bstream project",
    47  		config.Chinese: "更新项目",
    48  	}
    49  )
    50  
    51  func init() {
    52  	wsProjectUpdate.Flags().Uint64P("project-id", "i", 0, config.TranslateInLang(_flagProjectIDUsages, config.UILanguage))
    53  	wsProjectUpdate.Flags().StringP("project-config-file", "u", "", config.TranslateInLang(_flagProjectConfigFileUsages, config.UILanguage))
    54  	wsProjectUpdate.Flags().StringP("project-config-hash", "v", "", config.TranslateInLang(_flagProjectConfigHashUsages, config.UILanguage))
    55  
    56  	_ = wsProjectCreate.MarkFlagRequired("project-id")
    57  	_ = wsProjectCreate.MarkFlagRequired("project-config-file")
    58  }
    59  
    60  func updateProject(projectID uint64, filename, hashstr string) (string, error) {
    61  	uri, hashv, err := upload(wsProjectIPFSEndpoint, filename, hashstr)
    62  	if err != nil {
    63  		return "", err
    64  	}
    65  	fmt.Printf("project config file validated and uploaded:\n"+
    66  		"\tipfs uri: %s\n"+
    67  		"\thash256:  %s\n\n\n", uri, hex.EncodeToString(hashv[:]))
    68  
    69  	contract, err := util.Address(wsProjectRegisterContractAddress)
    70  	if err != nil {
    71  		return "", output.NewError(output.AddressError, "failed to get project register contract address", err)
    72  	}
    73  
    74  	bytecode, err := wsProjectRegisterContractABI.Pack(updateWsProjectFuncName, projectID, uri, hashv)
    75  	if err != nil {
    76  		return "", output.NewError(output.ConvertError, fmt.Sprintf("failed to pack abi"), err)
    77  	}
    78  
    79  	res, err := action.ExecuteAndResponse(contract, big.NewInt(0), bytecode)
    80  	if err != nil {
    81  		return "", errors.Wrap(err, "execute contract failed")
    82  	}
    83  
    84  	r, err := waitReceiptByActionHash(res.ActionHash)
    85  	if err != nil {
    86  		return "", errors.Wrap(err, "wait contract execution receipt failed")
    87  	}
    88  
    89  	inputs, err := getEventInputsByName(r.ReceiptInfo.Receipt.Logs, wsProjectUpsertedEventName)
    90  	if err != nil {
    91  		return "", errors.Wrap(err, "get receipt event failed")
    92  	}
    93  	_projectid, ok := inputs["projectId"]
    94  	if !ok {
    95  		return "", errors.New("result `projectId` not found in event inputs")
    96  	}
    97  	_uri, ok := inputs["uri"]
    98  	if !ok {
    99  		return "", errors.New("result `uri` not found in event inputs")
   100  	}
   101  	_hash, ok := inputs["hash"].([32]byte)
   102  	if !ok {
   103  		return "", errors.New("result `hash` not found in event inputs")
   104  	}
   105  	hashstr = hex.EncodeToString(_hash[:])
   106  	return fmt.Sprintf("Your project is successfully updated:\n%s", output.JSONString(projectMeta{
   107  		ProjectID:  _projectid.(uint64),
   108  		URI:        _uri.(string),
   109  		HashSha256: hashstr,
   110  	})), nil
   111  }