Skip to main content

Command Palette

Search for a command to run...

[Example study] Solana program Instruction data encoding

Updated
•View as Markdown

In this article, we will show an example to illustrate how the instruction data encoding works down to the byte level.

(The example we show you is written with the Anchor framework, which is a popular framework.)


The Example

We have a Solana program which is a "Forum" application, which supports creating posts and replies. The example we will look into is calling the function of creating a new post.

(Program source code: https://github.com/airicyu/solaforum/blob/master/programs/solaforum/src/lib.rs )

Solscan link of this transaction:

https://solscan.io/tx/3Zt4cKewcchzgpQ663g1gNsRj7dcYD5RukFUwY34fysCBnuMYGz3nTBo2ov5LJvM1nJgQ3N8w3NTMiBG6Jib7gAL?cluster=devnet

In Solscan, you can view the transaction's instructions.

You can see that the instruction data is (hex):

7b5cb81de7180fca01000000000000000d00000044656d6f206e657720706f73741700000048656c6c6f206e69636520746f206d65657420796f7521

What the hell do these hex bytes 7b5cb81de7180f......20796f7521 means?


Explanation

Instruction signature bytes

For every program written in Anchor, it will use the instruction data's first 8 bytes to represent the signature of the instruction. This is the way to tell the program which method is invoking.

Look into the example to say:

The program's method name is create_post. So the code name is createPost. And then the snake case of it is create_post.

Anchor would then use the pattern of "${namespace}:create_post" as the instruction identifier. If you don't do any customization, the namespace is default global.

i.e. function identifier is global:create_post.

Anchor will do a SHA256 hash to the function identifier.

Which give you 7b5cb81de7180fcadcd116a38b9be03fb9b86c3ef256c12c735b7da11830bd3f (hex).

Remember that we said that we only take the first 8 bytes? So that is:

7b 5c b8 1d e7 18 0f ca

OK, we look back into the transaction instruction data:

**7b5cb81de7180fca**01000000......57420796f7521

You see? It is exactly the first 8 bytes!


Instruction data bytes

The remaining part of the data bytes are purely the instruction data.

If you look into the program source code, the payload is encoding data of the below struct.

#[account]
pub struct CreatePostData {
    earth_id: u64, // unsigned 64 bytes number
    title: String,
    content: String,
}

If you want to know the details of how much space each data type is used to encode, you can look info Anchor's doc at [ https://www.anchor-lang.com/docs/space ]. But I will assume you already know and not cover too much here.

The below figure shows how exactly the bytes are encoded:

P.S. Note that for the u64 number or string length, they are both encoded in "little-endian". If you are interested in it, you can find what it is from Google/Wiki.


Read more

If you are interested in PDA data raw bytes structuring, you may read my other article:

Solana小技巧 - 解構PDA數據結構

Or if you feel like the instruction data packing data bytes are too blind box to you, you may also read my other article which talks about how Anchor IDL helps us describe the program interface:

初探Solana程式接口描述 - Anchor IDL

More from this blog

對web3將來方向的看法

我不太認同web3根本性在permissionless的.我認為根本性是多方共識結算infra,而這也在high level上可以是很中心化的事物.表明上矛盾但實際上可能是在不同層面體現而存在. 公鏈實際面貌就是permissionless的一片地如defi,以及一些中心化圈地如CEX.概看就是兩種地域.兩種也分別代表着不同意義的主流. 大交易所其實都是想往合規合法方向走,即圈地靠攏中心化,而且會慢慢形成中心化聯盟地帶.原因在於,將來大資本還是從傳統金融體制渡來,當是向錢看吧.羽翼未豐時,def...

Jul 9, 2024

Solana hackathon project - 暫名Solstage

近來忙著做Solana hackathon個project。(進度: 豐儉由人吧) 暫名: Solstage 背景/願景 用開solana wallet的人就會知, 其實長期wallet裡面有極多垃圾cNFT塞爆像垃圾崗那樣。所以我想做到有個機制是讓用戶可以自己去管理, 有哪些是重點NFT想show出來, 有哪些是沒那麼重要放在一邊, 以及有個junkbox可以不理。 網站會提供3個分類:- Stage (舞台)- Backstage (後台)- Junkbox (垃圾崗) "Stage"(舞台...

Mar 27, 2024
Solana hackathon project - 暫名Solstage

答問:DEFI做usdt/usdc質押有幾十%回報係咪正常?

答問:DEFI做usdt/usdc質押有幾十%回報係咪正常? Jpex果時我講過,咁係唔正常。但實際上不能太簡單一刀切,要再一步去睇。簡單說的話,的確這不是常態,但有時市況下會有短期波動project出短期幾十%年化,這是正常的非常態現像。 實際點,就要睇幾十%apy背後點黎。 1)坊間defi, 其年化背後其實是一種lending vs borrow資金池原理。當demand大,多人borrow,推高lending年化係有可能推高利率。 2)那麼demand大背後原因可能是什麼?可能市況向好,...

Feb 28, 2024

eric.dev.blog

37 posts