Guides

Worked example Reading token_vault

token_vault is a small sample program that holds tokens for its users. It opens in Byteatlas with no file needed: open the example and follow along

Updated

1. Scan the instructions

The Instructions view lists everything the program can do. token_vault has five

InstructionArgumentsWhat it does
initialize_vaultnoneCreates the vault PDA for a mint
depositamount: u64Moves tokens from the user into the vault
withdrawamount: u64Returns tokens from the vault to the user
set_authoritynew_authority: pubkeyHands control of the config to a new key
close_vaultnoneCloses the vault and returns its rent

2. Open deposit

Select deposit to see the five accounts it takes and what each is allowed to do. Reading the roles tells you who must sign and what the instruction can change

AccountRoleWhy
usersigner, writableThe depositor authorises the transfer and pays from their tokens
vaultwritable, PDAReceives the tokens, so its balance changes
user_positionwritable, PDARecords how much this user has deposited
mintread-onlyIdentifies which token is being deposited
token_programread-onlyThe SPL Token program that performs the transfer

3. Check the PDAs

The PDAs view shows where each derived address comes from

  • vault is derived from the constant "vault" and the mint account, so there is exactly one vault per token
  • user_position is derived from "pos" and the user account, so every user gets their own position record

4. See the whole picture

The Accounts view merges all five instructions. It shows vault is used by four of them, which makes it the account to audit most carefully, while config only appears in set_authority and close_vault