Skip to content

Commit da0d829

Browse files
committed
fixed schedule transactions
1 parent 3b046d8 commit da0d829

61 files changed

Lines changed: 3882 additions & 354 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
6 KB
Binary file not shown.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
alwaysApply: true
3+
---
4+
5+
# Cursor Agent Rules
6+
7+
These rules guide AI agents to generate correct, safe Cadence transactions using Scheduled Transactions.
8+
9+
## Required flow-cli version
10+
11+
Required: flow-cli 2.7.2 (scheduled transactions support)
12+
13+
- Verify:
14+
15+
```bash
16+
flow version
17+
```
18+
19+
- If your version is older than 2.7.2, update before proceeding:
20+
21+
```bash
22+
# macOS (Homebrew)
23+
brew update && brew upgrade flow-cli
24+
25+
# Or reinstall per docs
26+
# https://developers.flow.com/tools/flow-cli/install
27+
```
28+
29+
Proceed only when `flow version` reports 2.7.2.
30+
31+
## Imports
32+
33+
Always use string imports:
34+
35+
```cadence
36+
import "FungibleToken"
37+
import "FlowTransactionScheduler"
38+
```
39+
40+
## Required Params (Scheduled Transactions)
41+
42+
- `timestamp: UFix64` (0 for immediate execution)
43+
- `priority: UInt8` (0=High, 1=Medium, 2=Low)
44+
- `executionEffort: UInt64` (minimum 10)
45+
- `handlerStoragePath: StoragePath`
46+
- `transactionData: AnyStruct?` (max 100 bytes)
47+
48+
## Transaction Skeleton (Scheduled Transactions)
49+
50+
- Prepare:
51+
- Convert priority number to `FlowTransactionScheduler.Priority` enum.
52+
- Validate timestamp (future or 0 for immediate).
53+
- Estimate fees with `FlowTransactionScheduler.estimate()`.
54+
- Withdraw fees from user's FlowToken vault.
55+
- Issue handler capability: `Capability<auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}>`.
56+
- Execute:
57+
- Call `FlowTransactionScheduler.schedule()` with all parameters.
58+
- Optionally save `ScheduledTransaction` receipt for future reference/cancellation.
59+
- Post:
60+
- Verify transaction scheduled successfully (check events or receipt ID).
61+
62+
## Prompt-to-Params Examples
63+
64+
- "Schedule transaction to execute in 1 hour with high priority" →
65+
66+
- `timestamp = getCurrentBlock().timestamp + 3600.0`
67+
- `priority = 0` (High)
68+
- `executionEffort = 1000` (moderate effort)
69+
- `handlerStoragePath` from user specification
70+
- `transactionData = nil` unless user provides specific data.
71+
72+
- "Schedule recurring payments every day for a week" →
73+
- Multiple transaction transactions with `timestamp` incremented by 86400.0 (1 day)
74+
- `priority = 1` (Medium) for cost efficiency
75+
- `transactionData` containing payment details.
76+
77+
## Code Style
78+
79+
- Use named arguments.
80+
- Prefer early returns and minimal nesting inside connector implementations (transactions use assertions instead).
81+
82+
## Sanity Checklist
83+
84+
- Imports present and string-based.
85+
- Capability issuance and borrows succeed or `panic` with context.
86+
- For scheduled transactions: fee estimation before scheduling, timestamp validation, handler capability verification.
87+
- For recurring transactions: consider fee accumulation and batch scheduling efficiency.
88+
89+
## Development Guidelines
90+
91+
- Use string imports: `import "FlowTransactionScheduler"`, `import "FlowToken"`, `import "FungibleToken"`
92+
- Emulator only; start with: `flow emulator`
93+
- Estimate before schedule: `FlowTransactionScheduler.estimate(...)`
94+
- Issue handler capability with correct entitlement: `auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}`
95+
- Save `ScheduledTransaction` if you will need to cancel later; call `FlowTransactionScheduler.cancel(transaction: receipt)` to cancel
96+
97+
## Key Documentation
98+
99+
- **Index**: [`index.md`](./index.md) – Navigation and core contracts reference
100+
- **Agent Rules**: [`agent-rules.mdc`](./agent-rules.mdc) – Cursor agent guidance for generating transactions
101+
- **Quick Checklist**: [`quick-checklist.md`](./quick-checklist.md) – Essential implementation checklist
102+
- **FLIP 330**: [`flip.md`](./flip.md) – Scheduled Transactions specification and details

scheduledtransactions-scaffold/.cursor/rules/scheduledtransactions/flip.md

Lines changed: 463 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 📘 ScheduledTransactions Documentation Index
2+
3+
> **AI Agent Navigation**: This documentation is structured for optimal crawling and indexing. Each file has a clear purpose and defined scope. Cross-references use consistent anchor linking patterns.
4+
5+
## ⚡ Quick Links
6+
7+
- Scheduled Transactions FLIP: [`flip.md`](./flip.md)
8+
- Agent Guidance: [`agent-rules.mdc`](./agent-rules.mdc)
9+
- Checklist: [`quick-checklist.md`](./quick-checklist.md)
10+
11+
## 🛡️ Safety & Implementation
12+
13+
- [`quick-checklist.md`](./quick-checklist.md) – Essential implementation checklist & AI scaffold
14+
- [`agent-rules.md`](./agent-rules.md) – Cursor agent guidance for generating transactions
15+
16+
## 🛠️ Development Resources
17+
18+
- [`flip.md`](./flip.md) – FLIP 330: Scheduled Transactions specification and implementation details
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Quick Checklist
2+
3+
## Imports
4+
5+
- Use `import "ContractName"` format only.
6+
- Include all required contract imports.
7+
8+
## Preconditions/Postconditions
9+
10+
- Single boolean expression per pre/post block.
11+
- Use `assert()` for multi-step validation in execute.
12+
13+
## Capabilities & Addresses
14+
15+
- Validate capabilities before use.
16+
- Pass addresses as parameters only when you must resolve third-party capabilities directly.
17+
- For scheduled transactions: verify handler capability exists and is properly authorized.
18+
19+
## Test
20+
21+
- Zero amounts and `UFix64.max`
22+
- Invalid capabilities
23+
- For scheduled transactions: invalid timestamps (past), insufficient fees, missing handlers
24+
25+
## Links
26+
27+
- Agent Rules: [`agent-rules.mdc`](./agent-rules.mdc)
28+
- Scheduled Transactions FLIP: [`flip.md`](./flip.md)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# flow
2+
emulator-account.pkey
3+
.env
4+
5+
# Pay attention to imports directory
6+
!imports/**
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# flow
2+
imports
3+
.env
4+
*.pkey
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cadence.customConfigPath": "./flow.json"
3+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Scheduled Transactions Demo: Increment the Counter
2+
3+
This example shows how to schedule a transaction that increments the `Counter` in the near future and verify it on the Flow Emulator using the scheduler manager.
4+
5+
## Files used
6+
7+
- `cadence/contracts/Counter.cdc`
8+
- `cadence/contracts/CounterTransactionHandler.cdc`
9+
- `cadence/transactions/InitSchedulerManager.cdc`
10+
- `cadence/transactions/InitCounterTransactionHandler.cdc`
11+
- `cadence/transactions/ScheduleIncrementIn.cdc`
12+
- `cadence/scripts/GetCounter.cdc`
13+
14+
## Prerequisites
15+
16+
```bash
17+
flow deps install
18+
```
19+
20+
## 1) Start the emulator with Scheduled Transactions
21+
22+
```bash
23+
flow emulator --block-time 1s
24+
```
25+
26+
Keep this running. Open a new terminal for the next steps.
27+
28+
## 2) Deploy contracts
29+
30+
```bash
31+
flow project deploy --network emulator
32+
```
33+
34+
This deploys `Counter` and `CounterTransactionHandler` (see `flow.json`).
35+
36+
## 3) Initialize the scheduler manager (if not already done)
37+
38+
The scheduler manager is now integrated into the scheduling transactions, so this step is optional. The manager will be created automatically when you schedule your first transaction.
39+
40+
If you want to initialize it separately:
41+
42+
```bash
43+
flow transactions send cadence/transactions/InitSchedulerManager.cdc \
44+
--network emulator \
45+
--signer emulator-account
46+
```
47+
48+
## 4) Initialize the handler capability
49+
50+
Saves a handler resource at `/storage/CounterTransactionHandler` and issues the correct capability for the scheduler.
51+
52+
```bash
53+
flow transactions send cadence/transactions/InitCounterTransactionHandler.cdc \
54+
--network emulator \
55+
--signer emulator-account
56+
```
57+
58+
## 5) Check the initial counter
59+
60+
```bash
61+
flow scripts execute cadence/scripts/GetCounter.cdc --network emulator
62+
```
63+
64+
Expected: `Result: 0`
65+
66+
## 6) Schedule an increment in ~2 seconds
67+
68+
Uses `ScheduleIncrementIn.cdc` to compute a future timestamp relative to the current block. This transaction will automatically create the scheduler manager if it doesn't exist.
69+
70+
```bash
71+
flow transactions send cadence/transactions/ScheduleIncrementIn.cdc \
72+
--network emulator \
73+
--signer emulator-account \
74+
--args-json '[
75+
{"type":"UFix64","value":"2.0"},
76+
{"type":"UInt8","value":"1"},
77+
{"type":"UInt64","value":"1000"},
78+
{"type":"Optional","value":null}
79+
]'
80+
```
81+
82+
Notes:
83+
84+
- Priority `1` = Medium. You can use `0` = High or `2` = Low.
85+
- `executionEffort` must be >= 10 (1000 is a safe example value).
86+
- With `--block-time 1s`, blocks seal automatically; after ~3 seconds your scheduled transaction should execute.
87+
- The transaction uses the scheduler manager to track and manage the scheduled transaction.
88+
89+
## 7) Verify the counter incremented
90+
91+
```bash
92+
flow scripts execute cadence/scripts/GetCounter.cdc --network emulator
93+
```
94+
95+
Expected: `Result: 1`
96+
97+
## Troubleshooting
98+
99+
- Invalid timestamp error: use `ScheduleIncrementIn.cdc` with a small delay (e.g., 2.0) so the timestamp is in the future.
100+
- Missing FlowToken vault: on emulator the default account has a vault; if you use a custom account, initialize it accordingly.
101+
- Manager not found: The scheduler manager is automatically created in the scheduling transactions. If you see this error, ensure you're using the latest transaction files.
102+
- More docs: see `/.cursor/rules/scheduledtransactions/index.md`, `agent-rules.mdc`, and `flip.md` in this repo.

0 commit comments

Comments
 (0)