github.com/aeternity/aepp-sdk-go/v6@v6.0.0/cmd/contract.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/aeternity/aepp-sdk-go/v6/config"
     9  	"github.com/aeternity/aepp-sdk-go/v6/naet"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  var compilerURL string
    14  var contractCmd = &cobra.Command{
    15  	Use:   "contract subcommand",
    16  	Short: "Compile, call or deploy smart contracts",
    17  	Long:  ``,
    18  }
    19  
    20  var compileCmd = &cobra.Command{
    21  	Use:   "compile FILENAME",
    22  	Short: "Send a source file to a compiler",
    23  	Long:  ``,
    24  	Args:  cobra.ExactArgs(1),
    25  	RunE: func(cmd *cobra.Command, args []string) (err error) {
    26  		compiler := newCompiler()
    27  		return compileFunc(compiler, args)
    28  	},
    29  	SilenceUsage: true,
    30  }
    31  
    32  func compileFunc(conn naet.CompileContracter, args []string) (err error) {
    33  	s, err := readSource(args[0])
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	bytecode, err := conn.CompileContract(s, config.Compiler.Backend)
    39  	fmt.Println(bytecode)
    40  	return err
    41  }
    42  
    43  var encodeCalldataCmd = &cobra.Command{
    44  	Use:   "encodeCalldata SOURCE FUNCTIONNAME [..ARGS]",
    45  	Short: "Encode contract function calls. Needs the path to contract source file",
    46  	Long:  ``,
    47  	Args:  cobra.MinimumNArgs(2),
    48  	RunE: func(cmd *cobra.Command, args []string) (err error) {
    49  		compiler := newCompiler()
    50  		return encodeCalldataFunc(compiler, args)
    51  	},
    52  	SilenceUsage: true,
    53  }
    54  
    55  func encodeCalldataFunc(conn naet.EncodeCalldataer, args []string) (err error) {
    56  	s, err := readSource(args[0])
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	callData, err := conn.EncodeCalldata(s, args[1], args[2:], config.Compiler.Backend)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	fmt.Println(callData)
    66  	return
    67  }
    68  
    69  type decodeCalldataer interface {
    70  	naet.DecodeCalldataBytecoder
    71  	naet.DecodeCalldataSourcer
    72  }
    73  
    74  var decodeCalldataBytecodeCmd = &cobra.Command{
    75  	Use:   "decodeCalldataBytecode BYTECODE CALLDATA [..ARGS]",
    76  	Short: "Decode contract function calls. Needs the path to contract source file/compiled bytecode",
    77  	Long:  ``,
    78  	Args:  cobra.MinimumNArgs(2),
    79  	RunE: func(cmd *cobra.Command, args []string) (err error) {
    80  		compiler := newCompiler()
    81  		return decodeCalldataBytecodeFunc(compiler, args)
    82  	},
    83  	SilenceUsage: true,
    84  }
    85  
    86  func decodeCalldataBytecodeFunc(conn decodeCalldataer, args []string) (err error) {
    87  	if !IsBytecode(args[0]) {
    88  		return fmt.Errorf("%s is not bytecode", args[0])
    89  	}
    90  	if !IsBytecode(args[1]) {
    91  		return fmt.Errorf("%s is not bytecode", args[0])
    92  	}
    93  
    94  	r, err := conn.DecodeCalldataBytecode(args[0], args[1], config.Compiler.Backend)
    95  	if err != nil {
    96  		return
    97  	}
    98  
    99  	fmt.Println(*r.Function, r.Arguments)
   100  	return nil
   101  }
   102  
   103  var decodeCalldataSourceCmd = &cobra.Command{
   104  	Use:   "decodeCalldataSource SOURCE_FILE FUNCTION_NAME CALLDATA [..ARGS]",
   105  	Short: "Decode contract function calls. Needs the path to contract source file/compiled bytecode",
   106  	Long:  ``,
   107  	Args:  cobra.MinimumNArgs(2),
   108  	RunE: func(cmd *cobra.Command, args []string) (err error) {
   109  		compiler := newCompiler()
   110  		return decodeCalldataSourceFunc(compiler, args)
   111  	},
   112  	SilenceUsage: true,
   113  }
   114  
   115  func decodeCalldataSourceFunc(conn decodeCalldataer, args []string) (err error) {
   116  	source, err := readSource(args[0])
   117  	if err != nil {
   118  		return err
   119  	}
   120  	if !IsBytecode(args[2]) {
   121  		return fmt.Errorf("%s is not bytecode", args[0])
   122  	}
   123  
   124  	r, err := conn.DecodeCalldataSource(source, args[1], args[2], config.Compiler.Backend)
   125  
   126  	fmt.Println(*r.Function, r.Arguments)
   127  	return
   128  }
   129  
   130  var generateAciCmd = &cobra.Command{
   131  	Use:   "generateaci FILENAME",
   132  	Short: "Generate ACI out of source code",
   133  	Long:  ``,
   134  	Args:  cobra.ExactArgs(1),
   135  	RunE: func(cmd *cobra.Command, args []string) (err error) {
   136  		compiler := newCompiler()
   137  		return generateAciFunc(compiler, args)
   138  	},
   139  	SilenceUsage: true,
   140  }
   141  
   142  func generateAciFunc(conn naet.GenerateACIer, args []string) (err error) {
   143  	source, err := readSource(args[0])
   144  	if err != nil {
   145  		return
   146  	}
   147  
   148  	aci, err := conn.GenerateACI(source, config.Compiler.Backend)
   149  	if err != nil {
   150  		return
   151  	}
   152  	PrintObject("ACI", aci)
   153  	return nil
   154  }
   155  func readSource(path string) (s string, err error) {
   156  	file, err := os.Open(path)
   157  	if err != nil {
   158  		return "", err
   159  	}
   160  
   161  	b, err := ioutil.ReadAll(file)
   162  	return string(b), err
   163  }
   164  
   165  func init() {
   166  	RootCmd.AddCommand(contractCmd)
   167  	contractCmd.AddCommand(compileCmd)
   168  	contractCmd.AddCommand(encodeCalldataCmd)
   169  	contractCmd.AddCommand(decodeCalldataBytecodeCmd)
   170  	contractCmd.AddCommand(decodeCalldataSourceCmd)
   171  }