Solana Playground
Learn how to build your first Solana program using the Anchor framework directly in your browser.
In this section, we'll build, deploy, and test a simple Solana program using the Anchor framework. By the end, you'll have deployed your first program to the Solana blockchain!
Solana Playground (Solpg) is a browser-based development environment that allows you to quickly develop, deploy, and test Solana programs!
Getting Started
Open a new tab in your web browser and navigate to https://beta.solpg.io/.
Create Playground Wallet
If you're new to Solana Playground, the first step is to create your Playground Wallet. This wallet will allow you to interact with the Solana network right from your browser.
Step 1. Connect to Playground
Click the "Not connected" button at the bottom left of the screen.
Step 2. Create Your Wallet
You'll see an option to save your wallet's keypair. Optionally, save your wallet's keypair for backup and then click "Continue".
You should now see your wallet's address, SOL balance, and connected cluster (devnet by default) at the bottom of the window.
Your Playground Wallet will be saved in your browser's local storage. Clearing your browser cache will remove your saved wallet.
Some definitions you may find helpful:
- wallet address: a public key that serves as your unique identity on the Solana blockchain. Just like an email address is used to receive emails, your wallet address is used to receive SOL.
- connection cluster: a network of Solana nodes (computers running Solana validator client). Devnet is the cluster for developer testing.
Get Devnet SOL
Before we start building, we first need some devnet SOL.
From a developer's perspective, SOL is required for two main use cases:
- To create accounts on the network where we store data or deploy programs
- To pay for transaction fees when we interact with the network
Below are two methods to fund your wallet with devnet SOL:
Option 1: Using the Playground Terminal
To fund your Playground wallet with devnet SOL. In the Playground terminal, run:
Option 2: Using the Devnet Faucet
If the airdrop command doesn't work (due to rate limits or errors), you can use the Web Faucet.
- Enter your wallet address (found at the bottom of the Playground screen) and select an amount
- Click "Confirm Airdrop" to receive your devnet SOL
Create Anchor Project
First, open https://beta.solpg.io in a new browser tab.
-
Click the "Create a new project" button on the left-side panel.
-
Enter a project name, select Anchor as the framework, then click the "Create" button.
You'll see a new project created with the program code in the src/lib.rs
file.
Build and Deploy Program
To build the program, simply run build
in the terminal.
Notice that the address in declare_id!()
has been updated. This is your
program's on-chain address.
Once the program is built, run deploy
in the terminal to deploy the program to
the network (devnet by default). To deploy a program, SOL must be allocated to
the on-chain account that stores the program.
Before deployment, ensure you have enough SOL. You can get devnet SOL by either
running solana airdrop 5
in the Playground terminal or using the
Web Faucet.
Alternatively, you can also use the Build
and Deploy
buttons on the
left-side panel.
Once the program is deployed, you can now invoke its instructions.
Test Program
Included with the starter code is a test file found in tests/anchor.test.ts
.
This file demonstrates how to invoke the initialize
instruction on the starter
program from the client.
To run the test file once the program is deployed, run test
in the terminal.
You should see an output indicating that the test passed successfully.
You can also use the Test
button on the left-side panel.
You can then view the transaction logs by running the solana confirm -v
command and specifying the transaction hash (signature) from the test output:
For example:
Alternatively, you can view the transaction details on SolanaFM or Solana Explorer by searching for the transaction signature (hash).
Reminder to update the cluster (network) connection on the Explorer you are using to match Solana Playground. Solana Playground's default cluster is devnet.
Close Program
Lastly, the SOL allocated to the on-chain program can be fully recovered by closing the program.
You can close a program by running the following command and specifying the
program address found in declare_id!()
:
For example:
Congratulations! You've just built and deployed your first Solana program using the Anchor framework!