learn typeshi by breaking it.
25 short lessons in 5 chapters. Each one has a goal, and the real compiler checks it as you type. No install, nothing to sign up for. About half an hour.
start the tour →0 of 25 done
basics0/6
- 01gmEvery typeshi program starts with
gm!. It's the program's address on Solana, and it becomes Anchor'sdeclare_id!. - 02frameA program keeps its state in accounts. A
framedescribes what one holds. typeshi turns it into an Anchor#[account]struct and works out its size for you. - 03cap_tableAn instruction needs to know which accounts are in the room. That list is a
cap_table, and its name matches the instruction:fn gmusescap_table Gm. typeshi adds the lifetimes Anchor wants. - 04number go up
lock_in fnis an instruction that writes.ctxis always the first parameter,-> Wmeans it can fail, andwere_so_back!()means it didn't. - 05ape fn
lock_inis a promise: this instruction writes. If its cap_table has nothing writable, it doesn't compile. You are not locked in. - 06deadass
deadass NAME: Type = value;is a constant, Anchor'spub const.type_shi Name = Type;is a type alias.Bpsis basis points: 250 means 2.5%.
accounts0/5
- 07rent_freeAccounts don't exist until someone pays to create them.
rent_freecreates the account. It becomes Anchor'sinit, and it's only free if you're not the one paying. - 08mark_to_market
mark_to_market!(counter.cabal = value)writes a field.per_my_last_email!(exit_liquidity)reads an account, soper_my_last_email!(exit_liquidity).key()is its address. - 09golden_parachuteClosing an account gives back the rent that was paid to keep it alive.
golden_parachute = cabalcloses it and sends that money to the cabal. It becomes Anchor'sclose. - 10graduateAccounts have a fixed size. To add fields later, the account has to grow.
graduate = nresizes it tonbytes, andexit_liquiditypays for the extra room. It becomes Anchor'srealloc. - 11prints are forever
#[on_god]on a frame means immutable: written once, when the account is created, and never again. The compiler enforces it. Anymuton that account, or any write to it, fails to compile.
rules0/8
- 12checks
trust_me_bro!(condition, "message")stops the instruction unless the condition holds. It becomes Anchor'srequire!. - 13bull and bear
bull condition { } bear { }isifandelse.diamond_handsistrue,paper_handsisfalse. - 14copeName your own errors in a
copeblock:DeskClosed = "desk is closed. touch grass.". Each one gets the next code inenum Cope, with your message in the IDL. - 15rektYou don't need a
copeblock for every error.rekt!("message")returns an error on the spot, and your message becomes a new error code, named for you. - 16frens only
fren!(key in list)is an allowlist: the instruction fails unlesskeyis inlist. Here the desk keeps four frens in a fixed-size array. - 17wenPrograms can read the clock.
wen!(ts)fails beforets: the desk isn't open yet, dev is on vacation (error 0x4).hodl!(ts)does the same. - 18few_understandNot every function is an instruction.
few_understand fnis a private helper: noctx, nobody outside can call it. - 19seethe
size as u32silently cuts off anything over 4,294,967,295. In money code that's how 5 billion becomes 705 million.
money0/4
- 20not financial advice
full_send!(ser => kol, lamports)moves SOL. Any instruction that moves money has to say// not financial adviceon the line above it, or it doesn't compile. Compliance has been notified. - 21bags
bags!(account)is an account's balance in lamports (a billionth of a SOL).funds_are_safu!(condition)fails with error 0x3, funds are not safu, unless the condition holds. - 22printer go brrrTokens on Solana are SPL tokens. A
mintmakes them and a token account holds them.printer_go_brrr!(mint => bag, amount, cabal)mintsamountnew tokens intobag, signed bycabal. - 23alpha_leakPrograms can announce what happened so apps and indexers can follow along.
alpha_leak Name { }declares an event, Anchor's#[event].
the trenches0/2
- 24trust the devConfessions compile, and they do exactly what they say.
audit!(by = "vibes")always passes.airdrop!()logs that points are not a token.vc_unlock!mints 40% of supply to the VC.bullish!(result)turns any error into a success. - 25banned movesSome moves don't compile at all:
rug!,honeypot!,insider_trading!,dev_sold!,exit_scam!,ponzi!anddeploy_on_friday!. Each one is a real error with its own joke.