...
September 9, 2024

Solana Web3.js 2.0: A New Chapter in Solana Development

With consistently over 350,000 downloads per week (src: npmtrends.com), the Solana JavaScript SDK (@solana/web3.js) is one of the most popular JavaScript libraries in all of Web3. It’s used by thousands of developers to read, write, and execute logic on the Solana blockchain. If you are not a developer, you have likely used an application, wallet, or other tool that uses Solana JavaScript SDK under the hood.

In response to developer feedback on the Solana JavaScript SDK (v1.x), the Anza team (formerly known as Solana Labs) has set out to modernize the library, increase its performance, and improve the developer experience. Solana JavaScript SDK 2.0 is a complete overhaul of the library, designed to address many pain points developers have faced.

TL;DR

Here’s a quick summary of the key features and improvements in Solana JavaScript SDK 2.0:

Smaller bundles and faster load times: The new library is tree-shakable and modular, allowing you to import only the functions you need, reducing your bundle size dramatically. This leads to faster load times for your dApps and reduced compute costs for serverless deployments.Enhanced type safety: The new library leverages TypeScript’s advanced features to provide a more type-safe development experience. This means fewer runtime errors and more compile-time checks, better autocomplete and IntelliSense support in your IDE, and easier detection of common mistakes before they make it to production.Modern JavaScript features: The new library embraces modern JavaScript features, including native BigInt support for handling large numbers accurately, async iterators for working with subscriptions, and native Ed25519 support where available, falling back to a polyfill when necessary.Flexible RPC methods: The new library provides a more flexible and extensible RPC method API, allowing you to easily add custom RPC methods without modifying the core library. This makes it easier to integrate with your custom programs and tools like QuickNode Marketplace.

If you are used to the 1.x library, you will find that the new library is different and will take some practice to get used to. Though 2.0 will go live in the next few weeks, the legacy 1.x library will continue to be supported for the foreseeable future, so do not feel like you have to update your existing code if you are not ready to.

Feature

Solana JS SDK 1.x

Solana JS SDK 2.0

Architecture & Modularity

Monolithic, class-based

Modular, functional, tree-shakable

Performance & Size

Larger bundle, baseline performance

Smaller bundle, improved performance (e.g., 10x faster key operations)

Developer Experience

Basic TypeScript support, limited IDE assistance

Enhanced type safety, improved autocomplete and IntelliSense

Modern JavaScript Features

Limited

Native BigInt and Web Crypto API support, AsyncIterators for subscriptions

Flexibility & Extensibility

Fixed set of methods and features

Extensible RPC methods, custom transports, comprehensive codec system

Compatibility & Learning Curve

Established ecosystem support, familiar to existing users

Designed for modern environments, requires adaptation for 1.x users

1. Tree-Shakeability and Modular Design

One of the most significant improvements in 2.0 is its tree-shakable and modular architecture. This means:

You can now import only the functions you need (as opposed to the entire Connections class and all its methods, for example), reducing your bundle size dramatically.Smaller bundles lead to faster load times for your dApps and reduced compute costs for serverless deployments.The 2.0 library is made up of several sub-libraries that can be imported individually to handle specific tasks, rather than importing the entire library at once.

For example, instead of importing the entire 1.x library, you can now import specific elements from its smaller, more focused modules:

@solana/accounts: For fetching and decoding accounts@solana/codecs: For composing data (de)serializers from a set of primitives or building custom ones@solana/errors: For identifying and refining coded errors thrown in the @solana namespace@solana/rpc: For sending RPC requests@solana/rpc-subscriptions: For subscribing to RPC notifications@solana/signers: For building message and/or transaction signer objects@solana/sysvars: For fetching and decoding sysvar accounts@solana/transaction-messages: For building and transforming Solana transaction message objects@solana/transactions: For compiling and signing transactions for submission to the network

The net impact is a significant reduction in bundle size – in some cases, up to 80% smaller than using the entire 1.x library. In an early test of Solana Explorer, the landing page was reduced from 311KB to 226KB, a 26% reduction in size (source Solana YouTube).

2. Enhanced Type Safety

Web3.js 2.0 leverages TypeScript’s advanced features to provide a more type-safe development experience. This means:

Fewer runtime errors and more compile-time checks.Better autocomplete and IntelliSense support in your IDE.Easier detection of common mistakes before they make it to production.

For instance, the library now uses type refinement to ensure you’re using the correct parameters for different RPC methods. Common examples of this might include type errors if:

A transaction is missing a required signature.A transaction does not have a recent blockhash.An instruction is missing a required account.An account lookup table is passed into a legacy transaction.

3. Modern JavaScript Features

Web3.js 2.0 embraces modern JavaScript features, including:

Native BigInt support for handling large numbers accurately, which is important in Solana programming because Rust’s u64 type can represent numbers up to 2^64-1, while JavaScript’s Number type can only represent numbers up to 2^53-1.AsyncIterators for working with subscriptions.Native Ed25519 via CryptoKey which results in faster key generation and signing, fewer dependencies, and better compatibility with other libraries. Testing has demonstrated Keygen, Sign, and Verify performs nearly 10 times faster than the original 1.x library.

4. Extensibility – Custom RPC Methods

The new SDK is designed with extensibility in mind, allowing for seamless integration of custom RPC methods. This means you can easily access QuickNode Marketplace add-ons, your own custom Functions, or any other custom HTTP request required for your application. Further, it means you can integrate your own custom transport logic, such as sharding, retries, rate limiting, failover, etc.

This extensibility means that as we roll out new features and optimizations on our RPC endpoints, you’ll be able to take advantage of them immediately without waiting for updates to the core Solana web3.js library.

What This Means For You

As a developer using QuickNode, the Solana SDK 2.0 brings several benefits:

Faster Applications: Smaller bundle sizes and improved performance mean your dApps will load and run faster.Improved Developer Experience: Better type safety, modern JavaScript features, and tooling support make development more productive and enjoyable.Scalable: Integrate custom RPC methods, program clients, and custom transports to take advantage of new features and optimizations as they become available.Easier Debugging: Improved error handling and more predictable behavior make diagnosing and fixing issues when they occur easier.

Getting Started

To start using web3.js 2.0 with our RPC services, you can install the release candidate:

Then, you can create an RPC client pointing to our endpoint:

Conclusion

The Solana SDK 2.0 represents a significant step forward for Solana development. Its modern design, improved performance, and enhanced developer experience give you the tools you need to build the next generation of Solana applications. We’re excited to see what you’ll build with this new toolkit.

To help get you started, we have:

Added Sample Code to integrate QuickNode add-ons into your applicationCreated step-by-step technical Guides to help you get started

About QuickNode

QuickNode is building infrastructure to support the future of Web3. Since 2017, we’ve worked with hundreds of developers and companies, helping scale dApps and providing high-performance access to 40+ blockchains. Subscribe to our newsletter for more content like this, and stay in the loop with what’s happening in web3!

Leave a Reply

Your email address will not be published. Required fields are marked *

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.