Skip to main content

cadence

Creates a template function

Import

You can import the entire package and access the function:


_10
import * as sdk from "@onflow/sdk"
_10
_10
sdk.cadence(head, rest)

Or import directly the specific function:


_10
import { cadence } from "@onflow/sdk"
_10
_10
cadence(head, rest)

Usage


_30
import { template } from "@onflow/util-template"
_30
_30
// String template
_30
const simpleTemplate = template("Hello, World!");
_30
console.log(simpleTemplate()); // "Hello, World!"
_30
_30
// Template literal with interpolation
_30
const name = "Alice";
_30
const greeting = template`Hello, ${name}!`;
_30
console.log(greeting()); // "Hello, Alice!"
_30
_30
// Cadence script template
_30
const cadenceScript = template`
_30
access(all) fun main(greeting: String): String {
_30
return greeting.concat(", from Flow!")
_30
}
_30
`;
_30
console.log(cadenceScript()); // The Cadence script as a string
_30
_30
// Used with FCL for dynamic Cadence code
_30
import * as fcl from "@onflow/fcl";
_30
_30
const contractAddress = "0x123456789abcdef0";
_30
const scriptTemplate = fcl.cadence`
_30
import MyContract from ${contractAddress}
_30
_30
access(all) fun main(): String {
_30
return MyContract.getMessage()
_30
}
_30
`;

Parameters

  • Type: string | TemplateStringsArray | ((x?: unknown) => string)
  • Description: - A string, template string array, or template function

rest (optional)

  • Type: unknown[]
  • Description: - The rest of the arguments

Returns

(x?: unknown) => string


Rate this page