2018-02-09 16:08:50 +00:00
|
|
|
package smartcontract
|
|
|
|
|
|
|
|
import (
|
2018-10-16 06:33:29 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2018-03-05 08:53:09 +00:00
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2018-08-25 14:40:15 +00:00
|
|
|
"errors"
|
2018-03-05 08:53:09 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2018-08-25 14:40:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-03-05 08:53:09 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc"
|
2018-02-15 15:35:49 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/vm/compiler"
|
2018-02-09 16:08:50 +00:00
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2018-08-25 14:40:15 +00:00
|
|
|
var (
|
2019-10-18 15:34:58 +00:00
|
|
|
errNoInput = errors.New("no input file was found, specify an input file with the '--in or -i' flag")
|
|
|
|
errNoSmartContractName = errors.New("no name was provided, specify the '--name or -n' flag")
|
2018-08-25 14:40:15 +00:00
|
|
|
errFileExist = errors.New("A file with given smart-contract name already exists")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// smartContractTmpl is written to a file when used with `init` command.
|
|
|
|
// %s is parsed to be the smartContractName
|
|
|
|
smartContractTmpl = `package %s
|
|
|
|
|
2019-08-15 16:41:51 +00:00
|
|
|
import "github.com/CityOfZion/neo-go/pkg/interop/runtime"
|
2018-08-25 14:40:15 +00:00
|
|
|
|
|
|
|
func Main(op string, args []interface{}) {
|
|
|
|
runtime.Notify("Hello world!")
|
|
|
|
}`
|
2018-02-24 09:10:45 +00:00
|
|
|
)
|
|
|
|
|
2019-10-19 20:58:45 +00:00
|
|
|
// NewCommands returns 'contract' command.
|
|
|
|
func NewCommands() []cli.Command {
|
|
|
|
return []cli.Command{{
|
2018-02-09 16:08:50 +00:00
|
|
|
Name: "contract",
|
|
|
|
Usage: "compile - debug - deploy smart contracts",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "compile",
|
|
|
|
Usage: "compile a smart contract to a .avm file",
|
|
|
|
Action: contractCompile,
|
2018-02-15 15:35:49 +00:00
|
|
|
Flags: []cli.Flag{
|
2018-02-24 09:10:45 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
|
|
|
Usage: "Input file for the smart contract to be compiled",
|
|
|
|
},
|
2018-02-15 15:35:49 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "out, o",
|
|
|
|
Usage: "Output of the compiled contract",
|
|
|
|
},
|
2018-03-25 16:21:00 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "debug, d",
|
|
|
|
Usage: "Debug mode will print out additional information after a compiling",
|
|
|
|
},
|
2018-02-15 15:35:49 +00:00
|
|
|
},
|
2018-02-09 16:08:50 +00:00
|
|
|
},
|
2018-03-05 08:53:09 +00:00
|
|
|
{
|
2018-03-25 16:21:00 +00:00
|
|
|
Name: "testinvoke",
|
2018-03-05 08:53:09 +00:00
|
|
|
Usage: "Test an invocation of a smart contract on the blockchain",
|
|
|
|
Action: testInvoke,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
|
|
|
Usage: "Input location of the avm file that needs to be invoked",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-08-25 14:40:15 +00:00
|
|
|
{
|
|
|
|
Name: "init",
|
|
|
|
Usage: "initialize a new smart-contract in a directory with boiler plate code",
|
|
|
|
Action: initSmartContract,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name, n",
|
|
|
|
Usage: "name of the smart-contract to be initialized",
|
|
|
|
},
|
2018-10-16 06:33:29 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "skip-details, skip",
|
|
|
|
Usage: "skip filling in the projects and contract details",
|
|
|
|
},
|
2018-08-25 14:40:15 +00:00
|
|
|
},
|
|
|
|
},
|
2018-10-23 08:23:03 +00:00
|
|
|
{
|
|
|
|
Name: "inspect",
|
|
|
|
Usage: "creates a user readable dump of the program instructions",
|
|
|
|
Action: inspect,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "in, i",
|
|
|
|
Usage: "input file of the program",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-02-09 16:08:50 +00:00
|
|
|
},
|
2019-10-19 20:58:45 +00:00
|
|
|
}}
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-25 14:40:15 +00:00
|
|
|
// initSmartContract initializes a given directory with some boiler plate code.
|
|
|
|
func initSmartContract(ctx *cli.Context) error {
|
2018-10-16 06:33:29 +00:00
|
|
|
contractName := ctx.String("name")
|
|
|
|
if contractName == "" {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(errNoSmartContractName, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the file already exists, if yes, exit
|
2018-10-16 06:33:29 +00:00
|
|
|
if _, err := os.Stat(contractName); err == nil {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(errFileExist, 1)
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:33:29 +00:00
|
|
|
basePath := contractName
|
2018-08-25 14:40:15 +00:00
|
|
|
fileName := "main.go"
|
|
|
|
|
|
|
|
// create base directory
|
2018-10-16 06:33:29 +00:00
|
|
|
if err := os.Mkdir(basePath, os.ModePerm); err != nil {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:51:07 +00:00
|
|
|
// Ask contract information and write a neo-go.yml file unless the -skip-details flag is set.
|
|
|
|
// TODO: Fix the missing neo-go.yml file with the `init` command when the package manager is in place.
|
2018-10-16 06:33:29 +00:00
|
|
|
if !ctx.Bool("skip-details") {
|
|
|
|
details := parseContractDetails()
|
2019-08-15 16:51:07 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(basePath, "neo-go.yml"), details.toStormFile(), 0644); err != nil {
|
2018-10-16 06:33:29 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := []byte(fmt.Sprintf(smartContractTmpl, contractName))
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(basePath, fileName), data, 0644); err != nil {
|
2018-08-25 14:40:15 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2018-10-16 06:33:29 +00:00
|
|
|
|
|
|
|
fmt.Printf("Successfully initialized smart contract [%s]\n", contractName)
|
|
|
|
|
2018-08-25 14:40:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:08:50 +00:00
|
|
|
func contractCompile(ctx *cli.Context) error {
|
2018-02-24 09:10:45 +00:00
|
|
|
src := ctx.String("in")
|
|
|
|
if len(src) == 0 {
|
2018-03-05 08:53:09 +00:00
|
|
|
return cli.NewExitError(errNoInput, 1)
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-19 09:24:28 +00:00
|
|
|
o := &compiler.Options{
|
|
|
|
Outfile: ctx.String("out"),
|
2018-03-25 16:21:00 +00:00
|
|
|
Debug: ctx.Bool("debug"),
|
2018-02-15 15:35:49 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 09:10:45 +00:00
|
|
|
if err := compiler.CompileAndSave(src, o); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2018-02-24 09:10:45 +00:00
|
|
|
return nil
|
2018-02-09 16:08:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
func testInvoke(ctx *cli.Context) error {
|
|
|
|
src := ctx.String("in")
|
|
|
|
if len(src) == 0 {
|
|
|
|
return cli.NewExitError(errNoInput, 1)
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
b, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now we will hardcode the endpoint.
|
|
|
|
// On the long term the internal VM will run the script.
|
|
|
|
// TODO: remove RPC dependency, hardcoded node.
|
2018-10-23 08:23:03 +00:00
|
|
|
endpoint := "http://node1.ams2.bridgeprotocol.io:10332"
|
2018-10-20 05:11:00 +00:00
|
|
|
client, err := rpc.NewClient(context.TODO(), endpoint, rpc.ClientOptions{})
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptHex := hex.EncodeToString(b)
|
|
|
|
resp, err := client.InvokeScript(scriptHex)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = json.MarshalIndent(resp.Result, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(b))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// ContractDetails contains contract metadata.
|
2018-10-16 06:33:29 +00:00
|
|
|
type ContractDetails struct {
|
|
|
|
Author string
|
|
|
|
Email string
|
|
|
|
Version string
|
|
|
|
ProjectName string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d ContractDetails) toStormFile() []byte {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
2019-08-15 16:51:07 +00:00
|
|
|
buf.WriteString("# NEO-GO specific configuration. Do not modify this unless you know what you are doing!\n")
|
|
|
|
buf.WriteString("neo-go:\n")
|
2018-10-16 06:33:29 +00:00
|
|
|
buf.WriteString(" version: 1.0\n")
|
|
|
|
|
|
|
|
buf.WriteString("\n")
|
|
|
|
|
|
|
|
buf.WriteString("# Project section contains information about your smart contract\n")
|
|
|
|
buf.WriteString("project:\n")
|
|
|
|
buf.WriteString(" author: " + d.Author)
|
|
|
|
buf.WriteString(" email: " + d.Email)
|
|
|
|
buf.WriteString(" version: " + d.Version)
|
|
|
|
buf.WriteString(" name: " + d.ProjectName)
|
|
|
|
buf.WriteString(" description: " + d.Description)
|
|
|
|
|
|
|
|
buf.WriteString("\n")
|
|
|
|
|
|
|
|
buf.WriteString("# Module section contains a list of imported modules\n")
|
2019-08-15 16:51:07 +00:00
|
|
|
buf.WriteString("# This will be automatically managed by the neo-go package manager\n")
|
2018-10-16 06:33:29 +00:00
|
|
|
buf.WriteString("modules: \n")
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseContractDetails() ContractDetails {
|
|
|
|
details := ContractDetails{}
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
fmt.Print("Author: ")
|
|
|
|
details.Author, _ = reader.ReadString('\n')
|
|
|
|
|
|
|
|
fmt.Print("Email: ")
|
|
|
|
details.Email, _ = reader.ReadString('\n')
|
|
|
|
|
|
|
|
fmt.Print("Version: ")
|
|
|
|
details.Version, _ = reader.ReadString('\n')
|
|
|
|
|
|
|
|
fmt.Print("Project name: ")
|
|
|
|
details.ProjectName, _ = reader.ReadString('\n')
|
|
|
|
|
|
|
|
fmt.Print("Description: ")
|
|
|
|
details.Description, _ = reader.ReadString('\n')
|
|
|
|
|
|
|
|
return details
|
|
|
|
}
|
2018-10-23 08:23:03 +00:00
|
|
|
|
|
|
|
func inspect(ctx *cli.Context) error {
|
|
|
|
src := ctx.String("in")
|
|
|
|
if len(src) == 0 {
|
|
|
|
return cli.NewExitError(errNoInput, 1)
|
|
|
|
}
|
2019-08-14 17:29:32 +00:00
|
|
|
if err := compiler.CompileAndInspect(src); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2018-10-23 08:23:03 +00:00
|
|
|
return nil
|
|
|
|
}
|