Quickstart
Getting started
In this quickstart, you will learn how to automate a SOL transfer using Clockwork. We will install the Clockwork CLI and spawn a Clockwork thread from a Typescript application. Within 10 minutes, you will have your first automation up and running.
All code in this quickstart is open-source and free to fork on Github.
1. Create your first automation (5 min)
Let's begin by creating a new vanilla Node Typescript project:
Create a new tsconfig.json
file:
Create a new package.json
file with the below content. The main dependencies you really need in your project are the Clockwork SDK and Anchor.
After this, create a new file called main.ts
. Import the necessary libraries and set up a Clockwork provider.
Next, we'll prepare a SystemTransfer
instruction and automate it with a Clockwork thread. Threads are the basic building blocks of automations on Solana. Every thread has a trigger condition and a set of instructions to run. In the code sample below, we create a thread that schedules a SOL transfer to happen every 10 seconds.
We can see the threadCreate
function asks for 5 arguments. These include some basic information needed to initialize the thread account.
authority
– The owner of the thread. This account must be the transaction signer and will have permission to delete, pause, resume, stop, and update the thread.id
– An identifier for the thread (can also use buffer or vec u8).instructions
– The list of instructions to execute when the trigger condition becomes valid.trigger
– The trigger condition for the thread. When this condition is valid, the thread will begin executing the provided instructions.amount
– The number of lamports to fund the thread account with. Remember to provide a small amount of SOL. Read more about how fees are calculated here.
2. Run the tests (2 min)
Now we need to get our app running. If you have not done so already, you will need to install the Clockwork CLI by running the cargo command below. If you face any trouble here, please refer to the installation docs.
Now that we have Clockwork installed, we can go ahead and spin up a local Clockwork node:
In a separate terminal window, we'll run the test:
And voila:
3. Monitor your automation (1 min)
You can now watch your automation run all on its own. Grab the Clockwork explorer link that was printed out to the console. Using the Clockwork explorer, you can get simulation logs and inspect your thread. For example, here's mine: https://app.clockwork.xyz/threads/GB7YgYK3bKF8J4Rr9Z2oeA3hwxrJdvW5zgXuNaxWWmUF?cluster=devnet
Of course you can also look up your thread account in your favorite Solana explorer. You can alternatively use the Solana CLI to stream program logs by running the command provided below. Here's an example thread that was created in a test on May 24th, 2023.
Key insights
Threads are an automation primitive for Solana.
You can use threads to automate any program instruction on Solana.
Triggers allow you to define when a thread should begin execution.
Threads must be funded with a small amount of SOL to pay for automation fees.
Continue learning
Many more examples can be found in the guides section.
Ask questions on Discord.
Last updated