onflow / flixkit-go

Apache License 2.0
0 stars 7 forks source link

FlixKit

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

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 describes json structure, Here current version is v1.1.0

This package provides three functionalities.

The package also defines the following interfaces:

Methods

// 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 *FlixServiceConfig). FlixServiceConfig contains

The FlixService interface provides the following methods:

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

Examples

Here is a simple example of creating a new FlixService and fetching a template:

flixService := flixkit.NewFlixService(&flixkit.Config{})

template, err := flixService.GetTemplate(context.Background(), "transfer-flow")
if err != nil {
    log.Fatal(err)
}

fmt.Println(template)

Note: Remember to replace "transfer-flow" with the actual name of the template you wish to fetch.

To read more about Flow Interaction Templates, see the docs.

Binding Files

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.

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

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

fmt.Println(binding)
GetTemplateAndCreateBinding(ctx context.Context, templateName string, lang string, destFile string) (string, error)

Generate Templates

CreateTemplate creates the newest ratified version of FLIX, as of this update, see link to FLIP Flip above for more information.

CreateTemplate(ctx context.Context, contractInfos ContractInfos, code string, preFill string) (string, error)

Usage

flixService := flixkit.NewFlixService(&flixkit.Config{
    FileReader: myFileReader,
    Logger: myLogger,
})

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

fmt.Println(prettyJSON)

Cadence docs pragma

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

Example

#interaction(
        version: "1.1.0",
        title: "Transfer Flow",
        description: "Transfer Flow to account",
        language: "en-US",
    )

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

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

...
#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"
            )
        ],
    )
...