Token management program (vesting and mining) implemented in Anchor
Deep Dive into Anchor by Implementing Token Management Program
Author: @ironaddicteddog, @emersonliuuu
[Updated at 2022.5.21]
You can find the full code base here
What is Anchor?
There is a comprehensive explanation on the official website. 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
Workflow

- Develop the program (Smart Contract)
- Build the program and export the IDL
- Generate the client representation of program from the IDL to interact with the program
Why Anchor?
- Anchor is the new standard
- Productivity
- Security
sha256("account:<MyAccountName>")[..8] || borsh(account_struct)
- Used for more secure account validation and function dispatch
- See this Twitter thread for more details
- See here and here for the actual implementation
- Implement most of the best practices of secure program
Where can I learn more about Anchor?
Overview of Token Management
Note: These programs are originated from Serum's stake program developed by Armani Ferrante. There are a few things evolved from the original version:
- 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
- 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.
anchor-token-managementintergates vesting and mining modules and has the following features:
Architecture

Interfaces
| Locker | | :-------------------------------------------: | |
| |
|
| Pool (Rewarder) | | :-------------------------------------------: | |
| |
| |
| |
| |
|
| Pool (Staker) | | :-------------------------------------------: | |
| |
| |
| |
| |
| |
| |
|
Implementing Token Management Program in Anchor
Prerequisites
- Basic layouts of Anchor programs
- Anchor Constraints
[account(mut)]
- [account(init)]
- ...
- Token Program
To Be Covered
- PDA account creation and derivation
- Access control
- CPI usage
- Custimized Error
Step 1: Scaffolding
- Checkout to
step-1branch to see the full code - In this step, we scaffold the programs by following the interfaces explained above:
LockerManager
- Implement PoolManager
- Implement PoolRewardKeeper
Part 2: Implementing
- Checkout to
step-2branch to see the full code - In this step, we implement all the functions, context and states without concerning the security issues:
calculator
- RewardQueue
- Add constraints
- PDA creation and derivation
- has_one
- Raw constraints
- ...
Tips: you can use git diff to see what changes have been made:
>
> $ git diff step-1 >> $ git checkout step-2
Part 3: Improving Security
- Checkout to
step-3branch to see the full code - In this step, we improve the security:
Tips: you can use git diff to see what changes have been made:
>
> $ git diff step-2 >> $ git checkout step-3
More Advanced Topics
Control funds even if its locked in locker. For example: desposit to pool from locker. See the full code base for details.
withdrawtowhitelistdepositfromlockerwithdrawtolocker
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