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

Update Stable Cadence feature branch with main #35

Merged
merged 23 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e6fd302
consistently format addresses to be used when generation dependencies
bthaile Jan 8, 2024
4ccac39
Merge pull request #29 from onflow/format-addresses-0x
bthaile Jan 8, 2024
16366b5
add default output if one does not exist, add output when generating …
bthaile Jan 9, 2024
c721b51
Merge pull request #30 from onflow/fix-output-add-default
bthaile Jan 10, 2024
6d5b607
impl binding generator interface, needed for mocking and better code …
bthaile Jan 10, 2024
9cfc115
update to not having breaking changes
bthaile Jan 11, 2024
f3fb8ad
Change codeowners
chasefleming Jan 11, 2024
70ef28c
update naming of interfaces to be more consistent
bthaile Jan 11, 2024
8731090
Merge pull request #32 from onflow/cf/change-codeowners
chasefleming Jan 11, 2024
ae405f9
add conv method
bthaile Jan 11, 2024
6cc3d9d
set methods private that are no longer public
bthaile Jan 12, 2024
0ecb51f
update
ianthpun Jan 12, 2024
53ccd60
Merge branch 'impl-interface-binding' into ianthpun/test
ianthpun Jan 12, 2024
fb7d76a
update
ianthpun Jan 12, 2024
3810160
update
ianthpun Jan 12, 2024
1eee459
restructuring for v2.0
bthaile Jan 15, 2024
5073f3c
add unit tests, fix issue with creating binding file from template na…
bthaile Jan 16, 2024
45a11e3
condensed all functionality into one flix service
bthaile Jan 16, 2024
d5b59fb
tweaks to support empty prefill template
bthaile Jan 16, 2024
b926de9
addressed PR comments
bthaile Jan 17, 2024
fcc06d7
update readme to reflex code changes
bthaile Jan 17, 2024
abbd351
Merge pull request #31 from onflow/impl-interface-binding
bthaile Jan 18, 2024
d566cfd
Merge branch 'main' into bastian/update-stable-cadence
turbolent Jan 31, 2024
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
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @chasefleming @bjartek @bthaile
* @bjartek @bthaile @ianthpun
156 changes: 94 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# FlixKit

> FlixKit is a Go package that provides functionalities for interacting with Flow Interaction Templates (aka FLIX). Please note that this package is currently in alpha and may undergo significant changes.
> FlixKit is a Go package that provides functionalities for interacting with Flow Interaction Templates (aka FLIX). This package supports generating v1.1 FLIX template json, creating binding files for v1.0 and v1.1 and replacing import for v1.0 and v1.1. More information about FLIX [FLIX FLIP](https://github.com/onflow/flips/blob/main/application/20230330-interaction-templates-1.1.0.md)

The `flixkit` package is a Go module designed to interact with Flow Interaction Templates (FLIX). It allows users to fetch, parse, generate and create binding files for Flow interaction templates aka FLIX, aka Verified Interactions.

## Structures

See FLIP that descibes json structure, [Here](https://github.com/onflow/flips/blob/main/application/20230330-interaction-templates-1.1.0.md) current version is v1.1.0
See FLIP that describes json structure, [Here](https://github.com/onflow/flips/blob/main/application/20230330-interaction-templates-1.1.0.md) current version is v1.1.0

This package provides three functionalities.
- Getting network specific Cadence from FLIX
Expand All @@ -15,28 +15,41 @@ This package provides three functionalities.

The package also defines the following interfaces:

- `FlixService`: This interface defines methods to interact with the FLIX service such as fetching raw data or parsed data by template name or template ID.
- `Generator`: This interface generates FLIX json given Cadence, metadata can be provided in two ways:
- prefilled out json
- Cadence docs in the form of a pragma
- `Bindings`: This interface has two implementations for javascript and typescript using fcl
- `FlixService`: This interface defines methods to fetch templates using template name or template id (from flix service), URL or local file path.

### Methods
```go
// GetTemplate returns the raw flix template
GetTemplate(ctx context.Context, templateName string) (string, string, error)
// GetAndReplaceImports returns the raw flix template with cadence imports replaced
GetTemplateAndReplaceImports(ctx context.Context, templateName string, network string) (*FlowInteractionTemplateExecution, error)
// GenerateBinding returns the generated binding given the language
GetTemplateAndCreateBinding(ctx context.Context, templateName string, lang string, destFile string) (string, error)
// GenerateTemplate returns the generated raw template
CreateTemplate(ctx context.Context, contractInfos ContractInfos, code string, preFill string) (string, error)
```

## Usage

The package provides a `FlixService` interface with a constructor function `NewFlixService(config *Config)`. `Config` contains `FlixServerURL` which should be provided. If no URL is provided, it defaults to `"https://flix.flow.com/v1/templates"`.
The package provides a `FlixService` interface with a constructor function `NewFlixService(config *FlixServiceConfig)`. `FlixServiceConfig`
contains
- `FlixServerURL` which is defaulted to `"https://flix.flow.com/v1/templates"`. User can provide their own service url endpoint
- `FileReader` which is used to read local FLIX json template files
- `Logger` which is used in creating `flowkit.NewFlowkit` for FLIX template generation

The `FlixService` interface provides the following methods:

- `GetTemplate(ctx context.Context, templateName string) (string, error)`: Fetches template and returns as a string.
- `GetAndReplaceCadenceImports(ctx context.Context, templateName string, network string) (*FlowInteractionTemplateExecution, error)`: Fetches and parses a Flix template and provides the cadence for the network provided.
- `GetTemplate`: Fetches template and returns as a string.
- `GetTemplateAndReplaceImports` returns `FlowInteractionTemplateExecution`: Fetches and parses a Flix template and provides the cadence for the network provided. There are two helper methods to assist in determining if the Cadence is a transaction or a script.

- Note: `templateName` parameter can be the id or name of a template from the interactive template service. A local file or url to the FLIX json file.
- Note: `templateName` parameter can be the id or name of a template from the interactive template service. A local file or url to the FLIX json file or the template string itself.

Result form GetAndReplaceCadenceImports is a `FlowInteractionTemplateExecution` instance also provides the following methods:

- `IsScript() bool`: Checks if the template is of type "script".
- `IsTransaction() bool`: Checks if the template is of type "transaction".
- `GetAndReplaceCadenceImports(networkName string) (string, error)`: Replaces cadence import statements in the cadence script or transaction with their respective network addresses.
- `Cadence`: Replaced cadence with respective network addresses.
- `Network`: Name of network used to get import addresses

## Examples

Expand All @@ -58,86 +71,105 @@ Note: Remember to replace "transfer-flow" with the actual name of the template y
To read more about Flow Interaction Templates, [see the docs](https://developers.flow.com/tooling/fcl-js/interaction-templates).


## Bindings
## Binding Files

> Binding files are code files that bind consuming code with FLIX. The `bindings` module in Flixkit generates code that calls the FLIX cadence code. FLIX cadence is primarily transactions and scripts.
> Binding files are client code files used to call Cadence contracts using the scripts or transactions in a FLIX. These client files can be created given a FLIX, currently TypeScript and JavaScript are supported.

### Usage

The `bindings` module has two public methods `Generate` and `NewFclJSGenerator`. `FclJSGenerator` takes a template directory. `bindings` has fcl-js templates.


- `NewFclJSGenerator() *FclJSGenerator` // uses default fcl-js vanilla javascript
- `Generate(template string, templateLocation string) (string, error)` // flix is the hydrated template struct, templateLocation is the file location of the flix json file, isLocal is a flag that indicates if the template is local or on remote server

### Example

```go
flixService := flixkit.NewFlixService(&flixkit.Config{
FileReader: myFileReader
})

// uses default fcl-js templates
fclJsGen := flixkit.NewFclJSGenerator()

output, err := fclJsGen.Generate(template, flixQuery, isLocal)
binding, err := flixService.GetTemplateAndCreateBinding(context.Background(), "transfer-flow", "js", "./bindingFiles/transferFlow.js")
if err != nil {
log.Fatal(err)
}

// output is the javascript binding code
fmt.Println(output])
fmt.Println(binding)
```

```go
GetTemplateAndCreateBinding(ctx context.Context, templateName string, lang string, destFile string) (string, error)
```

## Generate
- `templateName` value can be template name, template id, url or local file.
- `lang` values supported are "js", "javascript", "ts", "typescript"
- `destFile` is the location of the destination binding file, this is used to create the relative path if the template is local. If the template is a template name, template id or url `destFile` isn't used

> Generate creates the newest ratified version of FLIX, as of this update, v1.1 has been passed. Version 1.0.0 will be supported with `FlixService` and `bindings`.
## Generate Templates

- `deployedContracts` is an array of v1_1.Contract structs of the contract dependencies the Cadence code depends on, Core contracts are already configured, look in `internal/contracts/core.go` for details
> CreateTemplate creates the newest ratified version of FLIX, as of this update, see link to FLIP Flip above for more information.
```go
CreateTemplate(ctx context.Context, contractInfos ContractInfos, code string, preFill string) (string, error)
```

### Example
### Usage
```go
generator, err := flixkit.NewGenerator(deployedContracts, logger output.Logger)
// preFilledTemplate is a partially populated flix template with human readable messages
// see FLIX flip for details
prettyJSON, err := generator.Generate(ctx, string(code), preFilledTemplate)
flixService := flixkit.NewFlixService(&flixkit.Config{
FileReader: myFileReader,
Logger: myLogger,
})

prettyJSON, err := flixService.CreateTemplate(ctx, depContracts, string(code), preFilled)

fmt.Println(prettyJSON)
```
- `contractInfos` is an array of v1_1.Contract struct. This provides the network information about the deployed contracts that are dependencies in the FLIX Cadence code.
- `code` is the actual Cadence code the template is based on
- `preFilled` is a partially filled out FLIX template. This can be a template name, template id, url or local file. Alternatively to using a prefilled template, the Cadence itself can provide metadata using a FLIX specific Cadence pragma, more on that below, [See Cadence Doc Flip](https://github.com/onflow/flips/blob/main/application/20230406-interaction-template-cadence-doc.md)


### Cadence docs pragma

> Using Cadence pragma the metadata can live with the Cadence code. Therefore a prefilled template isn't necessary
> Using Cadence pragma the metadata can exist along with the Cadence code. Therefore a prefilled template isn't necessary

### Example

```go
#interaction(
version: "1.1.0",
title: "Transfer Flow",
description: "Transfer Flow to account",
language: "en-US",
parameters: [
Parameter(
name: "amount",
title: "Amount",
description: "Amount of Flow to transfer"
),
Parameter(
name: "to",
title: "Receiver",
description: "Destination address to receive Flow Tokens"
)
],
)

import "FlowToken"

transaction(amount: UFix64, to: Address) {

let vault: @FlowToken.Vault
version: "1.1.0",
title: "Transfer Flow",
description: "Transfer Flow to account",
language: "en-US",
)

prepare(signer: &Account) {
// ...
import "FlowToken"
transaction(amount: UFix64, to: Address) {
let vault: @FlowToken.Vault
prepare(signer: &Account) {
...
}
}
}
`

```

The pragma describes the transaction parameters and reason for the transaction.
A `pragma` gives special instruction or processors, in this case FLIX specific information that describes the transaction or script.

Notice: Nested structures in Cadence pragma will be supported in future, this will allow describing parameters
```go
...
#interaction(
version: "1.1.0",
title: "Transfer Flow",
description: "Transfer Flow to account",
language: "en-US",
parameters: [
Parameter(
name: "amount",
title: "Amount",
description: "Amount of Flow to transfer"
),
Parameter(
name: "to",
title: "Reciever",
description: "Destination address to receive Flow Tokens"
)
],
)
...
```
33 changes: 33 additions & 0 deletions flixkit/flix_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package flixkit

import (
"context"

"github.com/onflow/flixkit-go/internal"
)

type FlixService interface {
// GetTemplate returns the raw flix template
GetTemplate(ctx context.Context, templateName string) (string, string, error)
// GetAndReplaceImports returns the raw flix template with cadence imports replaced
GetTemplateAndReplaceImports(ctx context.Context, templateName string, network string) (*FlowInteractionTemplateExecution, error)
// GenerateBinding returns the generated binding given the language
GetTemplateAndCreateBinding(ctx context.Context, templateName string, lang string, destFile string) (string, error)
// GenerateTemplate returns the generated raw template
CreateTemplate(ctx context.Context, contractInfos ContractInfos, code string, preFill string) (string, error)
}

// FlowInteractionTemplateCadence is the interface returned from Replacing imports, it provides helper methods to assist in executing the resulting Cadence.
type FlowInteractionTemplateExecution = internal.FlowInteractionTemplateExecution

// ContractInfos is an input into generating a template, it is a map of contract name to network information of deployed contracts of the source Cadence code.
type ContractInfos = internal.ContractInfos
type NetworkAddressMap = internal.NetworkAddressMap

// FlixServiceConfig is the configuration for the FlixService that provides a override for FlixServerURL and default values for FileReader and Logger.
type FlixServiceConfig = internal.FlixServiceConfig

// NewFlixService returns a new FlixService given a FlixServiceConfig
func NewFlixService(config *FlixServiceConfig) FlixService {
return internal.NewFlixService(config)
}
Loading