Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flix 1.0.0 generator #20

Merged
merged 35 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f71efc4
checkpoint, adding generator module
bthaile Oct 18, 2023
b5c0c98
working on version 1.0.0 first, 1.1.0 will be next
bthaile Oct 18, 2023
b492138
checkpoint, adding generator module
bthaile Oct 18, 2023
56dc598
working on version 1.0.0 first, 1.1.0 will be next
bthaile Oct 18, 2023
d2905af
checkpoint, adding more structure to code
bthaile Oct 19, 2023
869a1d7
checkpoint, figuring out dependencies structure, need to get placehol…
bthaile Oct 21, 2023
4bea3bf
resolve conflicts
bthaile Oct 23, 2023
201b7bf
fix merge conflict
bthaile Oct 23, 2023
1ad64e8
merged down changes to main, fixed regex to determine type of cadence…
bthaile Oct 23, 2023
90922b1
add more unit tests, get old import syntax parsed to Dependencies pro…
bthaile Oct 24, 2023
a16ff44
stubbed out pieces for generating flix, can test integration with flo…
bthaile Oct 24, 2023
743dc10
create simple way of creating a generator 1.0.0 version
bthaile Oct 25, 2023
ea6f08c
fix go sum file
bthaile Oct 25, 2023
39507c3
downgrade module to be conpat with flow-cli
bthaile Oct 26, 2023
fe491b1
manually process imports since cadence parser does not like placehold…
bthaile Oct 26, 2023
3ca04b4
fix partial impl of cadence comments to flix properties, fix spacing …
bthaile Oct 27, 2023
1212694
allow caller to pass in deployed contracts that are needed to process…
bthaile Oct 27, 2023
bfa1ff0
more unit tests, support new import syntax, add core contracts placeh…
bthaile Oct 31, 2023
034001a
get hashing simple template working, next is dep hashing
bthaile Nov 3, 2023
f41ac86
add sorting maps to get a consistent hashed template id
bthaile Nov 3, 2023
0251665
fix binding tests, get contract pinning hashes generated for mainnet …
bthaile Nov 5, 2023
c645666
have tests use generator instead of NewGenerator, build flowkit clien…
bthaile Nov 6, 2023
d5c7f0b
allow user to pass in their own version of core contracts
bthaile Nov 6, 2023
25a5e38
when generating flix, allow empty messages in cadence comment blocks
bthaile Nov 6, 2023
1ad40b9
fix binding file of empty object as parameter when there are no param…
bthaile Nov 6, 2023
1791315
add to generated js if method does not need any parameters
bthaile Nov 6, 2023
de1823c
make changes to address PR comments, reduce modules needed to sort fl…
bthaile Nov 7, 2023
01a8db5
create a v1_0_0 module for generating flix
bthaile Nov 8, 2023
9f26ac1
use generator interface for v1.0.0 generator so that v1.1.0 can use t…
bthaile Nov 8, 2023
30ba388
simplify how to process old and new import syntax
bthaile Nov 13, 2023
f8588ce
udate tests
bthaile Nov 13, 2023
09b5816
add tests
bthaile Nov 13, 2023
1e2b151
fix merging in prefill message with argument section
bthaile Nov 14, 2023
2cac7f7
fix generating params for scripts
bthaile Nov 14, 2023
090af4f
naming changes based on PR comments
bthaile Nov 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions bindings/fcl-js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ var minimumTemplate = &flixkit.FlowInteractionTemplate{
},
}

var minimumNoParamTemplate = &flixkit.FlowInteractionTemplate{
FType: "InteractionTemplate",
FVersion: "1.0.0",
ID: "290b6b6222b2a77b16db896a80ddf29ebd1fa3038c9e6625a933fa213fce51fa",
Data: flixkit.Data{
Type: "script",
Interface: "",
Cadence: "pub fun main(): Int { return 1 }",
},
}

func TestJSGenTransaction(t *testing.T) {
generator := FclJSGenerator{
TemplateDir: "./templates",
Expand Down Expand Up @@ -217,3 +228,11 @@ func TestJSGenMinScript(t *testing.T) {
assert.NoError(err, "ParseTemplate should not return an error")
autogold.ExpectFile(t, out)
}
func TestJSGenNoParamsScript(t *testing.T) {
generator := NewFclJSGenerator()
assert := assert.New(t)

out, err := generator.Generate(minimumNoParamTemplate, "./min.template.json", true)
assert.NoError(err, "ParseTemplate should not return an error")
autogold.ExpectFile(t, out)
}
9 changes: 5 additions & 4 deletions bindings/templates/js_fcl_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import flixTemplate from "{{.Location}}"
{{- else}}
const flixTemplate = "{{.Location}}"
{{- end}}

{{"\n"}}/**
* {{.Description}}{{"\n"}}
{{- if gt (len .Parameters) 0 -}}
* @param {Object} Parameters - parameters for the cadence
{{- range $index, $ele := .Parameters -}}
{{"\n"}}* @param {{"{"}} {{$ele.JsType}} {{"}"}} Parameters.{{$ele.Name}} - {{$ele.Description}}: {{$ele.CadType}}
{{"\n"}}* @param {{"{"}}{{$ele.JsType}}{{"}"}} Parameters.{{$ele.Name}} - {{$ele.Description}}: {{$ele.CadType}}
{{- end -}}
{{ end -}}
{{ else -}}
* No parameters needed.
{{- end -}}
{{- if not .IsScript -}}
{{"\n"}}* @returns {Promise<string>} - returns a promise which resolves to the transaction id
{{- end -}}
{{"\n"}}*/
{{- "\n"}}*/
{{if .IsScript}}
{{- template "js_fcl_script.tmpl" .}}
{{else}}
Expand Down
6 changes: 4 additions & 2 deletions bindings/templates/js_fcl_script.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export async function {{.Title}}({
export async function {{.Title}}(
{{- if len .Parameters -}}
{
{{- range $index, $ele := .Parameters -}}
{{if $index}}, {{end}}{{.Name}}
{{- end -}}
}
{{- end -}}
}) {
) {
const info = await fcl.query({
template: flixTemplate,
{{ if len .Parameters -}}
Expand Down
3 changes: 1 addition & 2 deletions bindings/testdata/TestJSGenArrayScript.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import * as fcl from "@onflow/fcl"
import flixTemplate from "./multiply-numbers.template.json"


/**
* Multiply numbers in an array
* @param {Object} Parameters - parameters for the cadence
* @param { Array<string> } Parameters.numbers - Array of numbers to be multiplied: Int
* @param {Array<string>} Parameters.numbers - Array of numbers to be multiplied: Int
*/
export async function multiplyNumbers({numbers}) {
const info = await fcl.query({
Expand Down
3 changes: 1 addition & 2 deletions bindings/testdata/TestJSGenMinScript.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import * as fcl from "@onflow/fcl"
import flixTemplate from "./min.template.json"


/**
*
* @param {Object} Parameters - parameters for the cadence
* @param { Array<string> } Parameters.numbers - : Int
* @param {Array<string>} Parameters.numbers - : Int
*/
export async function request({numbers}) {
const info = await fcl.query({
Expand Down
27 changes: 27 additions & 0 deletions bindings/testdata/TestJSGenNoParamsScript.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
`/**
This binding file was auto generated based on FLIX template v1.0.0.
Changes to this file might get overwritten.
Note fcl version 1.3.0 or higher is required to use templates.
**/

import * as fcl from "@onflow/fcl"
import flixTemplate from "./min.template.json"

/**
*
* No parameters needed.
*/
export async function request() {
const info = await fcl.query({
template: flixTemplate,

});

return info
}





`
5 changes: 2 additions & 3 deletions bindings/testdata/TestJSGenScript.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import * as fcl from "@onflow/fcl"
import flixTemplate from "./multiply_two_integers.template.json"


/**
* Multiply two numbers to another
* @param {Object} Parameters - parameters for the cadence
* @param { string } Parameters.x - number to be multiplied: Int
* @param { string } Parameters.y - second number to be multiplied: Int
* @param {string} Parameters.x - number to be multiplied: Int
* @param {string} Parameters.y - second number to be multiplied: Int
*/
export async function multiplyTwoIntegers({x, y}) {
const info = await fcl.query({
Expand Down
5 changes: 2 additions & 3 deletions bindings/testdata/TestJSGenTransaction.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import * as fcl from "@onflow/fcl"
import flixTemplate from "./transfer_token.json"


/**
* Transfer tokens from one account to another
* @param {Object} Parameters - parameters for the cadence
* @param { string } Parameters.amount - The amount of FLOW tokens to send: UFix64
* @param { string } Parameters.to - The Flow account the tokens will go to: Address
* @param {string} Parameters.amount - The amount of FLOW tokens to send: UFix64
* @param {string} Parameters.to - The Flow account the tokens will go to: Address
* @returns {Promise<string>} - returns a promise which resolves to the transaction id
*/
export async function transferTokens({amount, to}) {
Expand Down
2 changes: 1 addition & 1 deletion flixkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type Generator interface {
Generate(flix *FlowInteractionTemplate, templateLocation string, isLocal bool) (string, error)
Generate(ctx context.Context, code string, preFill *FlowInteractionTemplate) (*FlowInteractionTemplate, error)
}

type FlixService interface {
Expand Down
101 changes: 101 additions & 0 deletions generator/core_contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package generator

import (
"github.com/onflow/flixkit-go"
)

func GetDefaultCoreContracts() flixkit.Contracts {
return flixkit.Contracts{
"FungibleToken": {
"mainnet": {
Address: "0xf233dcee88fe0abe",
FqAddress: "A.0xf233dcee88fe0abe.FungibleToken",
Contract: "FungibleToken",
Pin: "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5",
PinBlockHeight: 34166296,
},
"testnet": {
Address: "0x9a0766d93b6608b7",
FqAddress: "A.0x9a0766d93b6608b7.FungibleToken",
Contract: "FungibleToken",
Pin: "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5",
PinBlockHeight: 74776482,
},
},
"NonFungibleToken": {
"mainnet": {
Address: "0x1d7e57aa55817448",
FqAddress: "A.0x1d7e57aa55817448.NonFungibleToken",
Contract: "NonFungibleToken",
Pin: "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5",
PinBlockHeight: 47509012,
},
"testnet": {
Address: "0x631e88ae7f1d7c20",
FqAddress: "A.0x631e88ae7f1d7c20.NonFungibleToken",
Contract: "NonFungibleToken",
Pin: "83c9e3d61d3b5ebf24356a9f17b5b57b12d6d56547abc73e05f820a0ae7d9cf5",
PinBlockHeight: 95808228,
},
},
"MetadataViews": {
"mainnet": {
Address: "0x1d7e57aa55817448",
FqAddress: "A.0x1d7e57aa55817448.NonFungibleToken",
Contract: "MetadataViews",
Pin: "ba061d95016d5506e9f5d1afda15d82eb066aa8b0552e8b26dc7950fa5714d51",
PinBlockHeight: 47487348,
},
"testnet": {
Address: "0x631e88ae7f1d7c20",
FqAddress: "A.0x631e88ae7f1d7c20.NonFungibleToken",
Contract: "MetadataViews",
Pin: "ba061d95016d5506e9f5d1afda15d82eb066aa8b0552e8b26dc7950fa5714d51",
PinBlockHeight: 95782517,
},
},
"FlowToken": {
"mainnet": {
Address: "0x1654653399040a61",
FqAddress: "A.0x1654653399040a61.FlowToken",
Contract: "FlowToken",
Pin: "0326c320322c4e8dde768ba2975c384184fb7e41765c2c87e79a2040bfc71be8",
PinBlockHeight: 47509023,
},
"testnet": {
Address: "0x7e60df042a9c0868",
FqAddress: "A.0x7e60df042a9c0868.FlowToken",
Contract: "FlowToken",
Pin: "0326c320322c4e8dde768ba2975c384184fb7e41765c2c87e79a2040bfc71be8",
PinBlockHeight: 95808240,
},
},
}
}

// GetContractInformation returns the contract information for a given contract name
// todo: this information should be generated so it can be updated easily
// todo: this should be moved to a separate package or maybe this inforation already exists somewhere else
func GetContractInformation(contractName string, deployedContracts []flixkit.Contracts, coreContracts flixkit.Contracts) flixkit.Networks {
contracts := make(flixkit.Contracts)

for contractName, network := range coreContracts {
contracts[contractName] = network
}

// add deployed contracts to contracts map
for _, contract := range deployedContracts {
for contractName, network := range contract {
contracts[contractName] = network
}
}

// To lookup a specific entry:
_, exists := contracts[contractName]
if exists {
return contracts[contractName]
}

return nil

}
31 changes: 31 additions & 0 deletions generator/core_contracts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package generator

import "testing"

func TestContractExist(t *testing.T) {
tests := []struct {
name string
input string
output bool
}{
{
name: "existing contract",
input: "FungibleToken",
output: true,
},
{
name: "non-existing contract",
input: "NonExistingContract",
output: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetContractInformation(tt.input, nil, GetDefaultCoreContracts())
if tt.output && got == nil {
t.Errorf("ContractExist() got = %v, want %v", got, tt.output)
}
})
}
}
Loading