Why You Should Use Anchor for Building on Solana
Intro to Anchor
Anchor’s author describes it, for those who come from the Ethereum ecosystem, as Solidity, web3.js, and Truffle all in one.
It might seem like the purpose of Anchor is to improve developer experience by using the right abstractions as it does that so well, but originally the reason Anchor was built was to help developers write secure programs!
In case you’re thinking of building on Solana without Anchor, (and you don’t have a solid reason to do so) I am going to make a case that you should use Anchor:
Decoupled Business Logic from Validation Logic
As you dig deeper in the program examples, you will notice that much of the code in most programs is just account validation.
One of the great things Anchor does, right off the bat is that it explicitly separates business logic and validation logic, so that you can focus on one thing at a time and keep things easy to deal with.
Very Useful Macros for Account Access Control and Validation
Anchor offers a lot of macros off the shelf, that are going to come handy when you’re going to be writing your program.
Need to check if the the correct account signed the transaction? There’s a macro for that. Need to see if the account trying to update the data is the one allowed to? There’s a macro for that.
Likewise, anchor provides macros for most of the typical usecases, for a full list, click here.
Account Type Checking Using A Discriminator
Anchor prefixes the account data with a unique identifier for each account “type” and checks that the type is as expected whenever you deserialize, so that it’s impossible to deserialize a certain type of account when your program expects a different type of account.
This is done for all #[account] types, where an account discriminator is generated and inserted into the first 8 bytes of account data.
In-built Support for Verifiable Builds
What is the point of a decentralised application, if users can’t read the source code of the program users are interacting with? As only binaries are published on-chain, there needs to be a mechanism for users to verify that the on-chain program is actually the one they expect it to be.
Anchor offers out-of-the-box solution for this by providing the toolchain to make verifiable builds and a place to publish the source online.
Automatic Program ID Verification
Anchor now enforces statically declared program IDs in all Anchor programs so that every single account owner and CPI program ID is always validated automatically, so that everyone is always talking to the right program.
You Would Be In Great Company!
Many top projects in the Solana ecosystem are built using Anchor, which also means there would be lots of open-source contracts to get inspiration from!