Reading Data from the Solana Blockchain

In this assignment, you will use the JSON-RPC API and Solana's web3.js library to read data from the Solana blockchain.

web3.js removes a lot of the boilerplate code that would be required if we just used the JSON-RPC API, so this assignment will be written using it. We will also be using Typescript instead of Javascript, but feel free to follow along in either language.

Example

  1. First, you will need to install Solana's web3.js library and any other necessary libraries. You can do this by running the following command:

npm install @solana/web3.js
  1. The second thing you want to do is create a new connection to the network. Run the following code to create a connection to the Solana devnet, which we can use to test our code for free before deploying to mainnet

const connection = new Connection(clusterApiUrl('devnet'));
  1. We can now use the SDK to query the Solana blockchain. Let’s write a function that gets the balance of a wallet address:

const connection = new Connection(clusterApiUrl('devnet'));

async function getBalance(address: PublicKey): Promise<number> {
	return connection.getBalance(address);
}

// Example call: 

const myAddress = connection.wallet.publicKey;
const balance = await getBalance(myAddress);
const balanceInSol = balance / Web3.LAMPORTS_PER_SOL
console.log(balanceInSol);

connection.getBalance() returns the balance in a fractional SOL called lamports, which represents 0.000000001 SOL. In order to get the balance in SOL, web3.js includes a constant LAMPORTS_PER_SOL that we can divide the balance by in order to get the balance in Solana.

Assignment

Use the Solana web3.js SDK to create a React/Next.js application that shows the balance of a user-inputted wallet, along with whether the account is executable or not.

An example input and output would look something like the following:

console.log(getBalance("my_wallet")) // returns balance in SOL
console.log(isExecutable("my_wallet")) // returns true or false

We already know how to get the balance of the wallet, so use the SDK documentation to figure out how to get whether the account is executable or not (our recommendation is to CMD+F for accountInfo)

If you get stuck, refer to the hints below:

Hint 1
Hint 2

Here’s some example code to get you started

import web3 from "@solana/web3/js";

let connection = new Web3.connection(web3.clusterApiUrl("devnet"));

let testAccount = web3.Keypair.generate(); // example account for demo

let accountInfo = await connection.getAccountInfo(testAccount.publicKey);

console.log(accountInfo);
Solution
import web3 from "@solana/web3/js";

let connection = new Web3.connection(web3.clusterApiUrl("devnet"));

const isExecutable(address: publicKey): Promise<boolean> {
	connection.getAccountInfo(address).then((info) => {
		return info.executable;
	}
}

// Running the function with a demo keypair

let testAccount = web3.Keypair.generate(); // example account for demo

console.log(isExecutable(testAccount.publicKey));

Once you’re done, make a PR with your react app to the homework repository on Github.

Last updated