SolMeet Book
  • About SolMeet
  • Notes
    • #1 - A Starter Kit for New Solana Developer
    • #2 - Introduction to Anchor
    • #3 - A Complete Guide to Mint Solana NFTs with Metaplex
    • #4 - BUIDL a Swap UI on Solana
    • #5 - BUIDL an Auto-compounding Bot on Saber
    • #6 - A Starter Kit for Running Solana Validator
    • #7 - A Complete Guide to Create a NFT DAO on Solana
    • #8 - Deep Dive into Anchor by Implementing Token Management Program
    • #9 - Walk Through Solana SDK Design
    • #10 - Walk Through NFT Breeding Tokenomic and Program Design
    • #11 - BUIDL an Orderbook-based DEX on Solana in 2 hours
    • #12 - A Complete Guide to Build a Simple Aggregator with Universal Rabbit Hole
    • #13 - Solana Pay in Practice: The Challenge and Solution
    • #14 - A Complete Guide to Implement an Adapter on Universal Rabbit Hole
    • #15 - A Complete Guide to Mint Solana NFTs through a Mobile App (Android)
Powered by GitBook
On this page
  • What is Anchor?
  • Workflow
  • Why Anchor?
  • Where can I learn more about Anchor?
  • Overview of Token Management
  • Vesting and Mining
  • Architecture
  • Interfaces
  • Implementing Token Management Program in Anchor
  • Prerequisites
  • To Be Covered
  • Step 1: Scaffolding
  • Part 2: Implementing
  • Part 3: Improving Security
  • More Advanced Topics
  • Referneces
  1. Notes

#8 - Deep Dive into Anchor by Implementing Token Management Program

Previous#7 - A Complete Guide to Create a NFT DAO on SolanaNext#9 - Walk Through Solana SDK Design

Last updated 2 years ago

Author: ,

[Updated at 2022.5.21]

You can find the full code base

What is Anchor?

There is a comprehensive explanation on the . Let me just quote relative paragraphs here:

Anchor is a framework for Solana's Sealevel runtime providing several convenient developer tools.

If you're familiar with developing in Ethereum's Solidity, Truffle, web3.js, then the experience will be familiar. Although the DSL syntax and semantics are targeted at Solana, the high level flow of writing RPC request handlers, emitting an IDL, and generating clients from IDL is the same.

In short, Anchor gives you the following handy tools for developing Solana programs:

  • Rust crates and eDSL for writing Solana programs

  • IDL specification

  • TypeScript package for generating clients from IDL

  • CLI and workspace management for developing complete applications

You can watch given by Armani Ferrante at Breakpoint 2021 to feel the power of Anchor.

Workflow

  1. Develop the program (Smart Contract)

  2. Build the program and export the IDL

  3. Generate the client representation of program from the IDL to interact with the program

Why Anchor?

  • Anchor is the new standard

  • Productivity

    • Make Solana program more intuitive to understand

    • More clear buisness Logic

    • Remove a ton of biolderplate code

  • Security

    • Use discriminator

      • Discriminator is generated and inserted into the first 8 bytes of account data. Ex: sha256("account:<MyAccountName>")[..8] || borsh(account_struct)

      • Used for more secure account validation and function dispatch

Where can I learn more about Anchor?

Overview of Token Management

  • Upgrade Anchor to latest version (Currently 0.24.2)

  • Optimize some function invocation to avoid stack frame limit

  • Rename struct and module to make them better reveal its designed purpose

In short, token management consist of two modules:

  • Locker Manager, which manages the vesting of locked tokens

  • Pool Manager, which manages the mining of rewarded tokens

Vesting and Mining

  • Token vesting and mining are two fundenmental components of DeFi tokenomics

  • There are existed solutions.

    • Bonfida vesting

    • Quarry

    • ...

  • anchor-token-management intergates vesting and mining modules and has the following features:

    • Written in Anchor

    • Modular and customizable condition of releasing rewards

    • Control funds even if its locked in locker. For example: desposit to pool from locker. (Not covered in this tutorial)

Architecture

Interfaces

Locker

Create Locker

Withdraw


Pool (Rewarder)

Create Pool

Drop Reward

Expired Reward


Pool (Staker)

Create Staker

Update Staker Vault

Deposit

Withdraw

Stake

Start Unstake

End Unstake

Claim Reward

Claim Reward to Locker

Implementing Token Management Program in Anchor

Prerequisites

  • Basic layouts of Anchor programs

    • program

    • context

    • state

    • error

    • ...

  • Anchor Constraints

    • [account(mut)]

    • [account(init)]

    • ...

  • Token Program

    • Token Account

    • Mint

    • Transfer

    • Burn

To Be Covered

  • PDA account creation and derivation

  • Access control

  • CPI usage

  • Custimized Error

Step 1: Scaffolding

  • In this step, we scaffold the programs by following the interfaces explained above:

    • Implement LockerManager

    • Implement PoolManager

    • Implement PoolRewardKeeper

Part 2: Implementing

  • In this step, we implement all the functions, context and states without concerning the security issues:

    • Add all contexts and states

    • Add utils

      • calculator

      • RewardQueue

      • PDA creation and derivation

      • has_one

      • Raw constraints

      • ...

Tips: you can use git diff to see what changes have been made:

$ git checkout step-2
$ git diff step-1

Part 3: Improving Security

  • In this step, we improve the security:

    • Implement access control and security check

    • Customize error

Tips: you can use git diff to see what changes have been made:

$ git checkout step-3
$ git diff step-2

More Advanced Topics

  • withdraw_to_whitelist

  • deposit_from_locker

  • withdraw_to_locker

Referneces

  • https://book.anchor-lang.com

  • https://solanacookbook.com

  • https://book.solmeet.dev/notes/intro-to-anchor

  • https://github.com/project-serum/stake/blob/master/docs/staking.md

  • https://docs.rs/anchor-lang/0.24.2/anchor_lang/derive.Accounts.html

See for more details

See and for the actual implementation

Implement most of the of secure program

Note: These programs are originated from Serum's developed by Armani Ferrante. There are a few things evolved from the original version:

Checkout to branch to see the full code

Checkout to branch to see the full code

Add

Checkout to branch to see the full code

Control funds even if its locked in locker. For example: desposit to pool from locker. See the full for details.

apr.dev
anchor.so
anchor.projectserum.com
this Twitter thread
here
here
best practices
Anchor Book
Anchor Examples
anchor-escrow
stake program
anchor-escrow
step-1
step-2
constraints
step-3
code base
@ironaddicteddog
@emersonliuuu
here
official website
this awesome talk