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:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
cargo install --git https://github.com/coral-xyz/anchor anchor-cli --locked
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt install -y nodejs
mkdir solana-smart-contract && cd solana-smart-contract cargo init --lib
Cargo.toml
: [dependencies] solana-program = "1.17.0"
src/lib.rs
: 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(()) }
cargo build-bpf
anchor init my_solana_program cd my_solana_program
programs/my_solana_program/src/lib.rs
: 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 {}
anchor build
solana-keygen new --outfile ~/.config/solana/id.json
solana airdrop 2
solana program deploy target/deploy/my_solana_program.so
Or with Anchor: anchor deploy
You can write a Solana client in JavaScript using the @solana/web3.js
library:
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:
solana program show <PROGRAM_ID>
Would you like help with a more advanced use case, such as token minting or NFT contracts?