You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/forte/scheduled-transactions/index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ keywords:
15
15
16
16
# Scheduled Transactions Tutorials
17
17
18
-
This series covers how to implement scheduled transactions for time-based smart contract execution on Flow, enabling developers to create automated workflows and time-sensitive blockchain applications. These tutorials are part of the Forte network upgrade, which introduces new capabilities to the Flow blockchain.
18
+
This series covers how to implement scheduled transactions for time-based smart contract execution on Flow, which allows developers to create automated workflows and time-sensitive blockchain applications. These tutorials are part of the Forte network upgrade, which introduces new capabilities to the Flow blockchain.
Copy file name to clipboardExpand all lines: docs/blockchain-development-tutorials/forte/scheduled-transactions/scheduled-transactions-introduction.md
+55-53Lines changed: 55 additions & 53 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,42 +19,44 @@ keywords:
19
19
20
20
:::warning
21
21
22
-
Scheduled transactions are a new feature that is under development and is a part of [FLIP 330]. Currently, they only work in the emulator and testnet. The specific implementation is closed to being finished but may change as a part of the development process.
22
+
Scheduled transactions are a new feature that is under development and is a part of [FLIP 330]. Currently, they only work in the emulator and testnet. We're close to finishing the specific implementation, but it but may change during the development process.
23
23
24
-
These tutorials will be updated, but you may need to refactor your code if the implementation changes.
24
+
We will update these tutorials, but you may need to refactor your code if the implementation changes.
25
25
26
26
:::
27
27
28
-
Flow, EVM, and other blockchains are a form of a **single** shared computer that anyone can use and no one has admin privileges, super user roles, or complete control. For this to work, one of the requirements is that it needs to be impossible for any user to freeze the computer, on purpose or by accident.
28
+
# Overview
29
29
30
-
As a result, most blockchain computers, including EVM and Solana, are not [Turing Complete], because they can't run an unbounded loop. Each transaction must take place within one block, and cannot consume more gas than the limit.
30
+
Flow, EVM, and other blockchains are a form of a **single** shared computer that anyone can use, with no admin privileges, super user roles, or complete control. For this to work, it must be impossible for any user to freeze the computer, on purpose or by accident.
31
+
32
+
As a result, most blockchain computers, including EVM and Solana, aren't [Turing Complete], because they can't run an unbounded loop. Each transaction must occur within one block, and can't consume more gas than the limit.
31
33
32
34
While this limitation prevents infinite loops, it makes it so that you can't do anything 100% onchain if you need it to happen at a later time or after a trigger. As a result, developers must often build products that involve a fair amount of traditional infrastructure and requires users to give those developers a great amount of trust that their backend will execute the promised task.
33
35
34
-
Flow fixes this problem with **scheduled transactions**. 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.
36
+
Flow fixes this problem with _scheduled transactions_. Scheduled Transactions let smart contracts execute code at, or after, a chosen time without an external transaction. You schedule work now and the network executes it later. This allows recurring jobs, deferred actions, and autonomous workflows.
35
37
36
38
## Learning Objectives
37
39
38
-
After completing this tutorial, you will be able to:
40
+
After you complete this tutorial, you will be able to:
39
41
40
-
- Understand the concept of scheduled transactions and how they solve blockchain limitations
41
-
- Explain the key components of the `FlowTransactionScheduler` system
42
-
- Implement a basic scheduled transaction using the provided scaffold
43
-
- Analyze the structure and flow of scheduled transaction transactions
44
-
- Create custom scheduled transaction contracts and handlers
45
-
- Evaluate the benefits and use cases of scheduled transactions in DeFi applications
42
+
- Understand the concept of scheduled transactions and how they solve blockchain limitations.
43
+
- Explain the key components of the `FlowTransactionScheduler` system.
44
+
- Implement a basic scheduled transaction using the provided scaffold.
45
+
- Analyze the structure and flow of scheduled transaction transactions.
46
+
- Create custom scheduled transaction contracts and handlers.
47
+
- Evaluate the benefits and use cases of scheduled transactions in DeFi applications.
46
48
47
49
# Prerequisites
48
50
49
51
## Cadence Programming Language
50
52
51
-
This tutorial assumes you have a modest knowledge of [Cadence]. If you don't, you'll be able to follow along, but you'll get more out of it if you complete our series of [Cadence] tutorials. Most developers find it more pleasant than other blockchain languages and it's not hard to pick up.
53
+
This tutorial assumes you have a modest knowledge of [Cadence]. If you don't, you can follow along, but you'll get more out of it if you complete our series of [Cadence] tutorials. Most developers find it more pleasant than other blockchain languages, and it's not hard to pick up.
52
54
53
55
## Getting Started
54
56
55
-
Begin by running`flow init` and select `Scheduled Transactions project`. Open the project.
57
+
To start, run`flow init` and select `Scheduled Transactions project`. Open the project.
56
58
57
-
The readme has a robust getting started guide. Complete that to set up and run the demo scheduled transaction. It doesn't seem like much at first. The counter was at `0`, you ran a transaction, now it's at `1`. What's the big deal?
59
+
The `readme` file has a robust getting started guide. Complete that to set up and run the demo scheduled transaction. It doesn't seem like much at first. The counter was at `0`, you ran a transaction, now it's at `1`. What's the big deal?
58
60
59
61
Let's try again to make it clearer what's happening. Open `cadence/transactions/ScheduleIncrementIn.cdc` and look at the arguments for the transaction:
60
62
@@ -69,10 +71,10 @@ transaction(
69
71
70
72
The first parameter is the delay in seconds for the scheduled transaction. Let's try running it again. You'll need to be quick on the keyboard, so feel free to use a higher number of `delaySeconds` if you need to. You're going to:
71
73
72
-
1. Call the script to view the counter
73
-
2. Call the transaction to schedule the counter to increment after 10 seconds
74
-
3. Call the script to view the counter again and verify that it hasn't changed yet
75
-
4. Wait 10 seconds, call it again, and confirm the counter incremented
74
+
1. Call the script to view the counter.
75
+
2. Call the transaction to schedule the counter to increment after 10 seconds.
76
+
3. Call the script to view the counter again and verify that it hasn't changed yet.
77
+
4. Wait 10 seconds, call it again, and confirm the counter incremented.
76
78
77
79
For your convenience, the updated transaction call is:
78
80
@@ -137,7 +139,7 @@ Result: 3
137
139
138
140
### Review of the Existing Contract and Transactions
139
141
140
-
If you're not familiar with it, review `cadence/contracts/Counter.cdc`. This is the standard contract created by default when you run `flow init`. It's very simple, with a counter and public functions to increment or decrement it.
142
+
If you're not familiar with `cadence/contracts/Counter.cdc` review it. This is the standard contract created by default when you run `flow init`. It's very simple, with a counter and public functions to increment or decrement it.
This contract is simple. It contains a [resource] that has a function with the `FlowTransactionScheduler.Execute`[entitlement]. This function contains the code that will be called by the scheduled transaction. It:
185
+
This contract is simple. It contains a [resource] that has a function with the `FlowTransactionScheduler.Execute`[entitlement]. This function contains the code that the scheduled transaction calls. It:
184
186
185
-
1. Calls the `increment` function in the `Counter` contract
186
-
2. Fetches the current value in the counter
187
-
3. Logs that value to the console **for the emulator**
187
+
1. Calls the `increment` function in the `Counter` contract.
188
+
2. Fetches the current value in the counter.
189
+
3. Logs that value to the console **for the emulator**.
188
190
189
-
It also contains functions to get metadata about the handler and a function, `createHandler`, which creates and returns an instance of the `Handler` resource. There are other metadata views that could be good to include in your Handler, but we are sticking to the basic ones for now.
191
+
It also contains functions to get metadata about the handler and a function, `createHandler`, which creates and returns an instance of the `Handler` resource. There are other metadata views that could be good to include in your Handler, but we're sticking to the basic ones for now.
190
192
191
193
### Initializing the Transaction Handler
192
194
@@ -237,9 +239,9 @@ manager.schedule(
237
239
238
240
It calls the `schedule` function from the `FlowTransactionSchedulerUtils.Manager` contract. This function has parameters for:
239
241
240
-
-`handlerCap`: The handler [capability] for the code that should be executed.
242
+
-`handlerCap`: The handler [capability] for the code that should execute.
241
243
242
-
This was created above as a part of the previous transaction with:
244
+
This was created above during the previous transaction with:
243
245
244
246
```cadence
245
247
let handlerCap = signer.capabilities.storage
@@ -279,7 +281,7 @@ The transaction call has an argument for `delaySeconds`, which is then converted
279
281
let future = getCurrentBlock().timestamp + delaySeconds
280
282
```
281
283
282
-
-`priority`: The priority this transaction will be given in the event of network congestion. A higher priority means a higher fee for higher precedence.
284
+
-`priority`: The priority this transaction is given in the event of network congestion. A higher priority means a higher fee for higher precedence.
283
285
284
286
The `priority` argument is supplied in the transaction as a `UInt8` for convenience, then converted into the appropriate [enum] type:
285
287
@@ -291,11 +293,11 @@ let pr = priority == 0
291
293
: FlowTransactionScheduler.Priority.Low
292
294
```
293
295
294
-
The `executionEffort` is also supplied as an argument in the transaction. This represents the gas limit for your transaction and is used to prepare the estimate for the gas fees that must be paid for the transaction, and directly in the call to `schedule()` the transaction.
296
+
The `executionEffort` is also supplied as an argument in the transaction. This represents the gas limit for your transaction and used to prepare the estimate for the gas fees that must be paid for the transaction, and directly in the call to `schedule()` the transaction.
295
297
296
298
-`fees`: A [vault] containing the appropriate amount of gas fees needed to pay for the execution of the scheduled transaction.
297
299
298
-
To create the vault, the `estimate()` function is first used to calculate the amount needed:
300
+
To create the vault, the `estimate()` function calculates the amount needed:
299
301
300
302
```cadence
301
303
let est = FlowTransactionScheduler.estimate(
@@ -306,7 +308,7 @@ let est = FlowTransactionScheduler.estimate(
306
308
)
307
309
```
308
310
309
-
Then, an [authorized reference] to the signer's vault is created and used to `withdraw()` the needed funds and [move] them into the `fees` variable which is then sent in the `schedule()` function call.
311
+
Then, an [authorized reference] to the signer's vault is created and used to `withdraw()` the needed funds and [move] them into the `fees` variable, which is then sent in the `schedule()` function call.
310
312
311
313
Finally, we also `assert` that some minimums are met to ensure the transaction will be called:
312
314
@@ -321,9 +323,9 @@ assert(
321
323
322
324
The `FlowTransactionSchedulerUtils.Manager` resource provides a safer and more convenient way to manage scheduled transactions. Instead of directly calling the `FlowTransactionScheduler` contract,
323
325
you can use the Manager resource that manages all your scheduled transactions from a single place and handles many of the common patterns to reduce boilerplate code.
324
-
It also provides many convenient functions to get detailed information about all the transactions you have scheduled by timestamp, handler, etc.
326
+
It also provides many convenient functions to get detailed information about all the transactions you have scheduled by timestamp, handler, and so on.
325
327
When setting up a manager, you also publish a capability for it so it is easy for scripts
326
-
to query your account and also see what transactions you have scheduled!
328
+
to query your account and also see what transactions are scheduled!
327
329
328
330
### Setting Up the Manager
329
331
@@ -365,26 +367,26 @@ manager.schedule(
365
367
366
368
The Manager also provides utility methods for:
367
369
368
-
- Scheduling another transaction with a previously used handler
369
-
- Getting scheduled transaction information in many different ways
370
-
- Canceling scheduled transactions
371
-
- Managing transaction handlers
372
-
- Querying transaction status
370
+
- Scheduling another transaction with a previously used handler.
371
+
- Getting scheduled transaction information in many different ways.
372
+
- Canceling scheduled transactions.
373
+
- Managing transaction handlers.
374
+
- Querying transaction status.
373
375
374
376
## Writing a New Scheduled Transaction
375
377
376
378
With this knowledge, we can create our own scheduled transaction. For this demo, we'll simply display a hello from an old friend in the emulator's console logs.
377
379
378
380
### Creating the Contracts
379
381
380
-
Start by using the [Flow CLI] to create a new contract called `RickRoll.cdc` and one called `RickRollTransactionHandler.cdc`:
382
+
To start, use the [Flow CLI] to create a new contract called `RickRoll.cdc` and one called `RickRollTransactionHandler.cdc`:
381
383
382
384
```zsh
383
385
flow generate contract RickRoll
384
386
flow generate contract RickRollTransactionHandler
385
387
```
386
388
387
-
Open the `RickRoll` contract, and add functions to log a fun message to the emulator console, and a variable to track which message to call:
389
+
Open the `RickRoll` contract and add functions to log a fun message to the emulator console, and a variable to track which message to call:
388
390
389
391
```cadence
390
392
access(all)
@@ -418,7 +420,7 @@ contract RickRoll {
418
420
}
419
421
```
420
422
421
-
Next, open `RickRollTransactionHandler.cdc`. Start by importing the `RickRoll` contract, `FlowToken`, `FungibleToken`, and `FlowTransactionScheduler`, and stubbing out the `Handler` and factory:
423
+
Next, open `RickRollTransactionHandler.cdc`. Import the `RickRoll` contract, `FlowToken`, `FungibleToken`, and `FlowTransactionScheduler`, and stub out the `Handler` and factory:
We could move forward with this, but it would be more fun to have each transaction schedule the follow transaction to share the next message. You can do this by moving most of the code found in the transaction to the handler. Start with configuring the `delay`, `future`, `priority`, and `executionEffort`. We'll hardcode these for simplicity:
481
+
We could move forward with this, but it would be more fun to have each transaction schedule the follow transaction to share the next message. To do this, move most of the code found in the transaction to the handler. Start with configuring the `delay`, `future`, `priority`, and `executionEffort`. We'll hardcode these for simplicity:
480
482
481
483
```cadence
482
484
var delay: UFix64 = 5.0
@@ -679,7 +681,7 @@ transaction(
679
681
680
682
### Deployment and Testing
681
683
682
-
It's now time to deploy and test the new scheduled transaction!: First, add the new contracts to the emulator account in `flow.json` (other contracts may be present):
684
+
It's now time to deploy and test the new scheduled transaction! First, add the new contracts to the emulator account in `flow.json` (other contracts may be present):
683
685
684
686
```json
685
687
"deployments": {
@@ -698,14 +700,14 @@ Then, deploy the contracts to the emulator:
698
700
flow project deploy --network emulator
699
701
```
700
702
701
-
And execute the transaction to initialize the new scheduled transaction handler:
703
+
Next, execute the transaction to initialize the new scheduled transaction handler:
@@ -757,13 +759,13 @@ In this tutorial, you learned about scheduled transactions, a powerful feature t
757
759
758
760
Now that you have completed this tutorial, you should be able to:
759
761
760
-
- Understand the concept of scheduled transactions and how they solve blockchain limitations
761
-
- Explain the key components of the FlowTransactionScheduler system
762
-
- Understand the benefits of the Transaction Scheduler Manager
763
-
- Implement a basic scheduled transaction using the provided scaffold
764
-
- Analyze the structure and flow of scheduled transaction transactions
765
-
- Create custom scheduled transaction contracts and handlers
766
-
- Evaluate the benefits and use cases of scheduled transactions in DeFi applications
762
+
- Understand the concept of scheduled transactions and how they solve blockchain limitations.
763
+
- Explain the key components of the FlowTransactionScheduler system.
764
+
- Understand the benefits of the Transaction Scheduler Manager.
765
+
- Implement a basic scheduled transaction using the provided scaffold.
766
+
- Analyze the structure and flow of scheduled transaction transactions.
767
+
- Create custom scheduled transaction contracts and handlers.
768
+
- Evaluate the benefits and use cases of scheduled transactions in DeFi applications.
767
769
768
770
Scheduled transactions open up new possibilities for DeFi applications, enabling recurring jobs, deferred actions, and autonomous workflows that were previously impossible on blockchain. This feature represents a significant step forward in making blockchain more practical for real-world applications that require time-based execution.
0 commit comments