github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/ws/wsprojectcreate.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 // wsProjectCreate represents the create w3bstream project command 19 wsProjectCreate = &cobra.Command{ 20 Use: "create", 21 Short: config.TranslateInLang(wsProjectCreateShorts, config.UILanguage), 22 RunE: func(cmd *cobra.Command, args []string) error { 23 filename, err := cmd.Flags().GetString("project-config-file") 24 if err != nil { 25 return output.PrintError(err) 26 } 27 hash, err := cmd.Flags().GetString("project-config-hash") 28 if err != nil { 29 return output.PrintError(err) 30 } 31 out, err := createProject(filename, hash) 32 if err != nil { 33 return output.PrintError(err) 34 } 35 output.PrintResult(out) 36 return nil 37 }, 38 } 39 40 // wsProjectCreateShorts create w3bstream project shorts multi-lang support 41 wsProjectCreateShorts = map[config.Language]string{ 42 config.English: "create w3bstream project", 43 config.Chinese: "创建项目", 44 } 45 46 _flagProjectConfigFileUsages = map[config.Language]string{ 47 config.English: "project config file path", 48 config.Chinese: "项目配置文件路径", 49 } 50 _flagProjectConfigHashUsages = map[config.Language]string{ 51 config.English: "project config file hash(sha256) for validating", 52 config.Chinese: "项目配置文件sha256哈希", 53 } 54 ) 55 56 func init() { 57 wsProjectCreate.Flags().StringP("project-config-file", "u", "", config.TranslateInLang(_flagProjectConfigFileUsages, config.UILanguage)) 58 wsProjectCreate.Flags().StringP("project-config-hash", "v", "", config.TranslateInLang(_flagProjectConfigHashUsages, config.UILanguage)) 59 60 _ = wsProjectCreate.MarkFlagRequired("project-config-file") 61 } 62 63 func createProject(filename, hashstr string) (string, error) { 64 uri, hashv, err := upload(wsProjectIPFSEndpoint, filename, hashstr) 65 if err != nil { 66 return "", err 67 } 68 fmt.Printf("project config file validated and uploaded:\n"+ 69 "\tipfs uri: %s\n"+ 70 "\thash256: %s\n\n\n", uri, hex.EncodeToString(hashv[:])) 71 72 contract, err := util.Address(wsProjectRegisterContractAddress) 73 if err != nil { 74 return "", output.NewError(output.AddressError, "failed to get project register contract address", err) 75 } 76 77 bytecode, err := wsProjectRegisterContractABI.Pack(createWsProjectFuncName, uri, hashv) 78 if err != nil { 79 return "", output.NewError(output.ConvertError, fmt.Sprintf("failed to pack abi"), err) 80 } 81 82 res, err := action.ExecuteAndResponse(contract, big.NewInt(0), bytecode) 83 if err != nil { 84 return "", errors.Wrap(err, "execute contract failed") 85 } 86 87 r, err := waitReceiptByActionHash(res.ActionHash) 88 if err != nil { 89 return "", errors.Wrap(err, "wait contract execution receipt failed") 90 } 91 92 inputs, err := getEventInputsByName(r.ReceiptInfo.Receipt.Logs, wsProjectUpsertedEventName) 93 if err != nil { 94 return "", errors.Wrap(err, "get receipt event failed") 95 } 96 projectid, ok := inputs["projectId"] 97 if !ok { 98 return "", errors.New("result not found in event inputs") 99 } 100 hashstr = hex.EncodeToString(hashv[:]) 101 return fmt.Sprintf("Your project is successfully created:\n%s", output.JSONString(&projectMeta{ 102 ProjectID: projectid.(uint64), 103 URI: uri, 104 HashSha256: hashstr, 105 })), nil 106 }