Learn how to write a simple Anchor program that increments a counter on Solana
This guide will walk you through the process of writing a simple Anchor program that increments a counter. You’ll learn how to deploy this program on Solana and interact with it using a React client.
This program is developed and tested with the following software packages. Other sofware may also be compatible.
Software | Version | Installation Guide |
---|---|---|
Solana | 2.1.21 | Install Solana |
Rust | 1.82.0 | Install Rust |
Anchor | 0.31.1 | Install Anchor |
If you prefer to dive straight into the code:
Let’s break down the key components of our counter program:
The program implements two main instructions:
initialize
: Sets the counter to 0increment
: Increments the counter by 1Here’s the core structure of our program:
Nothing special here, just a simple Anchor program that increments a counter. The only difference is that we’re adding the delegate
macro to inject some useful logic to interact with the delegation program.
In order to delegate the counter PDA, and make it writable in an Ephemeral Rollup session, we need to add an instruction which
internally calls the delegate_account
function. delegate_account
will CPI to the delegation program, which upon validation will gain ownership of the account.
After this step, an ephemeral validator can start processing transactions on the counter PDA and propose state diff trough the delegation program.
The ephemeral runtime allow to commit the state of the PDA while it is delegated. This is done by calling the commit_accounts
function.
Inspect transaction details on Solana Explorer
Inspect transaction details on Solana Explorer
Undelegating the PDA is done by calling the commit_and_undelegate_accounts
as part of some instruction.
Undelegation commit the latest state and give back the ownership of the PDA to the owner program.
Inspect transaction details on Solana Explorer
Inspect transaction details on Solana Explorer
The React client is a simple interface that allows you to interact with the Anchor program. It uses the Anchor bindings to interact with the program and the MagicBlock SDK to interact with the Ephemeral Rollup session.
Iframes only work with some wallets (e.g. Backpack). Alternatively, try the deployed demo here: https://main—ephemeral-counter.netlify.app/
To interact with the Ephemeral Rollup session, you need to configure the appropriate endpoint:
For devnet, use the following ephemeral endpoint:
For mainnet, please reach out to the MagicBlock team to receive the appropriate endpoint.
For localhost, download, install, and run the ephemeral validator locally with the appropriate environment variables:
Make sure to update your client configuration to use the correct endpoint based on your development or production environment.
These public RPC endpoints are currently free and supported for development:
Smart Router Devnet: https://devnet-rpc.magicblock.app
Solana Devnet: https://api.devnet.solana.com
ER Devnet: https://devnet.magicblock.app
Learn how to write a simple Anchor program that increments a counter on Solana
This guide will walk you through the process of writing a simple Anchor program that increments a counter. You’ll learn how to deploy this program on Solana and interact with it using a React client.
This program is developed and tested with the following software packages. Other sofware may also be compatible.
Software | Version | Installation Guide |
---|---|---|
Solana | 2.1.21 | Install Solana |
Rust | 1.82.0 | Install Rust |
Anchor | 0.31.1 | Install Anchor |
If you prefer to dive straight into the code:
Let’s break down the key components of our counter program:
The program implements two main instructions:
initialize
: Sets the counter to 0increment
: Increments the counter by 1Here’s the core structure of our program:
Nothing special here, just a simple Anchor program that increments a counter. The only difference is that we’re adding the delegate
macro to inject some useful logic to interact with the delegation program.
In order to delegate the counter PDA, and make it writable in an Ephemeral Rollup session, we need to add an instruction which
internally calls the delegate_account
function. delegate_account
will CPI to the delegation program, which upon validation will gain ownership of the account.
After this step, an ephemeral validator can start processing transactions on the counter PDA and propose state diff trough the delegation program.
The ephemeral runtime allow to commit the state of the PDA while it is delegated. This is done by calling the commit_accounts
function.
Inspect transaction details on Solana Explorer
Inspect transaction details on Solana Explorer
Undelegating the PDA is done by calling the commit_and_undelegate_accounts
as part of some instruction.
Undelegation commit the latest state and give back the ownership of the PDA to the owner program.
Inspect transaction details on Solana Explorer
Inspect transaction details on Solana Explorer
The React client is a simple interface that allows you to interact with the Anchor program. It uses the Anchor bindings to interact with the program and the MagicBlock SDK to interact with the Ephemeral Rollup session.
Iframes only work with some wallets (e.g. Backpack). Alternatively, try the deployed demo here: https://main—ephemeral-counter.netlify.app/
To interact with the Ephemeral Rollup session, you need to configure the appropriate endpoint:
For devnet, use the following ephemeral endpoint:
For mainnet, please reach out to the MagicBlock team to receive the appropriate endpoint.
For localhost, download, install, and run the ephemeral validator locally with the appropriate environment variables:
Make sure to update your client configuration to use the correct endpoint based on your development or production environment.
These public RPC endpoints are currently free and supported for development:
Smart Router Devnet: https://devnet-rpc.magicblock.app
Solana Devnet: https://api.devnet.solana.com
ER Devnet: https://devnet.magicblock.app