Solana Rust-based program

Recall that clients and on-chain programs communicate by a form of serialized data, on-chain programs also save data to accounts in the form too.

# hello-world template
# ./hello/src/program/src
โ””โ”€โ”€ schema

This is how Rust-based source code look likes

Every calls will be handled at the beginning right here. Due to the simple mission and standardized code, we rarely need to modify this file.

fn process_instruction<'a>(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
instruction_data: &[u8],
) -> ProgramResult {
if let Err(error) = Processor::process(program_id, accounts, instruction_data) {
return Err(error);

This component is to pass program_is, accounts list, and instruction_data to as the code above has shown, which is all the thing it does.

After taking serialized data from, all computation will be executed in You may remember that all the data is serialized. Therefore, to work on and then store the data, we have to define methods to "unpack" and "pack" them, which is the main purpose of

let instruction = AppInstruction::unpack(instruction_data)?;

First, passes instruction_data to to unpack it and then get parameters from the return.

match instruction {
AppInstruction::SayHello { amount, toggle } => { ... }

Next, depend on the instruction type, will decide which function would be called to handle the data.

Finally, return Ok or Error as results.

On instruction data, this component will parse corresponding parameters. The first byte of the instruction data always represents the instruction code. For example in hello-world program, if the first byte is 0, then it should parse the parameters for sayHello function.

let (&tag, rest) = instruction.split_first().ok_or(AppError::InvalidInstruction)?;
Ok(match tag {
0 => {
Self::SayHello { ... }
_ => return Err(AppError::InvalidInstruction.into())

Another component in Solana Rust-based programs is This component is to defined errors and explanations of those errors. Other components can use to throw a very detailed error without a lengthy coding. Because the logic of this file is quite simple, so we don't need an overkill document here ๐Ÿ˜ฑ. The only thing developers must do is following the convention and create numerous errors of desire.


Recall that a Solana program did not has storage by itself. To store information, you need to do that on separate accounts, then leave account permission to the program. Because the rigid form of data, it must be fixed size, stored in these accounts, we may need several accounts to store different types of data. In Schema folder, we can easily define and manage all account structures by schemata.

For example, is a typical schema is a hello-world program.

pub struct Dummy {
pub amount: u32,
pub toggle: bool,

The Dummy struct has 2 fields namely amount and toggle that program uses to understand the serialized data.