Flow Interaction Templates (FLIX)
Introduction
The Flow CLI provides a flix
command with a few sub commands execute
and package
. Get familiar with Flow Interaction Templates (FLIX). FLIX are a standard for distributing Cadence scripts and transactions, and metadata in a way that is consumable by tooling and wallets. FLIX can be audited for correctness and safety by auditors in the ecosystem.
_10>flow flix_10execute, generate, package_10_10Usage:_10 flow flix [command]_10_10Available Commands:_10 execute execute FLIX template with a given id, name, local filename, or url_10 generate generate FLIX json template given local Cadence filename_10 package package file for FLIX template fcl-js is default
Execute
The Flow CLI provides a flix
command to execute
FLIX. The Cadence being execute in the FLIX can be a transaciton or script.
_10flow flix execute <query> [<argument> <argument>...] [flags]
Queries can be a FLIX id
, name
, url
or path
to a local FLIX file.
Execute Usage
_10# Execute a FLIX transaction by name on Testnet_10flow flix execute transfer-flow 5.0 "0x123" --network testnet --signer "testnet-account"
_10# Execute a FLIX script by id on Testnet_10flow flix execute bd10ab0bf472e6b58ecc0398e9b3d1bd58a4205f14a7099c52c0640d9589295f --network testnet
_10# Execute a local FLIX script by path on Testnet_10flow flix execute ./multiply.template.json 2 3 --network testnet
The Flow CLI provides a flix
command to package
up generated plain and simple JavaScript. This JavaScript uses FCL (Flow Client Library) to call the cadence the Flow Interaction Templates (FLIX) is based on.
Currently, flix package
command only supports generating FCL (Flow Client Library) specific JavaScript, there are plans to support other languages like golang.
_10flow flix package <query> [flags]
Generate
Generate FLIX json file. This command will take in a Cadence file and produce a FLIX json file. There are two ways to provide metadata to populate the FLIX json structure.
- Use
--pre-fill
flag to pass in a pre populated FLIX json structure
Generate Usage
_10# Generate FLIX json file using cadence transaction or script, this example is not using a prefilled json file so will not have associated message metadata _10flow flix generate cadence/transactions/update-helloworld.cdc --save cadence/templates/update-helloworld.template.json
Example of Cadence simple, no metadata associated
_11import "HelloWorld"_11transaction(greeting: String) {_11_11 prepare(acct: AuthAccount) {_11 log(acct.address)_11 }_11_11 execute {_11 HelloWorld.updateGreeting(newGreeting: greeting)_11 }_11}
Resulting json when no prefill json is used
_32{_32 "f_type": "InteractionTemplate",_32 "f_version": "1.0.0",_32 "id": "f5873ad5c845458619f2781e085a71d03ed9e8685ca6e1cfff8e139645227360",_32 "data": {_32 "type": "transaction",_32 "interface": "",_32 "messages": {},_32 "cadence": "import HelloWorld from 0xHelloWorld\ntransaction(greeting: String) {\n\n prepare(acct: AuthAccount) {\n log(acct.address)\n }\n\n execute {\n HelloWorld.updateGreeting(newGreeting: greeting)\n }\n}\n",_32 "dependencies": {_32 "0xHelloWorld": {_32 "HelloWorld": {_32 "testnet": {_32 "address": "0xa58395c2f736c46e",_32 "fq_address": "A.a58395c2f736c46e.HelloWorld",_32 "contract": "HelloWorld",_32 "pin": "82d8fb62ec356884316c28388630b9acb6ba5027d566efe0d2adff2c6e74b4dc",_32 "pin_block_height": 132414699_32 }_32 }_32 }_32 },_32 "arguments": {_32 "greeting": {_32 "index": 0,_32 "type": "String",_32 "messages": {},_32 "balance": ""_32 }_32 }_32 }_32}
_10# Generate FLIX json file using cadence transaction or script passing in a pre filled FLIX json file. The json file will get filled out by the `flow flix generate` command_10flow flix generate cadence/scripts/read-helloworld.cdc --pre-fill cadence/templates/read-helloworld.prefill.json --save cadence/templates/read-helloworld.template.json
Example of json prefill file with message metadata
_39{_39 "f_type": "InteractionTemplate",_39 "f_version": "1.0.0",_39 "id": "",_39 "data": {_39 "type": "transaction",_39 "interface": "",_39 "messages": {_39 "title": {_39 "i18n": {_39 "en-US": "Update Greeting"_39 }_39 },_39 "description": {_39 "i18n": {_39 "en-US": "Update greeting of the HelloWorld smart contract"_39 }_39 }_39 },_39 "cadence": "",_39 "dependencies": {},_39 "arguments": {_39 "greeting": {_39 "messages": {_39 "title": {_39 "i18n": {_39 "en-US": "Greeting"_39 }_39 },_39 "description": {_39 "i18n": {_39 "en-US": "HelloWorld contract greeting"_39 }_39 }_39 }_39 }_39 }_39 }_39}
Example of generated output using prefilled json
_54{_54 "f_type": "InteractionTemplate",_54 "f_version": "1.0.0",_54 "id": "7238aed3ce8588ba3603d7a0ad79bf3aa1f7848618a61ae93d6865aff15387b2",_54 "data": {_54 "type": "transaction",_54 "interface": "",_54 "messages": {_54 "title": {_54 "i18n": {_54 "en-US": "Update Greeting"_54 }_54 },_54 "description": {_54 "i18n": {_54 "en-US": "Update greeting of the HelloWorld smart contract"_54 }_54 }_54 },_54 "cadence": "import HelloWorld from 0xHelloWorld\ntransaction(greeting: String) {\n\n prepare(acct: AuthAccount) {\n log(acct.address)\n }\n\n execute {\n HelloWorld.updateGreeting(newGreeting: greeting)\n }\n}\n",_54 "dependencies": {_54 "0xHelloWorld": {_54 "HelloWorld": {_54 "testnet": {_54 "address": "0xa58395c2f736c46e",_54 "fq_address": "A.a58395c2f736c46e.HelloWorld",_54 "contract": "HelloWorld",_54 "pin": "82d8fb62ec356884316c28388630b9acb6ba5027d566efe0d2adff2c6e74b4dc",_54 "pin_block_height": 132414700_54 }_54 }_54 }_54 },_54 "arguments": {_54 "greeting": {_54 "index": 0,_54 "type": "String",_54 "messages": {_54 "title": {_54 "i18n": {_54 "en-US": "Greeting"_54 }_54 },_54 "description": {_54 "i18n": {_54 "en-US": "HelloWorld contract greeting"_54 }_54 }_54 },_54 "balance": ""_54 }_54 }_54 }_54}
Package
Queries can be a FLIX id
, name
, url
or path
to a local FLIX file. This command leverages FCL which will execute FLIX cadence code.
Package Usage
_10# Generate packaged code that leverages FCL to call the Cadence transaction code, `--save` flag will save the output to a specific file_10flow flix package transfer-flow --save ./package/transfer-flow.js
_10# Geneate package code for a FLIX script using id, since there is no saving file, the result will display in terminal_10flow flix package bd10ab0bf472e6b58ecc0398e9b3d1bd58a4205f14a7099c52c0640d9589295f
_10# Generate package code using local template file to save in a local file _10flow flix package ./multiply.template.json --save ./multiply.js
Example Package Output
_10flow flix package transfer-flow
_25_25/**_25 This binding file was auto generated based on FLIX template v1.0.0. _25 Changes to this file might get overwritten._25 Note fcl version 1.3.0 or higher is required to use templates. _25**/_25_25import * as fcl from "@onflow/fcl"_25const flixTemplate = "transfer-flow"_25_25/**_25* Transfer tokens from one account to another_25* @param {Object} Parameters - parameters for the cadence_25* @param {string} Parameters.amount - The amount of FLOW tokens to send: UFix64_25* @param {string} Parameters.to - The Flow account the tokens will go to: Address_25* @returns {Promise<string>} - returns a promise which resolves to the transaction id_25*/_25export async function transferTokens({amount, to}) {_25 const transactionId = await fcl.mutate({_25 template: flixTemplate,_25 args: (arg, t) => [arg(amount, t.UFix64), arg(to, t.Address)]_25 });_25_25 return transactionId_25}
Resources
To find out more about FLIX, see the read the FLIP.
For a list of all templates, check out the FLIX template repository.
To generate a FLIX, see the FLIX CLI readme.
Arguments
- Name:
argument
- Valid input: valid FLIX
Input argument value matching corresponding types in the source code and passed in the same order.
You can pass a nil
value to optional arguments by executing the flow FLIX execute script like this: flow flix execute template.json nil
.
Flags
Arguments JSON
- Flag:
--args-json
- Valid inputs: arguments in JSON-Cadence form.
- Example:
flow flix execute template.script.json '[{"type": "String", "value": "Hello World"}]'
Arguments passed to the Cadence script in the Cadence JSON format.
Cadence JSON format contains type
and value
keys and is
documented here.
Pre Fill
- Flag:
--pre-fill
- Valid inputs: a json file in the FLIX json structure FLIX json format
Block Height
- Flag:
--block-height
- Valid inputs: a block height number
Block ID
- Flag:
--block-id
- Valid inputs: a block ID
Signer
- Flag:
--signer
- Valid inputs: the name of an account defined in the configuration (
flow.json
)
Specify the name of the account that will be used to sign the transaction.
Proposer
- Flag:
--proposer
- Valid inputs: the name of an account defined in the configuration (
flow.json
)
Specify the name of the account that will be used as proposer in the transaction.
Payer
- Flag:
--payer
- Valid inputs: the name of an account defined in the configuration (
flow.json
)
Specify the name of the account that will be used as payer in the transaction.
Authorizer
- Flag:
--authorizer
- Valid inputs: the name of a single or multiple comma-separated accounts defined in the configuration (
flow.json
)
Specify the name of the account(s) that will be used as authorizer(s) in the transaction. If you want to provide multiple authorizers separate them using commas (e.g. alice,bob
)
Gas Limit
- Flag:
--gas-limit
- Valid inputs: an integer greater than zero.
- Default:
1000
Specify the gas limit for this transaction.
Host
- Flag:
--host
- Valid inputs: an IP address or hostname.
- Default:
127.0.0.1:3569
(Flow Emulator)
Specify the hostname of the Access API that will be
used to execute the command. This flag overrides
any host defined by the --network
flag.
Network Key
- Flag:
--network-key
- Valid inputs: A valid network public key of the host in hex string format
Specify the network public key of the Access API that will be used to create a secure GRPC client when executing the command.
Network
- Flag:
--network
- Short Flag:
-n
- Valid inputs: the name of a network defined in the configuration (
flow.json
) - Default:
emulator
Specify which network you want the command to use for execution.
Filter
- Flag:
--filter
- Short Flag:
-x
- Valid inputs: a case-sensitive name of the result property.
Specify any property name from the result you want to return as the only value.
Output
- Flag:
--output
- Short Flag:
-o
- Valid inputs:
json
,inline
Specify the format of the command results.
Save
- Flag:
--save
- Short Flag:
-s
- Valid inputs: a path in the current filesystem.
Specify the filename where you want the result to be saved
Log
- Flag:
--log
- Short Flag:
-l
- Valid inputs:
none
,error
,debug
- Default:
info
Specify the log level. Control how much output you want to see during command execution.
Configuration
- Flag:
--config-path
- Short Flag:
-f
- Valid inputs: a path in the current filesystem.
- Default:
flow.json
Specify the path to the flow.json
configuration file.
You can use the -f
flag multiple times to merge
several configuration files.
Version Check
- Flag:
--skip-version-check
- Default:
false
Skip version check during start up to speed up process for slow connections.