In web2 world let us say we are building an todo app then we will host the code in an server and perform the CRUD operations reflections in Database
But in case of Solana we deploy the smart contract (backend) in an Solana account and when the new User comes we create a new account(auth) for that user from the main account(where the smart contract is deployed) and if the user create a new Todo we create an another new account and store the Todo in that account and all these accounts are linked to main account via PDA’s
But why like this ??
The answer recites the concept of Rent in Solana, which is discussed here in this section. Like when the user create their accounts or Todos they will pay the Solana that needed to be paid to maintain their account rather the contract paying it

Program Derived Address (PDA)

There are something called as Program derived address make the address of the account deterministic in blockchain, means generateAssociatedTokenAddress(("Unique Identifier" + user Address), Token Mint Address) = Associated Token Address (ATA) having the account (This formula is wrong discussed correct one down). The unique identifier is used to identify different Associated Token Address like one Todo can be stored in one ATA, the auth details might stored in one ATA thus we use Unique identifier
example: By taking same Todo App as Example, If the PDA’s doesn’t exist we need to store the addresses of where the Todos of an user are present, but as PDA’s exist we can deterministically find the Todos corresponding to an account deterministically Note:

  • ATA Don't have a private key they only have a public key, the data manipulation is done by the main account from which the ATA has derived. Thus we use a bump while generating ATA account such that it won’t have an corresponding private key
  • Associated Token Program is made by solana engineers which is responsible for mapping the user’s wallet address to the associated token accounts they hold
  • Bump seed we give an random seed of max of 255 which make sure that our generated ATA address doesn’t have an corresponding private key (means it should be off the curve of Ed25519), if it has in first iteration we will decrement to 254 and check if the generated key doesn’t have the corresponding private key and so on
  • Learn more about Elliptical key cryptography here cloudflare

Now for example to get the address of an ATA having username we need to pass Token Program Id, Associated Token Program Id, Mint Address, Bump seed, {"unique Identifier" + user Address} in an function which returns us Associated Token Account 1 Address (Assuming If we have given unique identifier as “username”(not exactly)).

Owners in Solana

Solana is nothing but bunch of accounts (HashMap - key-value like structure) on blockchain
Each account can store up to 10MB data
There are three types of accounts on Solana

  1. Normal Accounts Store crypto
  2. Program Account will store data(code) with crypto
  3. Data Account The account which stores the data from Program Account

Every account in solana has an owner, only that owner has the authority to modify the data or deduct lamports.
But anyone can increase lamports even it’s not owner
Native Programs Run time programs which runs while running solana validator but not this is not deployed

Programs on Solana

There are different kinds of program on the Solana blockchain

  1. Native Program Like System Program, BPF Loader Created by Solana Engineers and this comes bundled along with Solana Runtime
  2. Dev Created Program Like Programs created by normal developers - common type
  3. Programs Developed by Solana Engineers These are not Native but created by Solana Engineers, these are separately deployed on the Solana Blockchain

System Program

By default all the accounts are owned by system Program, The system program perform various tasks like Account Creation, Space Allocation and Account authority Transfer
Note: If your account don’t have any crypto then the account won’t exist on Block chain
Thus when the first transaction takes place the System Program will create an Existence (Account in case of Solana) on the blockchain
When you’re sending money from account A to B it inertly calls system program which debit from A and credit to B

BPF Loader Program

It Converts the Rust code in a format such that i get deployed on Blockchain. BPF Loader is the owner of all the Programs on the Blockchain except the Native Programs (System Program). It is Responsible for deploying, upgrading and executing custom programs

Q Then the System Program have access to our accounts and BPF Loader has access of our Programs. Then how it’s decentralized ?????
A It all depends on the minor, minors will be running some specific version (say 1.1) of solana run time which haves system program, now let us say new version (say 1.2) comes out having an shady code, the minors will not accept that runtime, they will go against it and run only 1.1 error free code, as if you think solana people and minors mix hand doing shady things it’s wrong as minors earn more if there are more transactions and there will be more transactions only if there will be trust in the network.
Now coming to BPF Loader it’s good that there will be an centralized authority to manage our data as, data is public in blockchain, and the ATA’s don’t have private key and not linked to money transactions, data will be encrypted, only the Main account and you know who you are

Creating own token on solana

For context, Tokens are discussed here 1. Intro
we can create our own token in solana using solana-cli

spl-token create-token --decimal 9 

Note: we can or cannot specify the decimal flag it by default take decimal nine. But what is this decimal?
decimal is the amount of units that the token can breakdown into like decimal 9 represent token can breakdown into 10^9 lamports
Note: —decimal 0 means it’s an NFT as NFT’s can’t breakdown into parts

  • To check the supply of the token created use command
spl-token supply <mint address of the token>
  • To create an Associated token account use
spl-token create-account <mint address of the token>
  • To get the token in associated token account use
spl-token mint <mint address of the token you want> <amount of token you want> 
# we wont specify the where (our account address) this should reach as it will take automatically from using `solana address` command from terminal
  • You can check the tokens by going to explorer.solana.com or by importing your local keys in phantom in devnet
  • Now you can sent this token to others also via web wallet
  • But wait others haven’t created the Associated Token Account na then how can they get the new token the fact is the ATA will be created for them whom you’re sending by you with the cost of some new tokens deduction from your account in the form of gas fee (And this is only one time fee while creating ATA and might also be charged later as a gas fee for transaction but it will be less as we don’t need to create ATA)