If you've been following the wild world of blockchain development, you've probably heard of Solana. But what makes it stand out? Simply put, speed and scalability. Solana can handle thousands of transactions per second (TPS), making it one of the fastest blockchains out there.
But enough about TPS—let's talk about something even more interesting: building on Solana using Rust and Anchor. Ready to dive in? Let's go!
Ever heard of smart contracts? Of course, you have. But on Solana, we call them programs. These aren’t just any smart contracts—they're the logic that powers decentralized applications (dApps) on the Solana blockchain. Imagine programs as on-chain accounts storing executable logic, ready to be called into action by a trigger known as an instruction.
So, what’s cool about Solana programs? Well, they’re stateless, which means they don’t store any data themselves. Instead, they can create new accounts to store and manage data. This stateless design helps keep the blockchain efficient and scalable.
When it comes to writing Solana programs, Rust is the go-to language. Why Rust? Because it’s fast, safe, and provides fine-grained control over memory management—crucial for blockchain development. But here's where it gets even better: you can use Rust with or without frameworks.
In the decentralized world, trust is everything. That’s why verifiable builds are so important on Solana. A verifiable build ensures that the on-chain program matches the publicly available source code. This level of transparency builds trust among users and developers alike.
In this tutorial, you'll:
Install Rust and Cargo:
If you haven’t already, install Rust and Cargo by following the official guide:
curl --proto '=https' --tlsv1.2 -sSf <https://sh.rustup.rs> | sh
Install Solana CLI:
The Solana Command Line Interface (CLI) is essential for interacting with the Solana blockchain. Install it using the following command:
sh -c "$(curl -sSfL <https://release.solana.com/stable/install>)"
Create a New Project:
Create a new directory for your project and navigate into it:
mkdir solana-hello-world && cd solana-hello-world
Initialize a New Cargo Project:
Create a new Rust project with Cargo:
cargo new --lib hello_world
cd hello_world
Set Up Your Program:
Open the lib.rs
file in your favorite code editor and replace the contents with the following:
This basic program logs "Hello, Solana!" whenever it's invoked.
Build Your Program:
Compile your program to ensure there are no errors:
cargo build-bpf
Create a New Wallet:
Generate a new Solana wallet for Devnet:
solana-keygen new --outfile ~/my-solana-wallet.json
solana config set --keypair ~/my-solana-wallet.json
Airdrop SOL to Your Wallet:
Request some SOL tokens to deploy your program on Devnet:
solana airdrop 2
Deploy Your Program:
Deploy your program to the Solana blockchain:
solana program deploy ./target/deploy/hello_world.so
Note the program's public key, which will be used to interact with it.
Create a Client:
Write a simple Rust client to call your program:
Run Your Client:
Execute the client to interact with your program:
cargo run
If successful, you’ll see your transaction's signature in the console, and the "Hello, Solana!" message will be logged on Solana Explorer.
In this tutorial, you'll:
Install Rust and Cargo:
Follow the steps in the previous tutorial to install Rust and Cargo.
Install Anchor:
Install the Anchor CLI via Cargo:
cargo install --git <https://github.com/coral-xyz/anchor> avm --locked
anchor upgrade
Set Up Your Project:
Create a new Anchor project:
anchor init hello_anchor
cd hello_anchor
Write Your Program:
Open the lib.rs
file in the programs/hello_anchor/src/
directory and replace the contents with:
This program logs "Hello, Anchor!" to the console when executed.
Build Your Program:
Compile your Anchor program:
anchor build
Deploy to Devnet:
Deploy your program to Solana Devnet:
anchor deploy
Your program's public key will be displayed in the terminal after deployment.
Set Up Your Client:
Open the tests/hello_anchor.ts
file and replace the contents with:
Run the Client:
Execute the client test to interact with your program:
anchor test
After running the test, check the Solana Explorer for your transaction, where you’ll see the "Hello, Anchor!" message.
Building on Solana is more than just writing programs; it’s about creating the future of decentralized applications. Whether you choose to go full Rust or embrace the simplicity of Anchor, Solana offers the tools and infrastructure to bring your ideas to life. So, what will you build next?
When building and deploying on Solana, it’s crucial to ensure the security of your project. Make sure to audit your code with a reputable firm like QuillAudits to safeguard your work and protect your users.
Feel free to dive deeper into Solana’s documentation, experiment with different setups, and don’t hesitate to share your experience. Happy coding!
Get Pure Alpha Straight to Your Inbox. Miss this, and you’re missing out.
Insider Secrets - Delivered Right to You. Subscribe now.