DISCLAIMER Since the follow up courses will introduce more complex features, this tutorial will FOCUS ON how Anchor framework works and build a full-stack counter dApp as practice.

Table of Contents

Installation

Basic Rust Syntax

Anchor framework


If you can’t successfully setup the environment in your computer… Don’t give up! Here’s an alternative way by using online IDE:

Solana Playground | Solana IDE

But to have a better developing experience, I’d still recommend you set up the environment locally or in a VPS with Linux OS.

Generate a key pair if you don’t have one yet, we’ll use it later.

solana-keygen new

Start an Anchor Project


# anchor init <project_name>, change <program_name> to your own, for me it will be "my-program"
anchor init counter-program
cd counter-program

# or 
# anchor init counter-program --template multiple

After initializing, your project structure might looks like below:

counter-program
├── app
├── migrations
│   └── deploy.ts
├── programs
│   └── counter-program
│       ├── src
│       │   └── lib.rs
│       ├── Cargo.toml
│       └── Xargo.toml
├── target
│   └── deploy
│       └── counter_program-keypair.json
├── tests
│   └── counter-program.ts
├── Anchor.toml
├── Cargo.lock
├── Cargo.toml
├── package.json
├── tsconfig.json
└── yarn.lock

All your programs (smart contract) will be under programs folder and each program will have it’s logic inside it’s src/lib.rs the Cargo.toml will manage dependencies. At the beginning, target directory will only have deploy folder with program key pair inside, you can use anchor keys list command to verify the public key matches with the one inside lib.rs file. And after we build our program you can see the binary, idl, and type files be created inside target folder too. In tests folder, we can see it has a simple test created by Anchor, we will update the file later.

Before we start playing around with our program, lets check the scaffold works as expected by running

anchor test