DEV Community

Smile
Smile

Posted on

Build a Solana Smart Contract using Anchor(Part 1)

Solana programs are written using low-level programming languages. More specifically, you can write Solana programs using Rust or C and C++ programming language. For this tutorial, we are going to use Rust.

Solana has emerged as an attractive alternative to Ethereum due to the cost per transaction being cheaper and faster. While the focus of this article is not to mention the differences between using Solana and Ethereum, it is a good idea to have a general understanding of why Solana is gaining popularity quickly.

What kind of Solana program are we building?

We are going to build Solana program that writes a tweet, and users are able to like the tweet.

Do I need to know Rust?

While having previous knowledge in Rust will help you to focus on learning more about the concepts of a Solana program, it is not necessary to know Rust. However, having previous knowledge of a programming language is strongly recommended. We will do our best to break down step-by-step what some of the Rust syntaxes mean.

Setup Solana Development Environment

We are going to use Anchor, a framework for Solana which is in active development at the moment of this writing. The main purpose of Anchor is to provide convenient tools for developers to write Solana programs instead of spending time working on the tedious parts of the raw Solana programs.

Important Note: Currently, only x86_64 Linux is supported currently in Anchor. Therefore, your OS needs to run on x86_64. For Windows users, you can install WSL (Windows Subsystem for Linux) and follow the instructions to set up the Solana development environment.

I am going to skip this step.

Generate Paper Wallet

For us to test our Solana program, we need to have a crypto wallet. A crypto wallet stores collections of keys used to send and receive cryptocurrencies. For this tutorial, we are going to use a paper wallet generated using the command line.

To generate a paper wallet, we will use solana-keygen , which should have been installed when we installed solana. However, to verify it was correctly installed, run the following command:

solana-keygen --version

If you see the version of solana-keygen, that means we can start using it. Run the following command to generate a keypair.

solana-keygen new

This will generate a random seed phrase and prompt you to add an optional passphrase. Feel free to add a passphrase if you want, it is ok not to for the purposes of this tutorial.

Once you fill out the information, the terminal should display the public key and a generated seed phrase. Copy and save the seed phrase in a safe place.

The key pair will be generated in the following location:

/home/<your user>/.config/solana/id.json

Running the localnet Cluster

To run our Solana program, we need to be in a cluster. According to the Solana documentation, a Solana cluster is a set of validators working together to serve client transactions and maintain the integrity of the ledger.

In other words, the Solana program cannot run if it is not in a cluster. Currently, Solana has the following clusters:

  • devnet
  • testnet
  • mainnet

It is possible to connect to localnet, which is the cluster run in our local machine in localhost. To connect to localnet, we will use Solana CLI.

It is possible to connect to localnet, which is the cluster run in our local machine in localhost. To connect to localnet, we will use Solana CLI.

solana config set --url localhost
Now, verify you connected to localnet.

solana config get

Image description

By default, it will connect to port 8899 for RPC and port 8900 for Websocket.

Up to this point, we have completed the installation of the development environment.
I am going to deal about how start project in next post.
Thanks.

Top comments (0)