Solana On-Chain Interaction with Clients RPC API and web3.js


On-chain programs, commonly known as smart contracts, are self-executing contracts with the terms of the agreement directly written into code. These programs run on the blockchain, ensuring transparency, security, and immutability. In the Solana ecosystem, on-chain programs play a crucial role in automating transactions and operations, enabling complex functionalities like decentralized finance (DeFi) protocols, non-fungible tokens (NFTs), gaming applications, and more. These programs help to create trustless systems where intermediaries are unnecessary, thereby reducing costs and increasing efficiency.


Examples of On-Chain Programs on Solana

1. Token Programs: These programs manage the creation, transfer, and destruction of tokens on the Solana blockchain. The SPL Token program is a standard implementation that allows developers to create and manage fungible and non-fungible tokens.

  • Example: Serum, a decentralized exchange (DEX) on Solana, uses token programs to handle the trading of various cryptocurrencies.

2. DeFi Programs: Decentralized Finance (DeFi) programs enable financial services like lending, borrowing, and trading on the blockchain without traditional financial intermediaries.

  • Example: Raydium, an automated market maker (AMM) on Solana, uses DeFi programs to facilitate liquidity provision and decentralized trading.

3. Governance Programs: These programs manage decentralized governance processes, allowing token holders to vote on proposals and changes to the protocol.

  • Example: Solana Foundation's governance program allows the community to vote on upgrades and changes to the Solana protocol.

4. Gaming Programs: These programs support blockchain-based games, enabling functionalities like in-game asset ownership, trading, and rewards.

  • Example: Star Atlas, a blockchain-based game on Solana, uses on-chain programs to manage in-game assets and interactions.

Solana Clients

Interacting with the Solana blockchain requires using specific tools that facilitate communication with the network. Solana provides both command-line interface (CLI) tools and software development kits (SDKs) for various programming languages, making it accessible for developers with different preferences and expertise levels.

Solana CLI

The Solana Command Line Interface (CLI) is a powerful tool for developers and users to interact directly with the Solana blockchain. It enables tasks such as creating wallets, sending transactions, and deploying smart contracts. Let's explore installing, configuring, and using the Solana CLI.


To install the Solana CLI, explore the following article.


After installing the CLI, configure it to connect to the Solana cluster you want to interact with. The Solana network has different clusters: mainnet-beta, testnet, and devnet.

1. Select a Cluster

For mainnet

solana config set --url

For testnet

solana config set --url

For devnet

solana config set --url

2. Configure Wallet

Generate a new key pair for your wallet or use an existing one.

solana-keygen new --outfile ~/.config/solana/id.json

3. Set Wallet Path

solana config set --keypair ~/.config/solana/id.json

Basic Commands

Here are some basic commands to get started with Solana CLI.

1. Check Balance

solana balance

2. Airdrop SOL (only on devnet or testnet)

​​​​​​​solana airdrop 2

3. Transfer SOL

solana transfer <RECIPIENT_ADDRESS> <AMOUNT>

4. Deploy a Program

solana program deploy <PROGRAM_FILEPATH>

5. Get Account Info

solana account <ACCOUNT_ADDRESS>

6. Show Config

​​​​​​​solana config get

These commands provide a foundational understanding of interacting with the Solana blockchain using the CLI.

Solana SDKs

Solana also offers SDKs in various programming languages to facilitate development. These SDKs provide high-level abstractions for interacting with the blockchain, making it easier to develop applications without dealing directly with low-level details.

Available SDKs

1. JavaScript/TypeScript (web3.js)

npm install @solana/web3.js


const solanaWeb3 = require('@solana/web3.js');
const connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl('mainnet-beta'), 'confirmed');

2. Rust


Add the following to your `Cargo.toml`:

solana-client = "1.8.0"
solana-sdk = "1.8.0"


use solana_client::rpc_client::RpcClient;
use solana_sdk::signature::Keypair;

let rpc_client = RpcClient::new("".to_string());
let keypair = Keypair::new();

3. Python (Solana-py)


pip install solana


from solana.rpc.api import Client
solana_client = Client("")

4. C#


Add the Solnet package to your project.

dotnet add package Solnet.Rpc


using Solnet.Rpc;
using Solnet.Rpc.Builders;
var rpcClient = ClientFactory.GetClient(Cluster.MainNet);

These SDKs enable developers to build and interact with the Solana blockchain using the language they are most comfortable with.


Remote Procedure Call (RPC) is a protocol that one program can use to request a service from a program located on another computer in a network. In the context of blockchain and Solana, RPC is used to communicate with the nodes in the network. It allows developers to interact with the blockchain by sending requests to read data or to broadcast transactions. The Solana RPC API is crucial because it enables developers to perform various blockchain-related tasks without needing to run a full node locally.

Why is RPC Used?

  • Access Blockchain Data: Retrieve information about accounts, transactions, and block states.
  • Submit Transactions - Send transaction data to the blockchain for processing.
  • Interact with Smart Contracts - Query and execute smart contracts.
  • Network Management - Monitor and manage the state and health of the network.

Common RPC Methods

The Solana RPC API includes a wide range of methods to interact with the blockchain. Here are some of the most commonly used ones.

1. getAccountInfo

Description: Retrieves detailed information about a specified account.


  • pubkey (string): The public key of the account to query.
  • commitment (optional): The level of commitment desired (e.g., "finalized", "confirmed").

Example Usage

       "jsonrpc": "2.0",
       "id": 1,
       "method": "getAccountInfo",
       "params": [
           "encoding": "base64"

Response: Returns the account's data, lamports (Solana's native token), owner, and more.

2. sendTransaction

Description: Sends a signed transaction to the Solana blockchain.


  • transaction (string): The base64-encoded signed transaction.
  • options (optional): Options for the transaction, such as skipping the pre-flight check.

Example Usage

       "jsonrpc": "2.0",
       "id": 1,
       "method": "sendTransaction",
       "params": [

Response: Returns the transaction signature (ID).

3. getProgramAccounts

Description: Retrieves all accounts owned by a specified program.


  • programId (string): The public key of the program to query.
  • config (optional): Additional filters or configurations.

Example Usage

       "jsonrpc": "2.0",
       "id": 1,
       "method": "getProgramAccounts",
       "params": [

Response: Returns a list of accounts, each containing the account's public key, owner, and data.


Web3.js is a popular JavaScript library that enables developers to interact with blockchain networks. Originally designed for Ethereum, web3.js has been adapted to support various blockchain protocols, including Solana. It provides a collection of modules that contain specific functionalities to make blockchain interactions straightforward and developer-friendly.

Basic web3.js Functions

1. Connecting to a Solana Node

It would be best if you connected to a node to interact with the Solana blockchain. Solana provides various clusters like 'mainnet-beta', 'testnet', and 'devnet'. Here’s how you can connect to the devnet.

const connection = new solanaWeb3.Connection(

2. Retrieving Account Data

To retrieve data from a Solana account, you need the account's public key. Here’s an example of fetching the balance of an account.

const publicKey = new solanaWeb3.PublicKey('YourPublicKeyHere');

   connection.getBalance(publicKey).then((balance) => {
       console.log(`Balance: ${balance} lamports`);
   }).catch((err) => {

3. Sending Transactions

Sending a transaction involves creating, signing, and sending it to the Solana network. Here’s how you can send SOL from one account to another.

const from = solanaWeb3.Keypair.generate();
   const to = solanaWeb3.Keypair.generate();

   // Airdrop SOL to the sender account for testing purposes
   connection.requestAirdrop(from.publicKey, solanaWeb3.LAMPORTS_PER_SOL).then(() => {
       const transaction = new solanaWeb3.Transaction().add(
               fromPubkey: from.publicKey,
               toPubkey: to.publicKey,
               lamports: solanaWeb3.LAMPORTS_PER_SOL / 100, // Transfer 0.01 SOL

       solanaWeb3.sendAndConfirmTransaction(connection, transaction, [from]).then((signature) => {
           console.log(`Transaction confirmed with signature: ${signature}`);
       }).catch((err) => {
   }).catch((err) => {


on-chain programs on Solana automate transactions and support applications like DeFi, token management, gaming, and governance, enhancing efficiency by removing intermediaries. Developers use tools like the Solana CLI, SDKs, and the RPC API to interact with the blockchain, making it easier to build decentralized applications.

Similar Articles