github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didupdate.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package did
     7  
     8  import (
     9  	"math/big"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/newcmd/action"
    17  )
    18  
    19  // Multi-language support
    20  var (
    21  	_updateCmdUses = map[config.Language]string{
    22  		config.English: "update (CONTRACT_ADDRESS|ALIAS) hash uri",
    23  		config.Chinese: "update (合约地址|别名) hash uri",
    24  	}
    25  	_updateCmdShorts = map[config.Language]string{
    26  		config.English: "Update DID on IoTeX blockchain",
    27  		config.Chinese: "在IoTeX链上更新DID",
    28  	}
    29  )
    30  
    31  // NewDidUpdateCmd represents the contract invoke update command
    32  func NewDidUpdateCmd(client ioctl.Client) *cobra.Command {
    33  	use, _ := client.SelectTranslation(_updateCmdUses)
    34  	short, _ := client.SelectTranslation(_updateCmdShorts)
    35  
    36  	cmd := &cobra.Command{
    37  		Use:   use,
    38  		Short: short,
    39  		Args:  cobra.ExactArgs(3),
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			cmd.SilenceUsage = true
    42  			contract, err := client.Address(args[0])
    43  			if err != nil {
    44  				return errors.Wrap(err, "failed to get contract address")
    45  			}
    46  			bytecode, err := encode(_updateDIDName, args[1], args[2])
    47  			if err != nil {
    48  				return errors.Wrap(err, "failed to decode data")
    49  			}
    50  			gasPrice, signer, password, nonce, gasLimit, assumeYes, err := action.GetWriteCommandFlag(cmd)
    51  			if err != nil {
    52  				return err
    53  			}
    54  			return action.Execute(client, cmd, contract, big.NewInt(0), bytecode, gasPrice, signer, password, nonce, gasLimit, assumeYes)
    55  		},
    56  	}
    57  	action.RegisterWriteCommand(client, cmd)
    58  	return cmd
    59  }