mutate
A transaction execution function that allows you to submit Cadence transactions to the Flow blockchain to mutate on-chain state. This function handles the complete transaction lifecycle including building, signing, and sending transactions to Flow. It provides a high-level interface that abstracts the complexity of transaction construction while offering flexibility for advanced use cases.
The mutate function automatically handles authorization using the current authenticated user by default, but allows for custom authorization functions to be specified for different transaction roles (proposer, payer, authorizer). It supports both simple single-party transactions and complex multi-party transactions with different signatories.
This function integrates with FCL's address replacement system, allowing you to use placeholder addresses in your Cadence code that are replaced with actual addresses at execution time. It also supports Interaction Templates for standardized transaction execution patterns.
The mutate function accepts a configuration object with the following structure:
_10{_10cadence?: string, // The Cadence transaction code to execute (required if template not provided)_10args?: Function, // Function that returns an array of arguments for the transaction_10template?: any, // Interaction Template object or URL for standardized transactions_10limit?: number, // Compute (gas) limit for the transaction execution_10authz?: AccountAuthorization, // Authorization function for all signatory roles (proposer, payer, authorizer)_10proposer?: AccountAuthorization, // Specific authorization function for the proposer role_10payer?: AccountAuthorization, // Specific authorization function for the payer role_10authorizations?: AccountAuthorization[] // Array of authorization functions for authorizer roles_10}
Import
You can import the entire package and access the function:
_10import * as fcl from "@onflow/fcl"_10_10fcl.mutate(opts)
Or import directly the specific function:
_10import { mutate } from "@onflow/fcl"_10_10mutate(opts)
Usage
_30// Basic transaction submission_30import * as fcl from "@onflow/fcl"_30_30// Configure FCL first_30fcl.config({_30 "accessNode.api": "https://rest-testnet.onflow.org",_30 "discovery.wallet": "https://fcl-discovery.onflow.org/testnet/authn",_30 "flow.network": "testnet"_30})_30_30// Authenticate user_30await fcl.authenticate()_30_30// Submit a basic transaction_30const txId = await fcl.mutate({_30 cadence: `_30 transaction(message: String) {_30 prepare(account: AuthAccount) {_30 log("Transaction executed by: ".concat(account.address.toString()))_30 log("Message: ".concat(message))_30 }_30 }_30 `,_30 args: (arg, t) => [_30 arg("Hello Flow!", t.String)_30 ],_30 limit: 50_30})_30_30console.log("Transaction submitted:", txId)
Parameters
opts
- Type:
any
- Description: Transaction configuration options
Returns
_10(opts?: MutateOptions) => Promise<string>
Promise that resolves to the transaction ID (txId) when the transaction is submitted