|
| 1 | +// Copyright (c) 2020 IoTeX Foundation |
| 2 | +// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no |
| 3 | +// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent |
| 4 | +// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache |
| 5 | +// License 2.0 that can be found in the LICENSE file. |
| 6 | + |
| 7 | +package contract |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io/ioutil" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | + |
| 16 | + "github.com/iotexproject/iotex-core/ioctl/config" |
| 17 | + "github.com/iotexproject/iotex-core/ioctl/output" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + abiOut string |
| 22 | + binOut string |
| 23 | +) |
| 24 | + |
| 25 | +// Multi-language support |
| 26 | +var ( |
| 27 | + contractCompileCmdUses = map[config.Language]string{ |
| 28 | + config.English: "compile CONTRACT_NAME --abi-out ABI_PATH --bin-out BIN_PATH", |
| 29 | + config.Chinese: "compile 合约名 --abi-out ABI路径 --bin-out BIN路径", |
| 30 | + } |
| 31 | + contractCompileCmdShorts = map[config.Language]string{ |
| 32 | + config.English: "Compile smart contract of IoTeX blockchain from source code", |
| 33 | + config.Chinese: "编译IoTeX区块链的智能合约代码", |
| 34 | + } |
| 35 | + flagAbiOutUsage = map[config.Language]string{ |
| 36 | + config.English: "set abi file output path", |
| 37 | + config.Chinese: "设置abi文件输出路径", |
| 38 | + } |
| 39 | + flagBinOutUsage = map[config.Language]string{ |
| 40 | + config.English: "set bin file output path", |
| 41 | + config.Chinese: "设置bin文件输出路径", |
| 42 | + } |
| 43 | +) |
| 44 | + |
| 45 | +// ContractCompileCmd represents the contract compile command |
| 46 | +var ContractCompileCmd = &cobra.Command{ |
| 47 | + Use: config.TranslateInLang(contractCompileCmdUses, config.UILanguage), |
| 48 | + Short: config.TranslateInLang(contractCompileCmdShorts, config.UILanguage), |
| 49 | + Args: cobra.ExactArgs(1), |
| 50 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 51 | + cmd.SilenceUsage = true |
| 52 | + err := compile(args) |
| 53 | + return output.PrintError(err) |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | +func init() { |
| 58 | + sourceFlag.RegisterCommand(ContractCompileCmd) |
| 59 | + sourceFlag.MarkFlagRequired(ContractCompileCmd) |
| 60 | + ContractCompileCmd.Flags().StringVar(&abiOut, "abi-out", "", |
| 61 | + config.TranslateInLang(flagAbiOutUsage, config.UILanguage)) |
| 62 | + ContractCompileCmd.Flags().StringVar(&binOut, "bin-out", "", |
| 63 | + config.TranslateInLang(flagBinOutUsage, config.UILanguage)) |
| 64 | + ContractCompileCmd.MarkFlagRequired("abi-out") |
| 65 | + ContractCompileCmd.MarkFlagRequired("bin-out") |
| 66 | +} |
| 67 | + |
| 68 | +func compile(args []string) error { |
| 69 | + contractName := sourceFlag.Value().(string) + ":" + args[0] |
| 70 | + |
| 71 | + contracts, err := Compile(sourceFlag.Value().(string)) |
| 72 | + if err != nil { |
| 73 | + return output.NewError(0, "failed to compile", err) |
| 74 | + } |
| 75 | + |
| 76 | + contract, ok := contracts[contractName] |
| 77 | + if !ok { |
| 78 | + return output.NewError(output.CompilerError, fmt.Sprintf("failed to get contract from %s", contractName), nil) |
| 79 | + } |
| 80 | + |
| 81 | + abiByte, err := json.Marshal(contract.Info.AbiDefinition) |
| 82 | + if err != nil { |
| 83 | + return output.NewError(output.SerializationError, "failed to marshal abi", err) |
| 84 | + } |
| 85 | + |
| 86 | + if err := ioutil.WriteFile(abiOut, abiByte, 0600); err != nil { |
| 87 | + return output.NewError(output.WriteFileError, "failed to write abi file", err) |
| 88 | + } |
| 89 | + // bin file starts with "0x" prefix |
| 90 | + if err := ioutil.WriteFile(binOut, []byte(contract.Code), 0600); err != nil { |
| 91 | + return output.NewError(output.WriteFileError, "failed to write bin file", err) |
| 92 | + } |
| 93 | + return nil |
| 94 | +} |
0 commit comments