Execute Scripts
It is often the case that you need to query current state of the network. For example, to check balance of the account, read public value of the contract or ensure that user has specific resource in their storage.
We abstract this interaction into single method called executeScript
. Method have 2 different signatures.
⚠️ Required: Your project must follow the required structure it must be initialized to use the following functions.
executeScript(props)
Provides explicit control over how you pass values.
Arguments
props
object accepts following fields:
Name | Type | Optional | Description |
---|---|---|---|
code | string | ✅ | string representation of Cadence script |
name | string | ✅ | name of the file in scripts folder to use (sans .cdc extension) |
args | [any] | ✅ | an array of arguments to pass to script. Optional if script does not expect any arguments. |
transformers | [CadenceTransformer] | ✅ | an array of operators to modify the code, before submitting it to network |
⚠️ Required: Either
code
orname
field shall be specified. Method will throw an error if both of them are empty. Ifname
field provided, framework will source code from file and override value passed viacode
field.
Returns
Type | Description |
---|---|
ResponseObject | Script result |
Usage
_29import path from "path"_29import {init, emulator, executeScript} from "@onflow/flow-js-testing"_29_29const main = async () => {_29 const basePath = path.resolve(__dirname, "../cadence")_29_29 // Init framework_29 init(basePath)_29 // Start emulator_29 await emulator.start()_29_29 // Define code and arguments we want to pass_29 const code = `_29 pub fun main(message: String): Int{_29 log(message)_29_29 return 42_29 }_29 `_29 const args = ["Hello, from Cadence"]_29_29 const [result, error, logs] = await executeScript({code, args})_29 console.log({result}, {error}, {logs})_29_29 // Stop emulator instance_29 await emulator.stop()_29}_29_29main()
executeScript(name: string, args: [any])
This signature provides simplified way of executing a script, since most of the time you will utilize existing Cadence files.
Arguments
Name | Type | Optional | Description |
---|---|---|---|
name | string | name of the file in scripts folder to use (sans .cdc extension) | |
args | [any] | ✅ | an array of arguments to pass to script. Optional if scripts don't expect any arguments. Default: [] |
Returns
Type | Description |
---|---|
ResponseObject | Script result |
Usage
_22import path from "path"_22import {init, emulator, executeScript} from "@onflow/flow-js-testing"_22_22const main = async () => {_22 const basePath = path.resolve(__dirname, "../cadence")_22_22 // Init framework_22 init(basePath)_22 // Start emulator_22 await emulator.start()_22_22 // Define arguments we want to pass_22 const args = ["Hello, from Cadence"]_22_22 // We assume there is a file `scripts/log-message.cdc` under base path_22 const [result, error, logs] = await executeScript("log-message", args)_22 console.log({result}, {error}, {logs})_22_22 await emulator.stop()_22}_22_22main()