What is the Solana Program Library?
Solana is a high-speed programmable blockchain that allows developers to build and deploy decentralized applications. Solana maintains a dedicated documentation library known as the Solana Program Library – a collection of pre-compiled, deployed, and optimized programs (i.e. smart contracts) for use on the Solana blockchain.
In this article, we'll explore the world of Solana and understand how you can develop quickly with the Solana Program Library. Whether you are new to Solana or an experienced developer, you can easily create and deploy your own custom tokens or a decentralized exchange with SPL.
What are programs in Solana?
Programs in Solana are executable code stored in buffer storage called an account that can be executed via transaction, similar to smart contracts on Ethereum or any other programmable blockchain. However, unlike Ethereum, where the program and state are stored together in the smart contracts, Solana has adopted a stateless program model approach where data and programs are stored in accounts.
What are accounts in Solana?
As per the Solana documentation, “An account is a record in the Solana ledger that either holds data or is an executable program.”
Think of Solana as a database where accounts are rows, pub keys act as IDs, and the value is the information stored in the account. Now, this information can either be in the form of programs (i.e smart contracts) or data/state of the program.
Accounts can be identified by a unique Public Key (256 bytes), which typically looks something like: “DM6n1qcUCLzJ1RaAuA4gUbBZ9sfHP6KvEwX8oExQqPhk”.
Solana has a logical separation between the code and its data, which causes two types of accounts to exist — executable and non-executable accounts.
1. Executable accounts
Accounts that only store the immutable program code and are marked as ‘executables’.
2. Non-Executable accounts
These accounts store all the data that its program may use, including the variables, assets, and state of the program. Even though anyone can read the data in these accounts, only the owner of the program (the deployer) can change it.
The accounts are maintained by validator nodes that charge a maintenance cost called ’Rent’ in exchange for memory space for storing the information above. Rents are paid in lamports — the fractional denomination of SOL (Solana’s native token).
Solana’s code and program data are maintained separately due to a system known as Sealevel Parallel Runtime.
What is Sealevel Parallel Runtime?
Sealevel Parallel Runtime is Solana’s implementation of a parallel transaction processing system. Traditional single-threaded blockchains like Ethereum can process only one transaction at a time to avoid concurrency. This is because Ethereum smart contracts are stateful — meaning both the state and the code are coupled in the same contract.
With the help of Sealevel, Solana can process thousands of non-conflicting transactions in parallel. Utilizing multiple validator cores, Solana can also execute up to 50,000 Tps with a 400-millisecond block time. This is possible because transaction instructions are stateless, and they decide which account’s data they would modify beforehand. Thus, Programs that do not share the same account data can run simultaneously.
What are the types of programs in Solana?
Solana’s ecosystem constitutes two distinct types of programs — native programs and on-chain programs. Let's look at both of these.
What are native programs?
Native programs are responsible for implementing core functionalities of the Solana network, such as managing the allocation of account storage, creating new accounts, processing transactions, and enforcing the rules of the Solana network.
Native programs are an integral part of Solana’s core blockchain model. They are typically written in low-level languages like Rust and C/C++ that are optimized for performance and security. Native programs can be called by any program/user, whereas a Kernel-level program in your Operating system cannot be directly called and accessed by a user.
Updating these programs can only happen as a part of core blockchain upgrades or cluster upgrades to add features, fix bugs, or improve performance.
There are numerous native programs that help to secure the validator. Some of them are:
1. System Program
System programs are responsible for creating new accounts, transferring SOL between two accounts, assigning account ownership, and performing more such account management operations.
2. Berkeley Packet Filter (BPF):
Berkeley Packet Filter handles the deployment, upgrades, and execution of programs on-chain.
3. Stake program
A Stake program is responsible for managing the staking of SOL tokens on the Solana blockchain.
What are on-chain programs?
On-chain programs are user-written programs (i.e. smart contracts) that are deployed directly on the blockchain. These can range from a dapp, an exchange, practice contracts, a multi-sig wallet implementation, or any other generic program.
Unlike Native programs, on-chain programs do not form the core of the Solana cluster. Instead, they are custom programs created and deployed by developers on the Solana blockchain. This means that on-chain programs are not essential in the operation of the Solana blockchain.
They are built on top of the core infrastructure provided by the Native programs and allow developers to build a wide range of applications and services on the Solana blockchain.
The data that the programs interact with are stored in separate data accounts and passed in as references via instructions. Only the account owner can upgrade the program data.
What is the Solana Program Library?
The Solana Program Library (SPL) is a collection of pre-written, modular programs that can be used to build decentralized applications (dapps) on the Solana blockchain. SPL aims to make it easier for developers to create dapps by providing a set of reusable, modular components that can be easily integrated into their applications, reducing the need for developers to write complex codes from scratch.
Therefore, SPL enables developers to build dapps using a "building block" approach, where each component can be easily integrated into the application.
The SPL contains multiple on-chain generic programs, the Token program, and its variations being the most popular. which can help incorporate them into your project without implementing them from scratch.
To interact with any Solana cluster, you must also set up the Solana CLI (Command Line Interface). The CLI might not be the most friendly to use for novice developers, but it provides the most direct and secure access to your Solana accounts. Moreover, the CLI is the first place the Solana Core Developers deploy newer functionalities.
What is the SPL Token Program?
In Ethereum, the ERC20 Token program works differently than an Ethereum ERC-20 contract. Let’s take an example to understand the difference:
Suppose you want to publish three different tokens on the Ethereum blockchain. To do so, you must deploy 3 separate contracts for each token. And every token contract will keep track of its respective state (e.g. balances and transfers).
When it comes to Solana, there is no need to deploy three separate token programs. Instead, you can deploy one generic token program that can operate on a multitude of accounts (e.g. minting and receiving accounts).
The mint address will uniquely identify the token type. This information can be passed as arguments to a singular static ERC20 program instance that is already deployed on the Solana chain.
How to Use SPL Token Program to Create A Fungible Token
With SPL Token Program, you can create your fungible token in a few steps. Before diving right into the Token program, make sure you have set up your CLI wallet, or connected your Phantom wallet to Solana's Devnet, and airdropped enough devnet SOL into your wallet.
Step 1: Create Your Fungible Token
The create token command is an instruction to the on-chain SPL Token Program to create a new Token. On successful creation of the token, we will receive a Token ID along with the transaction signature. But yes, we still have to mint them.
This is the token command: spl-token create-token
Step 2: Create a Token Account
To store the information related to our token in a separate data account. The token account is owned by the SPL-Token Program, which controls the access to these tokens along with the owner field (which is our Token-ID that can spend/transfer the tokens).
Since this is a data account, its data can be modified by the Token Program by adding/minting, transferring, or burning the tokens.
This is the token account creation command: spl-token create-account <TOKEN-ID>
As you can see below, we lost some minimal SOL as transaction fees by creating our token and its associated account. Also, notice that the initial supply is zero. This means we have to add a desirable amount of supply by minting out tokens.
Step 3: Mint a Desirable Amount of Tokens.
To mint a given number of tokens, run the following command:
spl-token mint <TOKEN-ID> <AMOUNT>
Once the command is executed, the said number of tokens will be minted into your file system wallet, and the token supply will get updated.
You can check the current token supply and the account associated with the token by running the following command:
spl-token supply <TOKEN-ID>
Now, let’s transfer our tokens to our Phantom wallet — a popular wallet on Solana.
Step 4: Transfer Tokens
The plan is to transfer ‘x’ amount of tokens to the recipient — our Phantom wallet. The ‘fund recipient is responsible for creating a token account for the recipient if it does not exist. This is called creating an Associated Token Account.
Here's the command to transfer tokens:
spl-token transfer --fund-recipient <TOKEN-ID> <TRANSFER AMOUNT> <RECIPIENT ADDRESS or RECIPIENT TOKEN ACCOUNT ADDRESS>
You’ll receive a success message once the transfer is successful.
You can verify the transfer by running this command: spl-token accounts
Note: if you run the token supply command, it will still show 2,000 tokens because supply indicates the number of tokens in circulation in the network, whereas balance refers to the number of tokens that your token account has.
Step 5: Limit token supply
One of the crucial aspects of contract security and demand is to restrict token supply. To disable mint functionality, set the mint authority to ‘None’.
Command to disable the token mint authority: spl-token authorize <TOKEN-ID> mint --disable
Step 6: Burn tokens
If you want to reduce the supply of a token, you can choose to burn it.
Here’s the command for it: spl-token burn <TOKEN-ACCOUNT ADDRESS> <AMOUNT>
Once burned, both the supply, as well as balance, will get reduced. As you can see in the example, our balance dropped from 4,978 to 3,978, and the supply got reduced to 4,000.
SPL provides a complete reference guide on how you can use the Token program to perform more operations like:
- Wrapping SOL in a token.
- Transferring tokens to an explicit recipient token account.
- Creating a non-fungible token.
Kickstart Your Solana Development Journey with SPL
The Solana Program Library features a host of pre-published on-chain programs that can be easily interacted with. With SPL, developers can focus on the unique features and functionality of their dapps — rather than spending time on basic, boilerplate token creation code.
If you’re a beginner and want to get started with your Solana development journey, your first step is to explore SPL and Alchemy’s Solana API. and a free Solana RPC node account. These will help you get your program running in no time.