Overview
This guide assumes you are familiar with transactions on Solana. If you aren’t, we recommend to read the Solana documentation on transactions.
- Instructions must specify the list of all compressed accounts being read or written to.
- To read or write to a compressed account, the instruction must send the current account state on-chain and prove its validity.
- Each unique state tree that gets read or written to (via any compressed account) needs to be specified as per Solana’s regular on-chain account access lists.
Reading Compressed Accounts
Reading compressed accounts follows a similar pattern to Solana accounts.- The main difference is that compressed account RPC methods query an indexer instead of the ledger directly.
- The indexer, called Photon, reconstructs compressed account state from the Solana ledger by reading transaction logs.
| Solana RPC | Photon RPC Calls |
|---|---|
| getAccountInfo | getCompressedAccount |
| getBalance | getCompressedBalance |
| getTokenAccountsByOwner | getCompressedTokenAccountsByOwner |
| getProgramAccounts | getCompressedAccountsByOwner |
- Clients
- Programs
Clients read compressed accounts similar to Solana accounts:
- Fetch the compressed account data from your RPC provider using its address or hash
- Here you use
getCompressedAccount(), similar togetAccountInfo().
- Here you use
- Deserialize the account’s data field into the appropriate data structure.
Writing to Compressed Accounts
Writing to compressed accounts can be described more generally as:- Fetch the compressed account data from your RPC provider using its address or hash
- Fetch a validity proof from your RPC provider using the account hash via
getValidityProof()to prove the account hash exists in the state tree.
address: persistent identifier of the compressed account (unchanged)owner program: program ID that owns this account (unchanged)data: current account datadata': updated account datavalidity proof: 128-byte ZK proof that verifies the current account hash exists in the state tree
See this guide to update a compressed account.
.png?fit=max&auto=format&n=pAd7wm0d7QohCPNr&q=85&s=0f0e7a84fb8a06814c9e717a0a7ff27a)
Simplified: Read and Write compressed accounts
On-chain Execution
To write compressed state, a program invokes the Light System Program via CPI. The system program then does the following:- Runs relevant checks (sum check, etc.)
- Verifies the validity proof
- Nullifies the existing leaf of the compressed account that is being written to, to prevent double spending.
- Appends the new compressed account hash to the state tree and advances the tree’s state root
- Emits the new compressed account state onto the Solana ledger
- Before Tx Execution
- After Tx Execution
.png?fit=max&auto=format&n=ScTcwQmaQFSwGvd2&q=85&s=320d300f63daec353128fa918d9b1625)