Deploying a smart contract (called a program in Solana) involves writing, compiling, and deploying it onto the Solana blockchain. Here's a step-by-step guide:
Before you start, ensure you have the following installed:
sh
CopyEdit
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
sh
CopyEdit
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
sh
CopyEdit
cargo install --git https://github.com/coral-xyz/anchor anchor-cli --locked
sh
CopyEdit
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
sh
CopyEdit
mkdir solana-smart-contract && cd solana-smart-contract
cargo init --lib
Cargo.toml
:toml
CopyEdit
[dependencies]
solana-program = "1.17.0"
src/lib.rs
:rust
CopyEdit
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
msg,
pubkey::Pubkey,
};
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
msg!("Hello, Solana!");
Ok(())
}
sh
CopyEdit
cargo build-bpf
sh
CopyEdit
anchor init my_solana_program
cd my_solana_program
programs/my_solana_program/src/lib.rs
:rust
CopyEdit
use anchor_lang::prelude::*;
declare_id!("YourProgramID");
#[program]
mod my_solana_program {
use super::*;
pub fn hello_world(ctx: Context<Initialize>) -> Result<()> {
msg!("Hello, Solana!");
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize {}
sh
CopyEdit
anchor build
sh
CopyEdit
solana-keygen new --outfile ~/.config/solana/id.json
sh
CopyEdit
solana airdrop 2
sh
CopyEdit
solana program deploy target/deploy/my_solana_program.so
sh
CopyEdit
anchor deploy
You can write a Solana client in JavaScript using the @solana/web3.js
library:
js
CopyEdit
const { Connection, PublicKey, clusterApiUrl } = require("@solana/web3.js");
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const programId = new PublicKey("YourProgramID");
console.log("Solana Program Deployed at:", programId.toBase58());
Check if your program is deployed by running:
sh
CopyEdit
solana program show <PROGRAM_ID>
© 2025 Invastor. All Rights Reserved
User Comments