This project is a starting point for you to test out Scheduled Transactions on the Flow Blockchain. It comes with example contracts, scripts, transactions, and tests to help you get started.
Scheduled Transactions let smart contracts execute code at (or after) a chosen time without an external transaction. You schedule work now; the network executes it later. This enables recurring jobs, deferred actions, and autonomous workflows.
Core pieces:
- Capability to a handler implementing
FlowTransactionScheduler.TransactionHandler - Parameters:
timestamp,priority,executionEffort,fees, optionaltransactionData - Estimate first (
FlowTransactionScheduler.estimate), then schedule using the scheduler manager - The scheduler manager (
FlowTransactionSchedulerUtils.Manager) helps track and manage scheduled transactions
Here are some essential resources to help you hit the ground running:
- Flow Documentation - The official Flow Documentation is a great starting point to start learning about about building on Flow.
- Cadence Documentation - Cadence is the native language for the Flow Blockchain. It is a resource-oriented programming language that is designed for developing smart contracts. The documentation is a great place to start learning about the language.
- Visual Studio Code and the Cadence Extension - It is recommended to use the Visual Studio Code IDE with the Cadence extension installed. This will provide syntax highlighting, code completion, and other features to support Cadence development.
- Flow Clients - There are clients available in multiple languages to interact with the Flow Blockchain. You can use these clients to interact with your smart contracts, run transactions, and query data from the network.
- Block Explorers - Block explorers are tools that allow you to explore on-chain data. You can use them to view transactions, accounts, events, and other information. Flowser is a powerful block explorer for local development on the Flow Emulator.
Follow this to run the demo that schedules a transaction to increment the Counter.
- Ensure flow-cli 2.7.2
flow version
# If older than 2.7.2, update first: https://developers.flow.com/tools/flow-cli/install- Create
emulator-account.pkey
- Generate a key pair and copy the private key hex
flow keys generate
# Copy the "Private Key" value from the output (hex string)- Save the private key to
emulator-account.pkeyin the project root
printf "<PASTE_PRIVATE_KEY_HEX_HERE>" > emulator-account.pkey- Install deps and start emulator
flow deps install
flow emulator --block-time 1s- In a new terminal, deploy, init, schedule, verify
flow project deploy --network emulator
# Initialize handler (scheduler manager will be created automatically)
flow transactions send cadence/transactions/InitCounterTransactionHandler.cdc \
--network emulator --signer emulator-account
flow scripts execute cadence/scripts/GetCounter.cdc --network emulator
# Schedule increment (creates manager if needed)
flow transactions send cadence/transactions/ScheduleIncrementIn.cdc \
--network emulator --signer emulator-account \
--args-json '[
{"type":"UFix64","value":"2.0"},
{"type":"UInt8","value":"1"},
{"type":"UInt64","value":"1000"},
{"type":"Optional","value":null}
]'
# after ~3s
flow scripts execute cadence/scripts/GetCounter.cdc --network emulatorFor full details see EXAMPLE.md. For the continuous loop variant, see EXAMPLE-LOOP.md.
Note: The scheduler manager is automatically created when you schedule your first transaction, or you can initialize it separately using InitSchedulerManager.cdc.
Follow this to run the demo that continuously increments the Counter by rescheduling on each transaction execution.
Repeat steps 1–3 above, then in a new terminal run:
flow project deploy --network emulator
flow transactions send cadence/transactions/InitCounterLoopTransactionHandler.cdc \
--network emulator --signer emulator-account
flow scripts execute cadence/scripts/GetCounter.cdc --network emulator
flow transactions send cadence/transactions/ScheduleIncrementInLoop.cdc \
--network emulator --signer emulator-account \
--args-json '[
{"type":"UFix64","value":"2.0"},
{"type":"UInt8","value":"1"},
{"type":"UInt64","value":"1000"},
{"type":"Optional","value":null}
]'
# after ~3s, the transaction runs and schedules the next one automatically
flow scripts execute cadence/scripts/GetCounter.cdc --network emulatorFor full details and troubleshooting, see EXAMPLE-LOOP.md.
Follow this to run the demo that increments the Counter at precise intervals using cron-like scheduling (prevents drift).
Repeat steps 1–3 above, then in a new terminal run:
flow project deploy --network emulator
flow transactions send cadence/transactions/InitCounterCronTransactionHandler.cdc \
--network emulator --signer emulator-account
flow scripts execute cadence/scripts/GetCounter.cdc --network emulator
flow transactions send cadence/transactions/ScheduleIncrementInCron.cdc \
--network emulator --signer emulator-account \
--args-json '[
{"type":"UFix64","value":"3.0"},
{"type":"UInt8","value":"1"},
{"type":"UInt64","value":"1000"},
{"type":"Optional","value":{"type":"UInt64","value":"3"}},
{"type":"Optional","value":null}
]'
# after ~12s, check that it ran exactly 3 times at precise 3-second intervals
flow scripts execute cadence/scripts/GetCounter.cdc --network emulatorFor full details and troubleshooting, see EXAMPLE-CRON.md.
Your project has been set up with the following structure:
flow.json– Project configuration and dependency aliases (string-imports)/cadence– Your Cadence code/.cursor/rules/scheduledtransactions– Local documentation and agent guidance.
Inside the cadence folder you will find:
/contracts- This folder contains your Cadence contracts (these are deployed to the network and contain the business logic for your application)Counter.cdcCounterTransactionHandler.cdcCounterLoopTransactionHandler.cdcCounterCronTransactionHandler.cdc
/scripts- This folder contains your Cadence scripts (read-only operations)GetCounter.cdc
/transactions- This folder contains your Cadence transactions (state-changing operations)IncrementCounter.cdcInitSchedulerManager.cdc- Initialize the scheduler managerInitCounterTransactionHandler.cdcInitCounterLoopTransactionHandler.cdcInitCounterCronTransactionHandler.cdcScheduleIncrementIn.cdc- Schedule single incrementScheduleIncrementInLoop.cdc- Schedule looping incrementScheduleIncrementInCron.cdc- Schedule cron-like increment
/tests- This folder contains your Cadence tests (integration tests for your contracts, scripts, and transactions to verify they behave as expected)Counter_test.cdc
Docs and rules live under /.cursor/rules/scheduledtransactions:
index.md– Navigation and referencesagent-rules.mdc– Agent guidance for composing safe transaction transactionsquick-checklist.md– Essential checklistflip.md– FLIP-330: Scheduled Transactions
Other folders/files:
EXAMPLE.md– Step-by-step walkthrough to run the scheduled transactions demoEXAMPLE-LOOP.md– Step-by-step walkthrough for the continuous loop scheduled transactions demoEXAMPLE-CRON.md– Step-by-step walkthrough for the cron-like scheduled transactions demo (precise intervals)
If your flow.json references a key file for the emulator-account, create one and run the emulator with that same key so signing matches the service account. This is required; starting the emulator without the same private key will cause deploys/signing to fail.
- Generate a key pair and copy the private key hex
flow keys generate --sig-algo ECDSA_P256 --hash-algo SHA3_256
# Copy the "Private Key" value from the output (hex string)- Save the private key to
emulator-account.pkeyin the project root
printf "<PASTE_PRIVATE_KEY_HEX_HERE>" > emulator-account.pkeyTo add a new contract to your project, run the following command:
flow generate contractThis command will create a new contract file and add it to the flow.json configuration file.
To add a new script to your project, run the following command:
flow generate scriptThis command will create a new script file. Scripts are used to read data from the blockchain and do not modify state (i.e. get the current balance of an account, get a user's NFTs, etc).
You can import any of your own contracts or installed dependencies in your script file using the import keyword. For example:
import "Counter"To add a new transaction to your project you can use the following command:
flow generate transactionThis command will create a new transaction file. Transactions are used to modify the state of the blockchain (i.e purchase an NFT, transfer tokens, etc).
You can import any dependencies as you would in a script file.
- Imports (string imports):
import "FlowTransactionScheduler",import "FlowTransactionSchedulerUtils",import "FlowToken",import "FungibleToken" - Required params when scheduling:
timestamp: UFix64(must be in the future; on emulator prefergetCurrentBlock().timestamp + smallDelta)priority: UInt8(0 = High, 1 = Medium, 2 = Low)executionEffort: UInt64(minimum 10)handlerCap: Capability<auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}>transactionData: AnyStruct?
- Flow: Initialize/borrow manager → Estimate → withdraw fees → schedule through manager
Minimal scheduling skeleton with manager:
import "FlowToken"
import "FungibleToken"
import "FlowTransactionScheduler"
import "FlowTransactionSchedulerUtils"
transaction(
timestamp: UFix64,
priority: UInt8,
executionEffort: UInt64,
handlerStoragePath: StoragePath,
transactionData: AnyStruct?
) {
prepare(signer: auth(BorrowValue, IssueStorageCapabilityController, SaveValue) &Account) {
let p = priority == 0
? FlowTransactionScheduler.Priority.High
: priority == 1
? FlowTransactionScheduler.Priority.Medium
: FlowTransactionScheduler.Priority.Low
// Get handler capability
let handlerCap = signer.capabilities.storage
.issue<auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}>(handlerStoragePath)
// Initialize manager if needed
if signer.storage.borrow<&AnyResource>(from: FlowTransactionSchedulerUtils.managerStoragePath) == nil {
let manager <- FlowTransactionSchedulerUtils.createManager()
signer.storage.save(<-manager, to: FlowTransactionSchedulerUtils.managerStoragePath)
}
// Borrow manager
let manager = signer.storage.borrow<auth(FlowTransactionSchedulerUtils.Owner) &{FlowTransactionSchedulerUtils.Manager}>(
from: FlowTransactionSchedulerUtils.managerStoragePath
) ?? panic("Could not borrow Manager")
let est = FlowTransactionScheduler.estimate(
data: transactionData,
timestamp: timestamp,
priority: p,
executionEffort: executionEffort
)
assert(
est.timestamp != nil || p == FlowTransactionScheduler.Priority.Low,
message: est.error ?? "estimation failed"
)
let vaultRef = signer.storage
.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("missing FlowToken vault")
let fees <- vaultRef.withdraw(amount: est.flowFee ?? 0.0) as! @FlowToken.Vault
// Schedule through manager
let transactionId = manager.schedule(
handlerCap: handlerCap,
data: transactionData,
timestamp: timestamp,
priority: p,
executionEffort: executionEffort,
fees: <-fees
)
log("Scheduled transaction id: ".concat(transactionId.toString()))
}
}If you want to use external contract dependencies (such as NonFungibleToken, FlowToken, FungibleToken, etc.) you can install them using Flow CLI Dependency Manager.
For example, to install the NonFungibleToken contract you can use the following command:
flow deps add mainnet://1d7e57aa55817448.NonFungibleTokenContracts can be found using ContractBrowser, but be sure to verify the authenticity before using third-party contracts in your project.
To verify that your project is working as expected you can run the tests using the following command:
flow testThis command will run all tests with the _test.cdc suffix (these can be found in the cadence/tests folder). You can add more tests here using the flow generate test command (or by creating them manually).
To learn more about testing in Cadence, check out the Cadence Test Framework Documentation.
To deploy your project to the Flow network, you must first have a Flow account and have configured your deployment targets in the flow.json configuration file.
You can create a new Flow account using the following command:
flow accounts createLearn more about setting up deployment targets in the Flow CLI documentation.
To deploy your project to the Flow Emulator, start the emulator with Scheduled Transactions enabled:
flow emulator --block-time 1sTo deploy your project, run the following command:
flow project deploy --network=emulatorThis command will start the Flow Emulator and deploy your project to it. You can now interact with your project using the Flow CLI or alternate client.
This repo is part of the Flow network, a Layer 1 blockchain built for consumer applications, AI agents, and DeFi at scale.
- Developer docs: https://developers.flow.com
- Cadence language: https://cadence-lang.org
- Community: Flow Discord · Flow Forum
- Governance: Flow Improvement Proposals