Skip to content

Commit

Permalink
Merge pull request #25 from onflow/new-gen-v1.1
Browse files Browse the repository at this point in the history
Add support for FLIX v1.1
  • Loading branch information
bthaile authored Dec 18, 2023
2 parents 0e81dc8 + 58b5633 commit e8284d6
Show file tree
Hide file tree
Showing 59 changed files with 4,333 additions and 2,645 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @chasefleming @bjartek
* @chasefleming @bjartek @bthaile
92 changes: 74 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@

> 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.
The `flixkit` package is a Go module designed to interact with Flow Interaction Templates (FLIX). It allows users to fetch, parse, and handle Flow interaction templates.
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

The package provides a range of structs to represent data fetched from FLIX service:
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

- `Network`: Contains information about a specific network like address, contract and pin.
- `Argument`: Represents the arguments that can be given to the contracts.
- `Title`, `Description`: Used for i18n (internationalization) purposes in messages.
- `Messages`: Contains a title and a description.
- `Data`: Provides detailed information about the Flow interaction template like type, interface, messages, dependencies and arguments.
- `FlowInteractionTemplate`: The main struct that contains all the details of a flow interaction template.
This package provides three functionalities.
- Getting network specific Cadence from FLIX
- Generate FLIX from Cadence
- Create binding files based on FLIX (javascript, typescript)

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

## 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 `FlixService` interface provides the following methods:

- `GetFlixRaw(ctx context.Context, templateName string) (string, error)`: Fetches a raw Flix template by its name.
- `GetFlix(ctx context.Context, templateName string) (*FlowInteractionTemplate, error)`: Fetches and parses a Flix template by its name.
- `GetFlixByIDRaw(ctx context.Context, templateID string) (string, error)`: Fetches a raw Flix template by its ID.
- `GetFlixByID(ctx context.Context, templateID string) (*FlowInteractionTemplate, error)`: Fetches and parses a Flix template by its ID.
- `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.

Each `FlowInteractionTemplate` instance also provides the following methods:
- 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.

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 with their respective network addresses.
- `GetAndReplaceCadenceImports(networkName string) (string, error)`: Replaces cadence import statements in the cadence script or transaction with their respective network addresses.

## Examples

Expand All @@ -43,7 +45,7 @@ Here is a simple example of creating a new FlixService and fetching a template:
```go
flixService := flixkit.NewFlixService(&flixkit.Config{})

template, err := flixService.GetFlix(context.Background(), "transfer-flow")
template, err := flixService.GetTemplate(context.Background(), "transfer-flow")
if err != nil {
log.Fatal(err)
}
Expand All @@ -66,14 +68,14 @@ The `bindings` module has two public methods `Generate` and `NewFclJSGenerator`.


- `NewFclJSGenerator() *FclJSGenerator` // uses default fcl-js vanilla javascript
- `Generate(flix *flixkit.FlowInteractionTemplate, templateLocation string, isLocal bool) (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
- `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

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

output, err := fclJsGen.Generate(template, flixQuery, isLocal)
if err != nil {
Expand All @@ -83,4 +85,58 @@ if err != nil {
// output is the javascript binding code
fmt.Println(output])

```
```

## Generate

> 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`.
- `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

### Example
```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)

fmt.Println(prettyJSON)
```
### Cadence docs pragma

> Using Cadence pragma the metadata can live 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: "Reciever",
description: "Destination address to receive Flow Tokens"
)
],
)

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

The pragma describes the transaction parameters and reason for the transaction.
135 changes: 0 additions & 135 deletions bindings/fcl-js.go

This file was deleted.

Loading

0 comments on commit e8284d6

Please sign in to comment.