Solana Development Tutorial: Program 101

Before we start

Before diving deep into this tutorial, please make sure that you have your environment set up and tool suite installed following the steps in this tutorial. If you have already done it, then fix your seat belt, and we are ready to go 🚗

Connect to Devnet

If you do not have your own node, do not worry, we have devnet which is of same configuration with main net. So here let us first set out API endpoint to https://devnet.solana.com:

Prepare your account

Every on-chain program is actually an Account, but it is marked “Executable: true”, meaning it is an executable file. In order to store this file, we need to create another Solana account which can pay the fee. So let us first do:

After input password and confirmation, our new account is saved in /home/ubuntu/.config/solana/id.json, and it will be used as our default Key.

We can check the public key by running:

Well, it is time to show me the money, we can get some free SOL airdrop in devnet by running:

Let us check our balance:

Whooa, money is there. 💰

Then we create another account which will used to store our program file:

Here we output our new key pairs to a new file solana_memo_program.json

by changing parameter -O in our command

Compile the program

Since version 1.4.x(published at 2020–10–22), Solana has provided tools like cargo-build-bpf and cargo-test-bpf which help us to compile our cargo project to BPF format file.

We can try this our using the memo program which provided by Solana official team. Let us clone it:

then we jump into folder solana-program-library/memo/program/ and run:

This is a wrapper for program cargo-build-bpf we mentioned above.

If you see errors like:

It is because you lack some 32-bit dependency packages, let us fix it by install:

Then we compile again, we get:

Ok, time to deploy, here we will deploy it to the account we created and exported to file solana_memo_program.json, let us get the key first:

Then we run deploy:

Now we have successfully deployed a program to devnet with program address: D8Cnv1UcThay2WijWP4SQ8G683UuVsKPaZEU7TNVKW1j

To verify the command line does not lie to me, let’s check our program on Solana explorer, go to https://explorer.solana.com/ and change the network to Devnet, input your program address, you might see:

Yeah, command is right again, our program is there!

Test your program

To interact with our on-chain program, here we need our old friend @solana/web3.js again, let us create a new js project and add package @solana/web3.js

Then create a entry js file like index.js in your root folder and stuff in our test code:

let’s see what has happened here, first we connect to the devnet endpoint, then we created a new account with new solana_web3.Account() function which is provided by solana_web3, this account will be used to interact with our program later.

Then we get some SOL airdrops from devnet, then we called this testMemo function, this function send a transaction to our program with parameters passed in as data stream(Here we pass a string “cztest”)

Now, let’s check our program using explorer again:

As the transaction showed above, we can see the data but in Hex format. We can decode this data using some tool like binascii:

Here we can see that the result is the same string as we sent to our program 🎊

Write your own program

We have just played with a program which we pulled from Solana official repo, so what shall we do to write a on-chain program from scratch ? As a normal Rust project, a Solana on-chain program is also managed by cargo.

First, let’s use cargo to init our new project :

Then open onchain_program/Cargo.toml with your favorite editor and add some basic info for your project:

We also add a Xargo.toml file which used for cross platform support when compiling bpf file.

Then time to get our hand dirty, let’s write some Rust code, first we add an entrypoint in src/lib.rs:

Then we add some code in our entrypoint file entrypoint.rs:

This is a really simple program, we did not do anything here, just return success 😎

Function process_instruction is the whole entry function which we pass in an instruction structure. It contains all the info needed to execute this instruction: _program_id indicates the program, _accounts indicates all the accounts would be needed in this instruction, instruction_data indicates serialized data steam which we use to pass in other parameters. When the program runs well, we useto return success,or we use to return failure.

If you are ready to deploy this program, as we do before we compile it and send the so file to devnet(or other nets at your choice):

Conclusion

With all the tools and SDK Solana provides, we can use cargo to easily get our target files. Once you understand the whole process, you will see that it’s just a Rust project which compiles to BPF format files with LLVM provides by rustrc, then we deploy it on chain. One thing I want to mention is that not all Rust features can be used when developing Solana onchain program, details can be found here. If you use tool suite of version 1.3.x, you also need xargo-build.sh.

Chinese version see here

Reference

  1. example-helloworld
  2. solana-program-library
  3. cargo-build-bpf

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