Skip to content

Commit

Permalink
Merge pull request #1222 from onflow/add-flix-gen
Browse files Browse the repository at this point in the history
Add support for JS code generation for flix
  • Loading branch information
bthaile authored Oct 30, 2023
2 parents fd092ef + 0009702 commit 670fac7
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 47 deletions.
2 changes: 1 addition & 1 deletion cmd/flow/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func main() {
// super commands
super.SetupCommand.AddToParent(cmd)
super.DevCommand.AddToParent(cmd)
super.FlixCommand.AddToParent(cmd)

// structured commands
cmd.AddCommand(settings.Cmd)
Expand All @@ -86,6 +85,7 @@ func main() {
cmd.AddCommand(config.Cmd)
cmd.AddCommand(signatures.Cmd)
cmd.AddCommand(snapshot.Cmd)
cmd.AddCommand(super.FlixCmd)

command.InitFlags(cmd)
cmd.AddGroup(&cobra.Group{
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/onflow/cadence-tools/languageserver v0.32.1-0.20231019231050-6b3b4c3d7391
github.com/onflow/cadence-tools/test v0.13.0
github.com/onflow/fcl-dev-wallet v0.7.4
github.com/onflow/flixkit-go v0.1.0
github.com/onflow/flixkit-go v0.2.0
github.com/onflow/flow-cli/flowkit v1.3.5-0.20230808220356-6a2bfeb10552
github.com/onflow/flow-core-contracts/lib/go/templates v1.2.3
github.com/onflow/flow-emulator v0.56.0
Expand Down Expand Up @@ -197,6 +197,7 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c // indirect
Expand Down Expand Up @@ -225,7 +226,7 @@ require (
golang.org/x/net v0.12.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
20 changes: 14 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ github.com/ethereum/go-ethereum v1.10.22/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbL
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.3.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fjl/memsize v0.0.0-20180418122429-ca190fb6ffbc/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0=
github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4/go.mod h1:T9YF2M40nIgbVgp3rreNmTged+9HrbNTIQf1PsaIiTA=
Expand Down Expand Up @@ -576,6 +577,9 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hexops/autogold/v2 v2.2.1 h1:JPUXuZQGkcQMv7eeDXuNMovjfoRYaa0yVcm+F3voaGY=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/valast v1.4.4 h1:rETyycw+/L2ZVJHHNxEBgh8KUn+87WugH9MxcEv9PGs=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/huin/goupnp v0.0.0-20161224104101-679507af18f3/go.mod h1:MZ2ZmwcBpvOoJ22IJsc7va19ZwoheaBk43rKg12SKag=
Expand Down Expand Up @@ -866,6 +870,7 @@ github.com/nats-io/nkeys v0.1.0/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxzi
github.com/nats-io/nkeys v0.1.3/go.mod h1:xpnFELMwJABBLVhffcfd1MZx6VsNRFpEugbxziKVo7w=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs=
Expand All @@ -889,8 +894,8 @@ github.com/onflow/cadence-tools/test v0.13.0 h1:K94h3EiPnwS1iWmvCjqO6cgArOGdPp+5
github.com/onflow/cadence-tools/test v0.13.0/go.mod h1:Xb/S5oP4WKoJQ2RUKXDNSW41hYtQBUpgdcWggn6S2t4=
github.com/onflow/fcl-dev-wallet v0.7.4 h1:vI6t3U0AO88R/Iitn5KsnniSpbN9Lqsqwvi9EJT4C0k=
github.com/onflow/fcl-dev-wallet v0.7.4/go.mod h1:kc42jkiuoPJmxMRFjfbRO9XvnR/3XLheaOerxVMDTiw=
github.com/onflow/flixkit-go v0.1.0 h1:3nH+1z+D+0YlmEJk9zYtvD8p4eUl2wJtiV6ZCgM7vYE=
github.com/onflow/flixkit-go v0.1.0/go.mod h1:gPffHQ6jDyuNtLG6W9C6FGvDZmcOb9CkvsJYKY0Opc4=
github.com/onflow/flixkit-go v0.2.0 h1:yI7jksFPkLoQkFZ31oYrq3xPaZBn+qqLslpLM7TttSo=
github.com/onflow/flixkit-go v0.2.0/go.mod h1:i3EYZbxDP+k0n4nt1fzlwBFMENdNA82w+ZlQ9knfunQ=
github.com/onflow/flow-archive v1.3.4-0.20230503192214-9e81e82d4dcc h1:C4ZniFeOv+pHlDLJdGc/4e3NklSjVuvaXKN47980gnY=
github.com/onflow/flow-archive v1.3.4-0.20230503192214-9e81e82d4dcc/go.mod h1:UPsvKk/37Atosif4wlBl3gsLbGJyGpdXYpXDsWtMVBE=
github.com/onflow/flow-core-contracts/lib/go/contracts v1.2.4-0.20230703193002-53362441b57d h1:B7PdhdUNkve5MVrekWDuQf84XsGBxNZ/D3x+QQ8XeVs=
Expand Down Expand Up @@ -1111,6 +1116,8 @@ github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1Fof
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q=
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8OR4w3TdeIHIh1g6EMY5p0gVNOovcWC+1vpc7naMuAw=
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU=
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
Expand Down Expand Up @@ -1327,7 +1334,7 @@ golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1514,8 +1521,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down Expand Up @@ -1615,7 +1622,7 @@ golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down Expand Up @@ -1856,6 +1863,7 @@ modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
modernc.org/sqlite v1.21.1 h1:GyDFqNnESLOhwwDRaHGdp2jKLDzpyT/rNLglX3ZkMSU=
modernc.org/sqlite v1.21.1/go.mod h1:XwQ0wZPIh1iKb5mkvCJ3szzbhk+tykC8ZWqTRTgYRwI=
mvdan.cc/gofumpt v0.5.0 h1:0EQ+Z56k8tXjj/6TQD25BFNKQXpCvT0rnansIc7Ug5E=
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
Expand Down
155 changes: 117 additions & 38 deletions internal/super/flix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
"os"

"github.com/onflow/flixkit-go"
"github.com/onflow/flixkit-go/bindings"

"github.com/onflow/flow-cli/flowkit"
"github.com/onflow/flow-cli/flowkit/output"
"github.com/onflow/flow-cli/internal/command"
"github.com/onflow/flow-cli/internal/scripts"
"github.com/onflow/flow-cli/internal/transactions"

"github.com/onflow/flow-cli/flowkit"

"github.com/spf13/cobra"
)

Expand All @@ -49,18 +49,44 @@ type flixFlags struct {
GasLimit uint64 `default:"1000" flag:"gas-limit" info:"transaction gas limit"`
}

type flixResult struct {
flixQuery string
result string
}

var flags = flixFlags{}
var FlixCmd = &cobra.Command{
Use: "flix",
Short: "execute, package",
TraverseChildren: true,
GroupID: "tools",
}

var FlixCommand = &command.Command{
var executeCommand = &command.Command{
Cmd: &cobra.Command{
Use: "flix <id | name | path>",
Short: "Execute FLIX template with a given id, name, or local filename",
Example: "flow flix multiply 2 3",
Args: cobra.ArbitraryArgs,
GroupID: "super",
Use: "execute <id | name | path>",
Short: "execute FLIX template with a given id, name, or local filename",
Example: "flow flix execute transfer-flow 1 0x123456789",
Args: cobra.MinimumNArgs(1),
},
Flags: &flags,
RunS: execute,
RunS: executeCmd,
}

var packageCommand = &command.Command{
Cmd: &cobra.Command{
Use: "package <id | name | path>",
Short: "package file for FLIX template fcl-js is default",
Example: "flow flix package multiply.template.json",
Args: cobra.MinimumNArgs(1),
},
Flags: &flags,
RunS: packageCmd,
}

func init() {
executeCommand.AddToParent(FlixCmd)
packageCommand.AddToParent(FlixCmd)
}

type flixQueryTypes string
Expand Down Expand Up @@ -95,43 +121,18 @@ func getType(s string) flixQueryTypes {
}
}

func execute(
func executeCmd(
args []string,
_ command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.Config{})
ctx := context.Background()
var template *flixkit.FlowInteractionTemplate
flixQuery := args[0]

switch getType(flixQuery) {
case flixId:
template, err = flixService.GetFlixByID(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err)
}

case flixName:
template, err = flixService.GetFlix(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err)
}

case flixPath:
file, err := os.ReadFile(flixQuery)
if err != nil {
return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
}
template, err = flixkit.ParseFlix(string(file))
if err != nil {
return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err)
}

default:
return nil, fmt.Errorf("invalid flix query type: %s", flixQuery)
template, err := getTemplate(state, flixService, flixQuery)
if err != nil {
return nil, err
}

cadenceWithImportsReplaced, err := template.GetAndReplaceCadenceImports(flow.Network().Name)
Expand Down Expand Up @@ -161,3 +162,81 @@ func execute(
}
return transactions.SendTransaction([]byte(cadenceWithImportsReplaced), args[1:], "", flow, state, transactionFlags)
}

func packageCmd(
args []string,
_ command.GlobalFlags,
logger output.Logger,
flow flowkit.Services,
state *flowkit.State,
) (result command.Result, err error) {
flixService := flixkit.NewFlixService(&flixkit.Config{})
flixQuery := args[0]

template, err := getTemplate(state, flixService, flixQuery)
if err != nil {
return nil, err
}

isLocal := false
if getType(flixQuery) == flixPath {
isLocal = true
}

fclJsGen := bindings.NewFclJSGenerator()

out, err := fclJsGen.Generate(template, flixQuery, isLocal)

return &flixResult{
flixQuery: flixQuery,
result: out,
}, err
}

func (fr *flixResult) JSON() any {
result := make(map[string]any)
result["flixQuery"] = fr.flixQuery
result["result"] = fr.result
return result
}

func (fr *flixResult) String() string {
return fr.result
}

func (fr *flixResult) Oneliner() string {
return fr.result
}

func getTemplate(state *flowkit.State, flixService flixkit.FlixService, flixQuery string) (*flixkit.FlowInteractionTemplate, error) {
var template *flixkit.FlowInteractionTemplate
var err error
ctx := context.Background()
switch getType(flixQuery) {
case flixId:
template, err = flixService.GetFlixByID(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with id %s: %w", flixQuery, err)
}

case flixName:
template, err = flixService.GetFlix(ctx, flixQuery)
if err != nil {
return nil, fmt.Errorf("could not find flix with name %s: %w", flixQuery, err)
}

case flixPath:
file, err := state.ReadFile(flixQuery)
if err != nil {
return nil, fmt.Errorf("could not read flix file %s: %w", flixQuery, err)
}
template, err = flixkit.ParseFlix(string(file))
if err != nil {
return nil, fmt.Errorf("could not parse flix from file %s: %w", flixQuery, err)
}

default:
return nil, fmt.Errorf("invalid flix query type: %s", flixQuery)
}
return template, nil
}

0 comments on commit 670fac7

Please sign in to comment.