2021-10-25 11:48:21 +00:00
|
|
|
package smartcontract
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-02-25 07:27:51 +00:00
|
|
|
"strings"
|
2021-10-25 11:48:21 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/binding"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/urfave/cli"
|
2021-09-25 10:09:55 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-10-25 11:48:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var generateWrapperCmd = cli.Command{
|
|
|
|
Name: "generate-wrapper",
|
|
|
|
Usage: "generate wrapper to use in other contracts",
|
2022-03-22 14:33:54 +00:00
|
|
|
UsageText: "neo-go contract generate-wrapper --manifest <file.json> --out <file.go> --hash <hash>",
|
2021-10-25 11:48:21 +00:00
|
|
|
Description: ``,
|
|
|
|
Action: contractGenerateWrapper,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Usage: "Configuration file to use",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "manifest, m",
|
|
|
|
Usage: "Read contract manifest (*.manifest.json) file",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "out, o",
|
|
|
|
Usage: "Output of the compiled contract",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "hash",
|
|
|
|
Usage: "Smart-contract hash",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractGenerateWrapper deploys contract.
|
|
|
|
func contractGenerateWrapper(ctx *cli.Context) error {
|
|
|
|
m, _, err := readManifest(ctx.String("manifest"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't read contract manifest: %w", err), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := binding.NewConfig()
|
|
|
|
if cfgPath := ctx.String("config"); cfgPath != "" {
|
2022-02-22 16:27:32 +00:00
|
|
|
bs, err := os.ReadFile(cfgPath)
|
2021-10-25 11:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't read config file: %w", err), 1)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(bs, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't parse config file: %w", err), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Manifest = m
|
|
|
|
|
2022-02-25 07:27:51 +00:00
|
|
|
h, err := util.Uint160DecodeStringLE(strings.TrimPrefix(ctx.String("hash"), "0x"))
|
2021-10-25 11:48:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid contract hash: %w", err), 1)
|
|
|
|
}
|
|
|
|
cfg.Hash = h
|
|
|
|
|
|
|
|
f, err := os.Create(ctx.String("out"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't create output file: %w", err), 1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
cfg.Output = f
|
|
|
|
|
|
|
|
err = binding.Generate(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("error during generation: %w", err), 1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|