Deploying Your First Solana Program
In this section, you'll build, deploy, and test a simple Solana program (smart contract) using the Anchor framework. By the end, you'll have deployed your first program to the Solana blockchain.
The purpose of this section focuses on familiarizing you with the Solana Playground. The guide walks through a more detailed example in the Program Derived Address (PDA) and Cross-Program Invocation (CPI) sections. For more details, refer to the Programs on Solana page.
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.
New Project
You'll see a new project created with the program code in the src/lib.rs
file.
This basic Solana program creates a new account and stores a number in it. The
program contains one instruction (initialize
) that takes a u64
number as
input, creates a new account, and saves that number in the account's data. When
you call the instruction, it also logs a message to the transaction's program
logs.
use anchor_lang::prelude::*;// This is your program's public key and it will update// automatically when you build the project.declare_id!("11111111111111111111111111111111");#[program]mod hello_anchor {use super::*;pub fn initialize(ctx: Context<Initialize>, data: u64) -> Result<()> {ctx.accounts.new_account.data = data;msg!("Changed data to: {}!", data); // Message will show up in the tx logsOk(())}}#[derive(Accounts)]pub struct Initialize<'info> {// We must specify the space in order to initialize an account.// First 8 bytes are default account discriminator,// next 8 bytes come from NewAccount.data being type u64.// (u64 = 64 bits unsigned integer = 8 bytes)#[account(init, payer = signer, space = 8 + 8)]pub new_account: Account<'info, NewAccount>,#[account(mut)]pub signer: Signer<'info>,pub system_program: Program<'info, System>,}#[account]pub struct NewAccount {data: u64}
Build and Deploy Program
To build the program, simply run build
in the terminal.
$build
Solana Playground updates the address in declare_id!()
. This address
represents your program's on-chain address (program ID).
After building the program, run deploy
in the terminal to deploy the
program to the network (devnet by default). Program deployment requires
allocating SOL 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.
$deploy
You can also use the Build
and Deploy
buttons on the
left-side panel.
Build and Deploy
After deploying the program, you can call its instructions.
Test Program
The starter code includes a test file located in tests/anchor.test.ts
.
This file demonstrates how to invoke the initialize
instruction
on the program from the client.
// No imports needed: web3, anchor, pg and more are globally availabledescribe("Test", () => {it("initialize", async () => {// Generate keypair for the new accountconst newAccountKp = new web3.Keypair();// Send transactionconst data = new BN(42);const txHash = await pg.program.methods.initialize(data).accounts({newAccount: newAccountKp.publicKey,signer: pg.wallet.publicKey,systemProgram: web3.SystemProgram.programId,}).signers([newAccountKp]).rpc();console.log(`Use 'solana confirm -v ${txHash}' to see the logs`);// Confirm transactionawait pg.connection.confirmTransaction(txHash);// Fetch the created accountconst newAccount = await pg.program.account.newAccount.fetch(newAccountKp.publicKey,);console.log("On-chain data is:", newAccount.data.toString());// Check whether the data on-chain is equal to local 'data'assert(data.eq(newAccount.data));});});
To run the test file after deploying the program, run test
in the
terminal.
$test
Look for output that confirms the test passed successfully.
You can also use the Test
button on the left-side panel.
Run Test
You can then view the transaction logs by running the solana confirm -v
command and specifying the transaction hash (signature) from the test output:
$solana confirm -v [TxHash]
For example:
$solana confirm -v 3TewJtiUz1EgtT88pLJHvKFzqrzDNuHVi8CfD2mWmHEBAaMfC5NAaHdmr19qQYfTiBace6XUmADvR4Qrhe8gH5uc
You can also view the transaction details on SolanaFM or Solana Explorer by searching for the transaction signature (hash).
Remember to update the cluster (network) connection on the Explorer you use to match Solana Playground. Solana Playground defaults to the devnet cluster.
Close Program
Lastly, closing the program allows full recovery of the SOL allocated to the on-chain program.
You can close a program by running the following command and specifying the
program address found in declare_id!()
:
$solana program close [ProgramID]
For example:
$solana program close 2VvQ11q8xrn5tkPNyeraRsPaATdiPx8weLAD8aD4dn2r
Congratulations. You've just built and deployed your first Solana program using the Anchor framework.
Is this page helpful?