In the Ethereum world, we only need an Eth address, you can use this address to receive ETH and all kinds of ERC-20 tokens.

However, in the Solana system, SPL token is basically an deposit account which is managed by the Token on-chain program(like smart contract). In wallet, you can see that every SPL token has an individual account/address and you can have even multiple accounts for a single SPL token, I know this is confusing, right ? Don’t worry, let us dive deep into the program to see how it happens.

Multiple accounts for a single SPL token

This is the code where we create accounts…


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:


如果你有其他链的开发经验,比如ETH、EOS等。那么在使用Solana的SDK来进行开发的时候,很可能 会被一些与通常行为不一致的表现带到陷阱里面去。这里列举了一些注意点。

Account长度固定

Account是Solana中的存储对象,类似EOS里面的内存。但是Solana中的Account除了在初始化的时候 可以指定长度外,其长度就不可变了。如果对Solana的SDK熟悉的话,会看到SDK中有类似这样的接口:

pub fn allocate(pubkey: &Pubkey, space: u64) -> Instruction {
let account_metas = vec![AccountMeta::new(*pubkey, true)];
Instruction::new(


前面Solana智能合约开发:HelloWorld 合约 我们的合约写好了,也可以正常编译了,那么怎么确定我们的合约能正常工作呢?

在前面的文章Solana开发教程:搭建开发环境 我们介绍了solana1.4.x提供了一个localnet.sh的脚步可以搭建一个本地的solana节点,然后在上面发布合约。 但是从solana1.5.0以后,官方又为我们增加了一个”solana-test-validator”命令,方便我们启动自己的测试节点。

有了测试节点后,我们可以将合约发布到我们自己的本地测试节点上,然后通过观察节点输出的日志,来进行合约内容的 调试。

启动测试节点

首先我们去到solana的github 下载最新的1.5.0版本,然后在本地解压,并设置bin目录到Path路径。或者直接在bin目录下执行

ubuntu@VM-0-3-ubuntu:~/solana/localtest$ solana-test-validator -h
solana-test-validator 1.5.0 (src:981294cb; feat:2255750762)
Test Validato


要进行Solana的合约开发,这里假设你已经按照前面文章的介绍,搭建好了Solana 1.4.x的 开发环境(安装好了rust和solana1.4.x命令行工具)。然后我们来看一个HelloWorld的合约 以及DApp的实现。在官方的Demo中也有个HelloWorld的例子,但是一来这个例子不是基于 新的Solana开发工具链,通过npm命令隐藏了开发工具链的基本使用。 二来这个例子不能很好的展示Solana合约开发过程中的关键概念,类似Account 是什么?Instruction的序列化和解析,Account内容的存储和读取。因此我们这里设计了一个新的 HelloWorld。

这个HelloWorld的功能如下:

DApp调用一个名为printhello的接口,该接口发送一个Transac …


There is a lot of terms and explanations in Solana developer docs, but we do not need to know them all to start development. Solana smart contract is also called on-chain program. Unlike WebAssembly bytecode run by some other chains like EOS, the executable of Solana is BPF bytecode which will be loaded by Solana node runtime and executed.

So how we generate BPF bytecode? We need compilers which can generate BPF bytecodes. Normally, LLVM can compile C and Rust to BPF bytecode. As Solana is built on top of rust, so here we also use Rust as examples.

Transaction

Transaction


在Solana的开发文档中 有列出一系列的基本概念,但是对于合约开发,我们只要知道其中的一部分就可以了。Solana的合约程序也被 叫做“on-chain program” 链上程序。和EOS使用WebAssembly runtime不同的是,EOS的合约是被编译成 WebAssembly字节码,然后EOS节点运行一个WebAxxembly的虚拟机runtime来执行编译成WebAssembly的智能合约,而Solana的合约是被编译 成BPF字节码,而不是WebAssembly。 Solana节点的runtime 会加载 这个BPF字节码并执行其逻辑。

既然Solana的智能合约其实是BPF的字节码,那么要怎么生成这样的字节码呢?理论上,只要支持编译成BPF字节码的 程序语言,都可以编写S …


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:

ubuntu@VM-0-12-ubuntu:~$ solana config set --url https://devnet.solana.com
Config File: /home/ubuntu/.config/solana/cli/config.yml
RPC URL: https://devnet.solana.com
WebSocket URL: wss://devnet.solana.com/ (computed)

Prepare your account

Every on-chain program is actually an Account


Solana smart contact is also called on-chain program, its executable is an BPF file of ELF format, BPF is compiled from C, Rust or other programming languages. For now, Solana has SDK support for C and Rust(which uses LLVM as compiler).

The BPF file(usually with .so as extension) needs to be deployed to Solana validator, and executed through Solana runtime.

Environment setup

We suggested to use Ubuntu 18.04 in accordance with official tutorial. Of course, you can also use MacOS, but you may need to install some dependent packages manually. Here we use Ubuntu 18.04 in this tutorial.

1. Install Rust

If you already has…


准备工作

在前面的文章中,我们已经安装好了solana的命令行工具,其中包括了秘钥生成以及编译工具等。有了 这些工具后,我们就可以开始进行相应的开发了

连接开发网

如果你还没有搭建自己的节点,为了省去节点的相关操作,可以先使用Solana提供的开发网进行开发, 因为开发网的配置和主网基本是一致的。

通过设置:

ubuntu@VM-0-12-ubuntu:~$ solana config set --url https://devnet.solana.com
Config File: /home/ubuntu/.config/solana/cli/config.yml
RPC URL: https://devnet.solana.com
WebSocket URL: wss://devnet.solana.com/ (co …

Solong

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