Running Your First Solana Project with Anchor

Running Your First Solana Project with Anchor


In this post, we’ll continue from where we left off in the last guide Setting Up Local Solana Environment. We’ve already set up our Solana development environment, so now, let’s run our first Solana project using the default Anchor code. By the end of this post, you’ll know how to set up your IDE, connect to WSL, and deploy your first Solana program on the Devnet.




Step 1: Install Visual Studio Code (VS Code)

VS Code is one of the best editors for Solana development. If you don’t already have it installed, download and install it from Visual Studio Code’s official website.




Step 2: Install Remote – WSL Extension

The Remote – WSL extension allows you to seamlessly work with files and folders inside your WSL Ubuntu environment.

  1. Open VS Code.
  2. Go to the Extensions view by clicking the Extensions icon on the left-hand side or pressing Ctrl+Shift+X.
  3. Search for Remote - WSL and click Install.






Step 3: Connect VS Code to WSL Ubuntu

After installing the extension, connect to the Ubuntu environment we created in the previous guide:

  1. Open the Command Palette (Ctrl+Shift+P) in VS Code.
  2. Search for and select Remote-WSL: New Window.
  3. This opens a new VS Code window connected to your WSL environment.



Remote - WSL



Step 4: Install Rust Analyzer Extension

Rust Analyzer provides powerful features like syntax highlighting, autocompletion, and inline documentation for Rust projects.

  1. In the Extensions view (Ctrl+Shift+X), search for Rust Analyzer.
  2. Click Install.



Rust Analyzer



Step 5: Generate a Default Anchor Project

Anchor provides a boilerplate project to help you start coding quickly. Follow these steps to create your first Solana program:

  1. Open the WSL terminal in VS Code or launch it from your Ubuntu environment.
  2. Run the following commands to initialize a new project:
   anchor init my_solana_project
   cd my_solana_project
Enter fullscreen mode

Exit fullscreen mode

This creates a new directory with a basic Solana program scaffold.




Step 6: Build the Anchor Project

Before deploying, we need to build the program:

  1. Navigate to the project directory if you’re not already there:
   cd my_solana_project
Enter fullscreen mode

Exit fullscreen mode

  1. Build the project:
   anchor build
Enter fullscreen mode

Exit fullscreen mode

This compiles the Rust code into a Solana-compatible program.




Step 7: Run Solana Test Validator

solana-test-validator starts a local Solana network on your machine, which allows you to deploy and test your Anchor program without relying on the Solana devnet or mainnet.

We will run the below command

solana-test-validator
Enter fullscreen mode

Exit fullscreen mode

If you encounter an error with the above command run

solana-test-validator --log
Enter fullscreen mode

Exit fullscreen mode

If you encounter errors like “AVX2 not supported”, refer to the Solana GitHub repository for instructions on building Solana from source.



Step 8: Get Sol for Gas Fees

To deploy your program, you’ll need some SOL to cover transaction fees. Let’s request SOL from the Devnet faucet:

  1. Set your Solana CLI to use the Devnet:
   solana config set --url https://api.devnet.solana.com
Enter fullscreen mode

Exit fullscreen mode

  1. Generate a new Solana wallet:
   solana-keygen new --outfile ~/.config/solana/id.json
Enter fullscreen mode

Exit fullscreen mode

  1. Fund your wallet with Devnet SOL:
   solana airdrop 2
Enter fullscreen mode

Exit fullscreen mode

  1. Confirm your balance:
   solana balance
Enter fullscreen mode

Exit fullscreen mode




Step 9: Deploy the Anchor Program

Now that everything is ready, deploy the program to the Devnet:

  1. Run the deployment command:
   anchor deploy
Enter fullscreen mode

Exit fullscreen mode

  1. After a successful deployment, you’ll see the program ID in the output. Save this ID for later use.



Step 10: Interact with Your Deployed Program (Optional)

Anchor provides a test suite for interacting with your program. To test the default code:

  1. Run the test command:
   anchor test
Enter fullscreen mode

Exit fullscreen mode

  1. This executes the default program logic on the Devnet and returns the results.



Additional Tips:

  • Use .env Files: Store your program ID, RPC URL, and other secrets in a .env file to keep your project organized.
  • Debugging: If you run into errors, check the target/deploy folder for compiled artifacts and logs.
  • Read the Docs: Explore Anchor’s official documentation for advanced topics like program state and cross-program invocations.
  • Error Handling: If you encounter deployment errors, check the logs in the target/deploy/ folder.
  • Local vs. Devnet: Use solana-test-validator for local testing. Switch to Devnet when interacting with real wallets or external users.
  • Documentation: Explore Anchor’s official docs for advanced features like program state and cross-program invocations.



Conclusion

Congratulations! You’ve successfully run your first Solana project using Anchor. From setting up VS Code to deploying on Devnet, you’ve completed the essential steps to get started with Solana development.

If you have questions or want to learn more, subscribe and stay tuned for the next guide where we’ll dive deeper into customizing Anchor programs.



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.