The Solana Programming Model
Solana often takes a while for people to wrap their heads around. I find this happens for a few reasons:
While there is some good content, it's scattered across many websites and blogs.
Solana is often explained in relation to Ethereum. But you don't need to understand Ethereum to understand Solana. This just confuses things.
There are some concepts in Solana (like the NFT token model) which are more complicated than they need to be.
As with everything in crypto, the majority of explainers are non-technical that use hard to understand metaphors.
Solana is a Key / Value Database
I've spent a few months banging my head at this and I think I have a mental model that makes sense now...
Solana is a huge key value data database. Each Solana node stores this key value database in memory (and is why nodes have to be so big).
In Solana nomenclature, the key is called an address and the value is called the account. Each address points to an account where data is stored.
And everything on Solana is an address / account pair. For example, each program has an address and stores the code for the program at the associated account. (It's actually a bit more complex than this but we don't need to get into that...)
Regular Accounts and Storage Accounts
Now let’s talk about addresses. On Solana an address is a 256 bit number that is most often encoded with base58, so it looks something like this CXk5NCtYva7h4BcGXg5BDBDtZDwgBuWZcwmWt5ReY1yA
.
There are two different types of accounts — let’s call them Regular accounts and Storage accounts.
Regular Accounts
Regular accounts are what you think of when you think of crypto addresses.
Bitcoin, Ethereum, and Solana all use Elliptic Curve cryptography which allows a public private keypair to sign messages. If you have a public private keypair, you can sign a message with your private key and other people can verify the message is signed correctly.
This public / private keypair mechanism allows wallets to sign transactions that authorize some action. The Bitcoin code won’t execute a transaction that sends Bitcoin unless that account has signed the transaction.
On Solana, the regular account corresponds to the public key. You hold the private key securely in your wallet and you can share your public key.
In this system, a public key is a 256 bit number which lies on an elliptic curve. So a valid Solana Regular Account is one which lies on the elliptic curve.
Regular accounts are used for a couple things:
Wallets — If you want to have an account that stores SOL, you will create a regular account. SOL is stored directly in your wallet. (Tokens / NFTs are stored on Storage Accounts associated with your wallet.)
Programs — Every program is a regular account. It stores data in the account that is executed when the program is called.
Storage Accounts (PDAs)
Storage accounts allow you to store data associated with programs. A program can do anything from storing the state of an NFT listing, allowing an escrow between two parties, to acting as a domain registry.
You know an account is a storage account because its address is not on the elliptic curve that we mentioned above. That means a storage account address is not part of a public / private keypair, it’s just a number.
Regular accounts can sign messages with their private key to authorize transactions. Storage accounts can’t do this since they don’t have a private key. Instead storage accounts are linked to a controller program.
So if you are creating a program that will take a poll of where everyone wants to go on vacation, that program will store information about the different vacation destinations and votes at storage programs that are linked to the program.
How NFTs Work
If you ask me, the NFT spec in Solana is as complicated as possible (and I really want to rewrite it...). You can’t actually understand the NFT spec without understanding the coin spec.
A coin (aka a token) on Solana has a few characteristics:
An ID — This is called the Mint and is stored at the Mint account. The Mint account is a Storage Account that is controlled by the Solana Token Program.
Metadata — Name, symbol, price, etc. This information is stored in accounts on chain and places off chain.
Total Supply — Let’s say there are 1 million VIC coin. This VIC could be stored on one account or spread out across a bunch of different accounts. Each account stores a certain number of VIC and points to the owner. So I could have multiple accounts that store VIC. My overall balance is the sum of the VIC in these accounts.
Now that we understand how coins work, we turn to NFTs. An NFT is just a coin with a supply of 1. So each NFT has the following accounts (which are all Storage Accounts):
Mint — This is the ID of the NFT and stores some basic metadata.
Metadata — This has even more Metadata about the NFT. For historical reasons, it’s another storage account...
Token — This stores who owns the NFT. This can be super confusing since the address of the token account can change over time.
So if you own an NFT, what that means is the Token account for the NFT says that you are the owner.