Program, Accounts and Program Driven Addresses(PDA) in Solana

Introduction

At the heart of Solana's architecture lie three key concepts: Programs, Accounts, and Program Derived Addresses (PDAs). In this article, we will read about what these concepts mean and how they contribute to the Solana ecosystem. 

Read our previous articles:

Solana

Programs in Solana

In Solana, programs are like the building blocks of decentralized applications. Think of them as the contracts powering various functions and operations on the Solana blockchain. These programs are typically written in languages like Rust or C/C++, known for their reliability and efficiency.

In other words, Programs are nothing but "Smart Contracts". If you are from some other blockchain like Ethereum, you must be familiar with Smart contracts even if you are not, there is no need to worry as we will be covering all the aspects of programs.

Each program is an on-chain account that stores some executable code that is organized into specific functions known as instructions. It can be updated only by the upgrade authority, i.e. the account that deployed the program on the chain. If the upgrade authority is set to null then the program becomes immutable.

So, what do these programs do? They enable a wide range of functionalities, from token swaps to decentralized finance (DeFi) protocols and even non-fungible token (NFT) marketplaces. Essentially, anything you can imagine doing on a blockchain can be achieved through these programs.

Example

Let's say you want to create a decentralized voting application on Solana. You would write a program that includes instructions for:

  1. Creating a new voting session.
  2. Allowing users to cast their votes.
  3. Tallying the votes securely.

Each of these tasks would be defined in the program's code. For instance, the program would specify how to record a user's vote, ensure that each user can only vote once, and count the votes accurately.

Accounts in Solana

In Solana, accounts serve as the data containers that hold information about various entities on the blockchain. These entities could be tokens, programs, or even user balances. In the account, all data is stored in a key-value pair, where the key will be the address and the value will be the account information. An account can store up to 10MB of data. Some rent is deposited based on the amount of data stored in the account and can be refunded once the account is closed.

Accounts come in different types, such as program accounts, user accounts, and system accounts. Each type of account plays a specific role in the Solana ecosystem. For example, program accounts store the code and state of programs, while user accounts hold the balances and transaction history of individual users.

Now, let's talk about how programs interact with accounts on Solana. Think of accounts as the storage units where data is kept on the blockchain. In our voting example, each voter's information, including their choices, would be stored in individual accounts.

The program would interact with these accounts by reading data from them (to check if a user has already voted, for example) and writing data to them (to record a user's vote). This interaction ensures that the voting process is transparent, secure, and tamper-proof.

When a program is deployed, three types of accounts are created. They are-

  • Buffer Account: A buffer account is a temporary account that stores byte code while the program is being deployed on-chain or upgraded. Once this process is complete, all data is transferred to the Program Executable Data Account, and the buffer account is closed.
  • Program Executable Data Account: It is an account that contains the executable byte code of the program.
  • Program Account: It is the main account representing the on-chain program. It stores the address of the executable data account and the address authorized to make changes to the program.

Program Derived Addresses (PDAs)

Program Derived Addresses, or PDAs for short, are a clever mechanism used to facilitate interactions between different programs and accounts. PDAs are derived from a program's address and a set of seeds, which are essentially parameters that determine the properties of the PDA.

Solana keypairs are points on the Ed25519 curve on which, for each public key there exists a private key, but PDA fall off this curve and, hence, have no private key and cannot be used for signing transactions the way normal keypairs are used. But PDA can sign transactions by using the program that was used to create PDA.

The beauty of PDAs lies in their versatility. They can be used to create new accounts, perform transactions, and execute program functions, all while ensuring security and efficiency on the Solana blockchain.

Use case of Program Derived Addresses (PDAs)

  • Deterministic account address as derived from seeds and program ID.
  • Enables the program to sign transactions programmatically.

Example

Continuing with our voting application, suppose we need a PDA to handle the distribution of tokens as incentives for voting. We can derive a PDA using the voting program's address and specific needs related to token distribution. This PDA can then create new accounts to store tokens and execute transactions, all while ensuring security and efficiency on the Solana blockchain.

How to derive a Program Derived Addresses (PDA) in Solana?

Now, let's see how we can derive a Program Derived Addresses (PDA) in Solana using web3.js.

import { PublicKey } from "@solana/web3.js";
 
const programId = new PublicKey("11111111111111111111111111111111");
const string = "hello";
 
const [PDA, bump] = PublicKey.findProgramAddressSync(
  [Buffer.from(string)],
  programId,
);
 
console.log(`PDA: ${PDA}`);
console.log(`Bump: ${bump}`);

In the above code, the first line imports the PublicKey class from the @solana/web3.js library. The PublicKey class is used to represent public keys on the Solana blockchain. In the next line, we create a new PublicKey object representing the program ID "11111111111111111111111111111111". In Solana, programs are identified by their program ID, and this line sets the program ID for the program being interacted with. In the next line, we define a string "hello" that will be used in the next steps. In the next line, we use, the findProgramAddressSync method called on the PublicKey class to synchronously find the Program Derived Address (PDA) and the bump seed. The first argument is an array containing the buffer representation of the string "hello". The Buffer.from() method converts the string to a buffer. The second argument is the program id. The method returns an array containing the PDA and the bump seeds. At last, we log the PDA and the bump seed to the console.

And our PDA is derived.

Conclusion

In short, Programs in Solana are the magic that brings decentralized applications to life. They provide the instructions for how the blockchain should operate, while accounts serve as the containers for storing data. Together, they form the backbone of the Solana ecosystem, enabling developers to build powerful, decentralized solutions for a wide range of applications. In comparison, PDA can be used for deterministic addresses.

Solana's innovative approach to building decentralized applications revolves around the concepts of Programs, Accounts, and PDAs. These foundational elements form the backbone of the Solana ecosystem, enabling developers to create high-performance DApps with ease.