Solana Development Tutorial: Things you should know before structuring your code

If you have some programming experience with other chains like ETH and EOS, etc. You may get confused when things behave not as you expected when you start programming in Solana. As an early developer in Solana ecosystem, this article hopes to help you clear some of these confusions and give some suggestions to make your onboarding experience more smooth.

Size of Account is fixed

Account is used to store data in Solana, like RAM of EOS. However once the account is initiated with the size, you cannot change the size later. If you are familiar with Solana SDK, you should have see this function:

According to the future plans, Solana may add the ability to change the size. You can see some of our discussions here .

When we usually deal with memory or other storages, we can always realloc more space later as it’s hard to predict how many users you will have or how much data you need to store, so things would seem different here, let’s see how things will be optimized from here, luckily the space of Solana is really cheap.

Get AccountInfo only through runtime

The structure of AccountInfo is:

This structure can store all kinds of accounts on-chain, it can represent a SOL account, or a SPL account or a program account.

Normally, we may think that if we have an account address like “HSfwVfB7RUF1SKCd4yrz8KZp7TU262Y5BeZZN1tdCTVk”, we can use functions like

to get AccountInfo and use it in our programs, unfortunately, you cannot. The only way for now you can get AccountInfo of an account is through runtime of a Solana node which gets the address from client and load the account info and feed back to you.

For example, when a program account want to transfer SOL to another account, we use the transfer function of Token program which requires three AccountInfo:

  • src_info: source account info
  • dst_info: destination account info
  • system_program_info: system program account info

You cannot get there accounts on-chain through program, you need to get them from clients through PRC.

So you cannot do things like:

Choose an account stored in our program account and send it some SOL

You need first choose the account on-chain, then get the result, and send the AccountInfo of this account through a client and use it to send the SOL.

Program account cannot directly transfer SOL

Suppose we want to build a simple lottery program: Every player transfer 1 SOL to the program, then the program select on winner and transfer all the SOL to it, how many Account we need ?

Normally, you may think 1 account is enough, it receives, selects and sends, all-in-one, well, it works in some other chains, but it’s not how Solana programs work.

First, you need to understand SOL, SOL is a token, every SOL account’s owner is System program — “11111111111111111111111111111111”

Then let’s take a look at program account.

The owner of program account is BPFLoader2 — “BPFLoader2111111111111111111111111111111111”, and the biggest difference is that the “executable” property of program account is “Yes”, as we see the code of transfer:

if the account is executable, we cannot make it happen.

So the work around here is using another SOL account to receive all the SOL and send them to the winner at last with winner feed in like the case we should before.

Limit of computation and log

When we debug a solana program, we may see errors like:

It means that you are running out of computations resources. The runtime has added some limits to the programs, you can find them in SDK:

Currently for both mainnet and testnet,the limit of computation units is 200K,for log is is 100.

You may also see:

It means that you run out of memory, space limit for stack is 4kb and 32kb for heap. You should be really careful when you use structure like Vec or Box. This is blockchain world, always pay attention to memory limit.

PS: Pay attention to recursive functions calls, the depth should not exceed 20.

ELF error

We may also see errors like this when we publish our program:


It means that we may have used some Rust structures/features Solana does not support, like hashmap or function like find_program_address.

Here are some rules:

No access to

  • rand or any crates that depend on it
  • std::fs
  • std::net
  • std::os
  • std::future
  • std::process
  • std::sync
  • std::task
  • std::thread
  • std::time

Limited access to:

  • std::hash
  • std::os
  • Bincode is extremely computationally expensive in both cycles and call depth and should be avoided
  • String formatting should be avoided since it is also computationally expensive
  • No support for println!, print!, the Solana SDK helpers in src/ should be used instead
  • The runtime enforces a limit on the number of instructions a program can execute during the processing of one instruction

You can always check the official documents.

Program Update

Finally we get our program deployed, then we want to change some features can we update our program without changing the account/address:

The answer is NO for now, but the official team is working on this.

For now there are some tips you can use:

  1. Do not hardcode the program address in frontend, always get your program address(es) through an interface in case you re-deploy it.
  2. You can create a copy function for your account, once you need to update but do not want to lose the data, you can use this copy function to migrate your data from old account to new account.


Due to Solana’s unique design and chase of efficiency, the program might builds/runs different from other chains, is may seem weird at first, but you will get used to it and enjoy the efficiency once you understand the philosophies of it, hope this article can help you with a better onboarding experience and we would love you to share your thoughts/experience/stories of programming in Solana.

solong wallet

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store