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

     1  package ws
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/iotexproject/iotex-address/address"
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/iotexproject/iotex-core/ioctl/cmd/action"
    11  	"github.com/iotexproject/iotex-core/ioctl/cmd/contract"
    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  	// wsProjectQuery represents the query w3bstream project command
    19  	wsProjectQuery = &cobra.Command{
    20  		Use:   "query",
    21  		Short: config.TranslateInLang(wsProjectQueryShorts, 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  			out, err := queryProject(id)
    28  			if err != nil {
    29  				return output.PrintError(err)
    30  			}
    31  			output.PrintResult(out)
    32  			return nil
    33  		},
    34  	}
    35  
    36  	// wsProjectQueryShorts query w3bstream project shorts multi-lang support
    37  	wsProjectQueryShorts = map[config.Language]string{
    38  		config.English: "query w3bstream project",
    39  		config.Chinese: "查询项目",
    40  	}
    41  )
    42  
    43  func init() {
    44  	wsProjectQuery.Flags().Uint64P("project-id", "i", 0, config.TranslateInLang(_flagProjectIDUsages, config.UILanguage))
    45  
    46  	_ = wsProjectQuery.MarkFlagRequired("project-id")
    47  }
    48  
    49  func queryProject(id uint64) (string, error) {
    50  	contractAddr, err := util.Address(wsProjectRegisterContractAddress)
    51  	if err != nil {
    52  		return "", output.NewError(output.AddressError, "failed to get project register contract address", err)
    53  	}
    54  
    55  	bytecode, err := wsProjectRegisterContractABI.Pack(queryWsProjectFuncName, id)
    56  	if err != nil {
    57  		return "", output.NewError(output.ConvertError, fmt.Sprintf("failed to pack abi"), err)
    58  	}
    59  
    60  	addr, _ := address.FromString(contractAddr)
    61  	data, err := action.Read(addr, "0", bytecode)
    62  	if err != nil {
    63  		return "", errors.Wrap(err, "read contract failed")
    64  	}
    65  	return contract.ParseOutput(&wsProjectRegisterContractABI, queryWsProjectFuncName, data)
    66  }